Completely rework the network stack for faster lower latency messages
This commit is contained in:
1 parent
a689f2656f
commit
ca39e184ed
7 files changed
+133
-129
No files matched your search
@@ -23,18 +23,36 @@ namespace Mistox.Client {
|
||||
async Task ReceiveThread() {
|
||||
using( NetworkStream ns = Server.GetStream() ) {
|
||||
try {
|
||||
byte[] headerBuffer = new byte[DataPacket.HeaderLength];
|
||||
byte[] payloadBuffer = new byte[65535]; // Upper limit for safety
|
||||
while( ClientInterface.ServerClientInstance.Alive ) {
|
||||
byte[] StreamData = new byte[1024];
|
||||
int bytesRead = await ns.ReadAsync(StreamData, 0, StreamData.Length);
|
||||
if( bytesRead > 0 ) {
|
||||
dynamic data = Serializer.tReceive( StreamData.Sub( 0, bytesRead ) );
|
||||
if( data != null ) {
|
||||
ClientInterface.ServerClientInstance.InvokeOnReceived( data );
|
||||
}
|
||||
|
||||
// Read the header off
|
||||
await ns.ReadExactlyAsync(headerBuffer, 0, 3);
|
||||
|
||||
// Parse the header
|
||||
byte packetbyte = headerBuffer[0];
|
||||
ushort dataLength = (ushort)((headerBuffer[1] << 8) | headerBuffer[2]);
|
||||
|
||||
// Make sure the data looks valid
|
||||
if (dataLength > payloadBuffer.Length) {
|
||||
throw new Exception("Incoming TCP payload is too large for the pre-allocated buffer.");
|
||||
}
|
||||
|
||||
// Read off the data
|
||||
await ns.ReadExactlyAsync(payloadBuffer, 0, dataLength);
|
||||
|
||||
// Create the dataobject
|
||||
dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
|
||||
|
||||
// Return the object
|
||||
ClientInterface.ServerClientInstance.InvokeOnReceived(dataobject);
|
||||
|
||||
}
|
||||
} catch( Exception e ) {
|
||||
Console.WriteLine( "TCP Receive exception : " + e.Message );
|
||||
} catch (Exception e) {
|
||||
ClientInterface.ServerClientInstance.Alive = false;
|
||||
Dispose();
|
||||
Console.WriteLine("TCP Receive exception: " + e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ namespace Mistox.Client {
|
||||
public class mUDPClient : IDisposable {
|
||||
|
||||
Socket udpClient;
|
||||
|
||||
IPEndPoint remoteHost;
|
||||
|
||||
public mUDPClient( long RemoteIP, int Port ) {
|
||||
@@ -25,14 +24,29 @@ namespace Mistox.Client {
|
||||
}
|
||||
|
||||
async Task ReceiveThread() {
|
||||
byte[] buffer = new byte[65535];
|
||||
while( ClientInterface.ServerClientInstance.Alive ) {
|
||||
try {
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
// Read the data out of the UDP packet with a buffer that has the maximum possible UDP packet size
|
||||
// As UDP reads the whole next packet then drops. Im guaranteed to receive 1 packet at a time
|
||||
int bytesRead = await udpClient.ReceiveAsync( buffer );
|
||||
dynamic data = Serializer.uReceive( buffer.Sub(0, bytesRead) );
|
||||
if( data != null ) {
|
||||
ClientInterface.ServerClientInstance.InvokeOnReceived( data );
|
||||
}
|
||||
|
||||
// Create the data readers
|
||||
ReadOnlyMemory<byte> dataMemory = buffer.AsMemory(0, bytesRead);
|
||||
ReadOnlySpan<byte> dataPacket = dataMemory.Span;
|
||||
|
||||
// Split apart the packet
|
||||
byte dataType = dataPacket[0];
|
||||
ushort DataLength = (ushort)((dataPacket[1] << 8) | dataPacket[2]);
|
||||
ReadOnlyMemory<byte> data = dataMemory.Slice(DataPacket.HeaderLength, DataLength);
|
||||
|
||||
// Extract the object
|
||||
dynamic buildObj = DataPacket.Deserialize( dataType, DataLength, data );
|
||||
|
||||
// Return the object
|
||||
ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj );
|
||||
|
||||
} catch( Exception e ) {
|
||||
Console.WriteLine( "UDP Receive exception : " + e.Message );
|
||||
}
|
||||
@@ -50,4 +64,4 @@ namespace Mistox.Client {
|
||||
udpClient = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user