mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-25 20:55:18 +00:00
More changes to reading varsize packets
This commit is contained in:
parent
8c5cd0ced2
commit
89dcb7f6c9
1 changed files with 9 additions and 5 deletions
|
@ -120,7 +120,7 @@ public class Server {
|
||||||
while (true) {
|
while (true) {
|
||||||
memory = memoryPool.Rent(Constants.HeaderSize);
|
memory = memoryPool.Rent(Constants.HeaderSize);
|
||||||
|
|
||||||
async Task Read(Memory<byte> readMem, int readOffset = 0, int readSize = -1) {
|
async Task<bool> Read(Memory<byte> readMem, int readOffset = 0, int readSize = -1) {
|
||||||
if (readSize == -1) readSize = Constants.HeaderSize;
|
if (readSize == -1) readSize = Constants.HeaderSize;
|
||||||
while (readOffset < readSize) {
|
while (readOffset < readSize) {
|
||||||
int size = await socket.ReceiveAsync(readMem[readOffset..readSize], SocketFlags.None);
|
int size = await socket.ReceiveAsync(readMem[readOffset..readSize], SocketFlags.None);
|
||||||
|
@ -128,22 +128,26 @@ public class Server {
|
||||||
// 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.");
|
||||||
if (socket.Connected) await socket.DisconnectAsync(false);
|
if (socket.Connected) await socket.DisconnectAsync(false);
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
readOffset += size;
|
readOffset += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
await Read(memory.Memory[..Constants.HeaderSize]);
|
if (!await Read(memory.Memory[..Constants.HeaderSize])) break;
|
||||||
PacketHeader header = GetHeader(memory.Memory.Span[..Constants.HeaderSize]);
|
PacketHeader header = GetHeader(memory.Memory.Span[..Constants.HeaderSize]);
|
||||||
{
|
{
|
||||||
IMemoryOwner<byte> memTemp = memory;
|
IMemoryOwner<byte> memTemp = memory;
|
||||||
memory = memoryPool.Rent(Constants.HeaderSize + header.Size);
|
memory = memoryPool.Rent(Constants.HeaderSize + header.PacketSize);
|
||||||
memTemp.Memory.CopyTo(memory.Memory);
|
memTemp.Memory.CopyTo(memory.Memory);
|
||||||
memTemp.Dispose();
|
memTemp.Dispose();
|
||||||
}
|
}
|
||||||
await Read(memory.Memory[Constants.HeaderSize..(Constants.HeaderSize + header.Size)]);
|
if (header.PacketSize > 0
|
||||||
|
&& !await Read(memory.Memory[Constants.HeaderSize..(Constants.HeaderSize + header.PacketSize)]))
|
||||||
|
break;
|
||||||
|
|
||||||
// connection initialization
|
// connection initialization
|
||||||
if (first) {
|
if (first) {
|
||||||
|
|
Loading…
Reference in a new issue