diff --git a/Server/Client.cs b/Server/Client.cs index 8ad978b..b38d663 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,4 +1,5 @@ using System.Net.Sockets; +using Shared; using Shared.Packet.Packets; namespace Server; @@ -19,9 +20,9 @@ public class Client : IDisposable { Socket?.Disconnect(false); } - public async Task Send(Memory data) { + public async Task Send(ReadOnlyMemory data) { if (!Connected) return; - await Socket!.SendAsync(data, SocketFlags.None); + await Socket!.SendAsync(data[..Constants.MaxPacketSize], SocketFlags.None); } public static bool operator ==(Client? left, Client? right) { diff --git a/Server/Server.cs b/Server/Server.cs index 5748876..86d591c 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -48,7 +48,7 @@ public class Server { PacketHeader header = new PacketHeader { Id = sender?.Id ?? Guid.Empty, - Type = Constants.Packets[typeof(T)].Type + Type = Constants.PacketMap[typeof(T)].Type }; FillPacket(header, packet, memory.Memory); await Broadcast(memory, sender); @@ -147,7 +147,7 @@ public class Server { Id = other.Id, Type = PacketType.Connect }; - MemoryMarshal.Write(connectBuffer.Memory.Span, ref connectHeader); + MemoryMarshal.Write(connectBuffer.Memory.Span[Constants.HeaderSize..], ref connectHeader); ConnectPacket connectPacket = new ConnectPacket { ConnectionType = ConnectionTypes.FirstConnection // doesn't matter what it is :) }; diff --git a/Shared/Constants.cs b/Shared/Constants.cs index 5141a8b..97cbb90 100644 --- a/Shared/Constants.cs +++ b/Shared/Constants.cs @@ -11,11 +11,16 @@ public static class Constants { public const int CostumeNameSize = 0x20; // dictionary of packet types to packet - public static readonly Dictionary Packets = Assembly + public static readonly Dictionary PacketMap = Assembly .GetExecutingAssembly() .GetTypes() .Where(type => type.IsAssignableTo(typeof(IPacket))) .ToDictionary(type => type, type => type.GetCustomAttribute()!); + public static readonly Dictionary PacketIdMap = Assembly + .GetExecutingAssembly() + .GetTypes() + .Where(type => type.IsAssignableTo(typeof(IPacket))) + .ToDictionary(type => type.GetCustomAttribute()!.Type, type => type); public static int HeaderSize { get; } = Marshal.SizeOf(); public static int PacketDataSize { get; } = MaxPacketSize - HeaderSize; diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj index f7c9978..727c233 100644 --- a/Shared/Shared.csproj +++ b/Shared/Shared.csproj @@ -8,7 +8,8 @@ - + + diff --git a/TestClient/Program.cs b/TestClient/Program.cs index 37d9e2b..5c45211 100644 --- a/TestClient/Program.cs +++ b/TestClient/Program.cs @@ -7,7 +7,8 @@ using Shared.Packet; using Shared.Packet.Packets; TcpClient client = new TcpClient(args[0], 1027); -Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); +// Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); +Guid ownId = Guid.NewGuid(); Guid otherId = Guid.Empty; Logger logger = new Logger("Client"); NetworkStream stream = client.GetStream(); @@ -32,10 +33,13 @@ async Task S() { await stream.ReadAsync(owner.Memory); PacketHeader header = MemoryMarshal.Read(owner.Memory.Span); PacketType type = header.Type; + if (type is not PacketType.Cap and not PacketType.Player) { + + } if (reboundPackets.All(x => x != type)) continue; header.Id = ownId; MemoryMarshal.Write(owner.Memory.Span, ref header); - await stream.WriteAsync(owner.Memory); + await stream.WriteAsync(owner.Memory[..Constants.MaxPacketSize]); } } @@ -50,7 +54,5 @@ ConnectPacket connect = new ConnectPacket { }; MemoryMarshal.Write(owner.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize], ref connect); await stream.WriteAsync(owner.Memory); -coolHeader.Type = PacketType.Player; -MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader); logger.Info("Connected"); await S(); \ No newline at end of file