0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-28 22:23:05 +00:00

Read until received full packet size

This commit is contained in:
Sanae 2022-02-15 15:17:18 -06:00
parent 57560a9db1
commit 4ccf33ad37

View file

@ -92,7 +92,10 @@ public class Server {
try { try {
while (true) { while (true) {
memory = memoryPool.Rent(Constants.MaxPacketSize); memory = memoryPool.Rent(Constants.MaxPacketSize);
int size = await socket.ReceiveAsync(memory.Memory, SocketFlags.None); {
int readOffset = 0;
while (readOffset < Constants.MaxPacketSize) {
int size = await socket.ReceiveAsync(memory.Memory[readOffset..Constants.MaxPacketSize], SocketFlags.None);
if (size == 0) { if (size == 0) {
// treat it as a disconnect and exit // treat it as a disconnect and exit
Logger.Info($"Socket {socket.RemoteEndPoint} disconnected."); Logger.Info($"Socket {socket.RemoteEndPoint} disconnected.");
@ -100,7 +103,13 @@ public class Server {
break; break;
} }
PacketHeader header = GetHeader(memory.Memory.Span[..size]); readOffset += size;
}
if (readOffset < Constants.MaxPacketSize) break;
}
PacketHeader header = GetHeader(memory.Memory.Span[..Constants.MaxPacketSize]);
//Logger.Info($"first = {first}, type = {header.Type}, data = " + memory.Memory.Span[..size].Hex()); //Logger.Info($"first = {first}, type = {header.Type}, data = " + memory.Memory.Span[..size].Hex());
// connection initialization // connection initialization
if (first) { if (first) {
@ -108,7 +117,7 @@ public class Server {
if (header.Type != PacketType.Connect) throw new Exception($"First packet was not init, instead it was {header.Type}"); if (header.Type != PacketType.Connect) throw new Exception($"First packet was not init, instead it was {header.Type}");
ConnectPacket connect = new ConnectPacket(); ConnectPacket connect = new ConnectPacket();
connect.Deserialize(memory.Memory.Span[Constants.HeaderSize..size]); connect.Deserialize(memory.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize]);
lock (Clients) { lock (Clients) {
bool firstConn = false; bool firstConn = false;
switch (connect.ConnectionType) { switch (connect.ConnectionType) {