Completely rework the network stack for faster lower latency messages

This commit is contained in:
derek committed 2026-07-03 15:00:01 -07:00
1 parent a689f2656f
commit ca39e184ed
7 files changed
+133 -129

No files matched your search

+21 -7
View File
@@ -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;
}
}
}
}