0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-23 19:55:18 +00:00

Add RentZero extension as rented memory can be non-zero

This commit is contained in:
Sanae 2022-02-16 15:20:03 -06:00
parent b173e53826
commit 78af568603
4 changed files with 17 additions and 7 deletions

View file

@ -32,7 +32,7 @@ public class Client : IDisposable {
}
public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPacket {
IMemoryOwner<byte> memory = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize);
IMemoryOwner<byte> memory = MemoryPool<byte>.Shared.RentZero(Constants.MaxPacketSize);
PacketHeader header = new PacketHeader {
Id = sender?.Id ?? Guid.Empty,

View file

@ -58,7 +58,7 @@ public class Server {
}
}
public async Task Broadcast<T>(T packet, Client sender) where T : struct, IPacket {
IMemoryOwner<byte> memory = memoryPool.Rent(Constants.MaxPacketSize);
IMemoryOwner<byte> memory = memoryPool.RentZero(Constants.MaxPacketSize);
PacketHeader header = new PacketHeader {
Id = sender?.Id ?? Guid.Empty,
@ -168,7 +168,7 @@ public class Server {
List<Client> otherConnectedPlayers = Clients.FindAll(c => c.Id != header.Id && c.Connected && c.Socket != null);
await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
IMemoryOwner<byte> tempBuffer = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize);
IMemoryOwner<byte> tempBuffer = MemoryPool<byte>.Shared.RentZero(Constants.MaxPacketSize);
PacketHeader connectHeader = new PacketHeader {
Id = other.Id,
Type = PacketType.Connect
@ -197,14 +197,14 @@ public class Server {
CostumePacket costumePacket = new CostumePacket {
BodyName = ""
};
costumePacket.Deserialize(memory.Memory.Span[Constants.HeaderSize..]);
costumePacket.Deserialize(memory.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize]);
client.CurrentCostume = costumePacket;
}
try {
// if (header.Type is not PacketType.Cap and not PacketType.Player) client.Logger.Warn($"lol {header.Type}");
IPacket packet = (IPacket) Activator.CreateInstance(Constants.PacketIdMap[header.Type])!;
packet.Deserialize(memory.Memory.Span[Constants.HeaderSize..]);
packet.Deserialize(memory.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize]);
if (PacketHandler?.Invoke(client, packet) is false) {
memory.Dispose();
continue;

View file

@ -1,4 +1,8 @@
namespace Shared;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace Shared;
public static class Extensions {
public static string Hex(this Span<byte> span) {
@ -18,4 +22,10 @@ public static class Extensions {
public static string TrimNullTerm(this string text) {
return text.Split('\0').FirstOrDefault() ?? "";
}
public static IMemoryOwner<byte> RentZero(this MemoryPool<byte> pool, int minSize) {
IMemoryOwner<byte> owner = pool.Rent(minSize);
CryptographicOperations.ZeroMemory(owner.Memory.Span);
return owner;
}
}

View file

@ -61,7 +61,7 @@ PacketHeader coolHeader = new PacketHeader {
Type = PacketType.Connect,
Id = ownId
};
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize);
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.RentZero(Constants.MaxPacketSize);
MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader);
ConnectPacket connect = new ConnectPacket {
ConnectionType = ConnectionTypes.Reconnecting,