0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-29 06:33:06 +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,15 +92,24 @@ 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); {
if (size == 0) { int readOffset = 0;
// treat it as a disconnect and exit while (readOffset < Constants.MaxPacketSize) {
Logger.Info($"Socket {socket.RemoteEndPoint} disconnected."); int size = await socket.ReceiveAsync(memory.Memory[readOffset..Constants.MaxPacketSize], SocketFlags.None);
if (socket.Connected) await socket.DisconnectAsync(false); if (size == 0) {
break; // treat it as a disconnect and exit
Logger.Info($"Socket {socket.RemoteEndPoint} disconnected.");
if (socket.Connected) await socket.DisconnectAsync(false);
break;
}
readOffset += size;
}
if (readOffset < Constants.MaxPacketSize) break;
} }
PacketHeader header = GetHeader(memory.Memory.Span[..size]); 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) {