From 7bbba0c691271ed89d5f5c88c8f6b451b019a363 Mon Sep 17 00:00:00 2001 From: Sanae Date: Thu, 10 Feb 2022 22:25:47 -0600 Subject: [PATCH] =?UTF-8?q?Everything=20just=20works=E2=84=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/Client.cs | 7 ++----- Server/Server.cs | 24 +++++++++++++++++------- Shared/Constants.cs | 2 +- TestClient/Program.cs | 30 +++++++++++++++++++++++------- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 1bb4094..57ae8e0 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -9,10 +9,7 @@ public class Client : IDisposable { public readonly Dictionary Metadata = new Dictionary(); // can be used to store any information about a player public bool Connected = false; - public CostumePacket CurrentCostume = new CostumePacket { - BodyName = "", - CapName = "" - }; + public CostumePacket? CurrentCostume; public Guid Id; public Socket? Socket; @@ -27,7 +24,7 @@ public class Client : IDisposable { Server.Logger.Info($"Didn't send {(PacketType) data.Span[16]} to {Id} because they weren't connected yet"); return; } - Server.Logger.Info($"Sending {(PacketType) data.Span[16]} to {Id} from {other?.Id.ToString() ?? "server"}"); + // Server.Logger.Info($"Sending {(PacketType) data.Span[16]} to {Id} from {other?.Id.ToString() ?? "server"}"); await Socket!.SendAsync(data[..Constants.MaxPacketSize], SocketFlags.None); } diff --git a/Server/Server.cs b/Server/Server.cs index 2c4cc06..36bfe80 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -43,7 +43,7 @@ public class Server { } // broadcast packets to all clients - public async Task Broadcast(T packet, Client? sender = null) where T : unmanaged, IPacket { + public async Task Broadcast(T packet, Client sender) where T : unmanaged, IPacket { IMemoryOwner memory = memoryPool.Rent(Constants.MaxPacketSize); PacketHeader header = new PacketHeader { @@ -139,25 +139,35 @@ public class Server { List otherConnectedPlayers = Clients.FindAll(c => c.Id != header.Id && c.Connected && c.Socket != null); await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => { - IMemoryOwner connectBuffer = MemoryPool.Shared.Rent(Constants.MaxPacketSize); + IMemoryOwner tempBuffer = MemoryPool.Shared.Rent(Constants.MaxPacketSize); PacketHeader connectHeader = new PacketHeader { Id = other.Id, Type = PacketType.Connect }; - MemoryMarshal.Write(connectBuffer.Memory.Span[Constants.HeaderSize..], ref connectHeader); + MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader); ConnectPacket connectPacket = new ConnectPacket { ConnectionType = ConnectionTypes.FirstConnection // doesn't matter what it is :) }; - MemoryMarshal.Write(connectBuffer.Memory.Span, ref connectPacket); - await client.Send(connectBuffer.Memory, null); - connectBuffer.Dispose(); + MemoryMarshal.Write(tempBuffer.Memory.Span[Constants.HeaderSize..], ref connectPacket); + await client.Send(tempBuffer.Memory, null); + if (other.CurrentCostume is {} costumePacket) { + connectHeader.Type = PacketType.Costume; + MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader); + costumePacket.Serialize(tempBuffer.Memory.Span[Constants.HeaderSize..]); + await client.Send(tempBuffer.Memory, null); + } + tempBuffer.Dispose(); }); Logger.Info($"Client {socket.RemoteEndPoint} ({client.Id}) connected."); } // todo support variable length packets if they show up - Logger.Warn($"broadcasting {header.Type} from {client.Id}"); + // Logger.Warn($"broadcasting {header.Type} from {client.Id}"); + if (header.Type == PacketType.Costume) { + client.CurrentCostume ??= new CostumePacket(); + client.CurrentCostume.Value.Deserialize(memory.Memory.Span[Constants.HeaderSize..]); + } await Broadcast(memory, client); } } catch (Exception e) { diff --git a/Shared/Constants.cs b/Shared/Constants.cs index 97cbb90..aea83bc 100644 --- a/Shared/Constants.cs +++ b/Shared/Constants.cs @@ -19,7 +19,7 @@ public static class Constants { public static readonly Dictionary PacketIdMap = Assembly .GetExecutingAssembly() .GetTypes() - .Where(type => type.IsAssignableTo(typeof(IPacket))) + .Where(type => type.IsAssignableTo(typeof(IPacket)) && type.GetCustomAttribute() != null) .ToDictionary(type => type.GetCustomAttribute()!.Type, type => type); public static int HeaderSize { get; } = Marshal.SizeOf(); diff --git a/TestClient/Program.cs b/TestClient/Program.cs index 5c45211..3c524ba 100644 --- a/TestClient/Program.cs +++ b/TestClient/Program.cs @@ -7,20 +7,19 @@ 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 = Guid.NewGuid(); -Guid otherId = Guid.Empty; +Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); +// Guid ownId = Guid.NewGuid(); +Guid otherId = Guid.Parse("d5feae62-2e71-1000-88fd-597ea147ae88"); Logger logger = new Logger("Client"); NetworkStream stream = client.GetStream(); int e = 0; double d = 0; Vector3 basePoint = Vector3.Zero; -PlayerPacket? playerPacket = null; PacketType[] reboundPackets = { PacketType.Player, - PacketType.Cap, + // PacketType.Cap, PacketType.Capture, PacketType.Costume, PacketType.Tag, @@ -33,8 +32,25 @@ 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 (header.Id != otherId) continue; + if (type is PacketType.Player) { + CapPacket cap = new CapPacket(); + PlayerPacket playerPacket = new PlayerPacket(); + playerPacket.Deserialize(owner.Memory.Span[Constants.HeaderSize..]); + cap.Position = playerPacket.Position + Vector3.UnitY * 500f; + // cap.Rotation = Quaternion.CreateFromYawPitchRoll(0,0,0); + cap.CapAnim = "StayR"; + playerPacket.Position = new Vector3(1000000f); + playerPacket.ThrowingCap = true; + header.Id = ownId; + MemoryMarshal.Write(owner.Memory.Span, ref header); + playerPacket.Serialize(owner.Memory.Span[Constants.HeaderSize..]); + await stream.WriteAsync(owner.Memory[..Constants.MaxPacketSize]); + header.Type = PacketType.Cap; + MemoryMarshal.Write(owner.Memory.Span, ref header); + cap.Serialize(owner.Memory.Span[Constants.HeaderSize..]); + await stream.WriteAsync(owner.Memory[..Constants.MaxPacketSize]); + continue; } if (reboundPackets.All(x => x != type)) continue; header.Id = ownId;