The main problem for the slowdown is the fact that you trash the socket totally.
WriteLine / ReadLine are a TOTAL no go for realtime!
They work by reading byte per byte till they find an end line, which is about the worst thing you can do!
Make your application write packets.
A packets contains:
1. Unique ID definining the content
1.2. Size of the packet in whole if it has varying sizes (for something that transfers strings like chat for example)
2. The content as specifyed by the Unique ID
For example for the position update you would have a
const P_UpdatePosition:int = 1
and the packet structure would be
byte | float | float | float | float | float | float
or in non typed:
P_UpdatePosition | X | Y | Z | RotationX | RotationY | RotationZ
the | are not written into the package, they are only to visually seperate the different byte parts.
That way the server / client just has to:
1. Check if bytes are available
2. if yes, read the first byte to get the type of package
3. Wait till as many bytes are at least available as the message size is. In above case this would mean wait till at least 24 bytes are available
4. ReadBytes and read the whole remaining message as 1 memory block.
5. parse the data out of the block in the way you prefer (TBank, memcopy into a TPositionUpdate type instance, ...)
If your game is ground based with no free 3D movement, RotationX and RotationZ can be removed theoretically.
if it is 2D you can remove the Z as well
WriteLine / ReadLine are a TOTAL no go for realtime!
They work by reading byte per byte till they find an end line, which is about the worst thing you can do!
Make your application write packets.
A packets contains:
1. Unique ID definining the content
1.2. Size of the packet in whole if it has varying sizes (for something that transfers strings like chat for example)
2. The content as specifyed by the Unique ID
For example for the position update you would have a
const P_UpdatePosition:int = 1
and the packet structure would be
byte | float | float | float | float | float | float
or in non typed:
P_UpdatePosition | X | Y | Z | RotationX | RotationY | RotationZ
the | are not written into the package, they are only to visually seperate the different byte parts.
That way the server / client just has to:
1. Check if bytes are available
2. if yes, read the first byte to get the type of package
3. Wait till as many bytes are at least available as the message size is. In above case this would mean wait till at least 24 bytes are available
4. ReadBytes and read the whole remaining message as 1 memory block.
5. parse the data out of the block in the way you prefer (TBank, memcopy into a TPositionUpdate type instance, ...)
If your game is ground based with no free 3D movement, RotationX and RotationZ can be removed theoretically.
if it is 2D you can remove the Z as well