mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-25 04:35:18 +00:00
Read until received full packet size
This commit is contained in:
parent
57560a9db1
commit
4ccf33ad37
1 changed files with 17 additions and 8 deletions
|
@ -92,7 +92,10 @@ public class Server {
|
|||
try {
|
||||
while (true) {
|
||||
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) {
|
||||
// treat it as a disconnect and exit
|
||||
Logger.Info($"Socket {socket.RemoteEndPoint} disconnected.");
|
||||
|
@ -100,7 +103,13 @@ public class Server {
|
|||
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());
|
||||
// connection initialization
|
||||
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}");
|
||||
|
||||
ConnectPacket connect = new ConnectPacket();
|
||||
connect.Deserialize(memory.Memory.Span[Constants.HeaderSize..size]);
|
||||
connect.Deserialize(memory.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize]);
|
||||
lock (Clients) {
|
||||
bool firstConn = false;
|
||||
switch (connect.ConnectionType) {
|
||||
|
|
Loading…
Reference in a new issue