diff --git a/Server/Client.cs b/Server/Client.cs index 1002404..4dc6339 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,5 +1,6 @@ using System.Buffers; using System.Net.Sockets; +using Shared.Packet.Packets; namespace Server; @@ -7,6 +8,10 @@ public class Client : IDisposable { public Socket? Socket; public bool Connected => Socket?.Connected ?? false; public Guid Id; + public CostumePacket CurrentCostume = new CostumePacket { + BodyName = "", + CapName = "" + }; public readonly Dictionary Metadata = new Dictionary(); // can be used to store any information about a player public async Task Send(Memory data) { diff --git a/Server/Server.cs b/Server/Server.cs index 5517c38..2356039 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -14,7 +14,7 @@ public class Server { public readonly Logger Logger = new Logger("Server"); public async Task Listen(ushort port) { Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - serverSocket.Bind(new IPEndPoint(IPAddress.Loopback, port)); + serverSocket.Bind(new IPEndPoint(IPAddress.Any, port)); serverSocket.Listen(); Logger.Info($"Listening on port {port}"); @@ -130,14 +130,11 @@ public class Server { Clients.Add(client); Parallel.ForEachAsync(toDisconnect, (c, token) => c.Socket!.DisconnectAsync(false, token)); - // Broadcast(connect, client); // done disconnecting and removing stale clients with the same id } } Logger.Info($"Client {socket.RemoteEndPoint} connected."); - - // continue; } diff --git a/Shared/Packet/Packets/UnhandledPacket.cs b/Shared/Packet/Packets/UnhandledPacket.cs index db33369..ad7350c 100644 --- a/Shared/Packet/Packets/UnhandledPacket.cs +++ b/Shared/Packet/Packets/UnhandledPacket.cs @@ -1,7 +1,7 @@ namespace Shared.Packet.Packets; [Packet(PacketType.Unknown)] // empty like boss -[Packet(PacketType.Command)] +// [Packet(PacketType.Command)] public struct UnhandledPacket : IPacket { public byte[] Data = new byte[Constants.PacketDataSize]; public void Serialize(Span data) { diff --git a/TestClient/Program.cs b/TestClient/Program.cs index 30af137..5a14bdb 100644 --- a/TestClient/Program.cs +++ b/TestClient/Program.cs @@ -18,68 +18,25 @@ double d = 0; Vector3 basePoint = Vector3.Zero; PlayerPacket? playerPacket = null; -async void Funny() { - Memory memory = new Memory(new byte[256]); - - { - PacketHeader header = new PacketHeader { - Id = ownId, - Type = PacketType.Player - }; - MemoryMarshal.Write(memory.Span, ref header); - } - - while (true) { - d += Math.PI / 32; - if (playerPacket == null) { - // wait until valid player packet has arrived - await Task.Delay(300); - continue; - } - - PlayerPacket packet = playerPacket.Value; - Vector3 pos = basePoint; - pos.X += 100f * (float) Math.Cos(d); - pos.Y += 300f; - pos.Z += 100f * (float) Math.Sin(d); - packet.Position = pos; - packet.Serialize(memory.Span[Constants.HeaderSize..]); - logger.Warn($"Current strs:{packet.Stage}-{packet.Act}-{packet.SubAct} {packet.Is2d} {packet.ThrowingCap} {packet.IsIt}"); - - await stream.WriteAsync(memory); - await Task.Delay(50); - } -} +PacketType[] reboundPackets = { + PacketType.Player, + PacketType.Cap, + PacketType.Capture, + PacketType.Costume, + PacketType.Tag, + PacketType.Shine +}; async Task S() { IMemoryOwner owner = MemoryPool.Shared.Rent(256); while (true) { await stream.ReadAsync(owner.Memory); - unsafe { - - PacketHeader header = MemoryMarshal.Read(owner.Memory.Span); - if (header.Type == PacketType.Player) { - if (otherId == Guid.Empty) otherId = header.Id; - if (otherId != header.Id) continue; - if (e++ != 0) { - e %= 3; - continue; - } - - header.Id = ownId; - MemoryMarshal.Write(owner.Memory.Span, ref header); - fixed (byte* data = owner.Memory.Span[Constants.HeaderSize..]) { - logger.Error($"{Marshal.OffsetOf(nameof(PlayerPacket.AnimationBlendWeights))} {Marshal.OffsetOf(nameof(PlayerPacket.AnimationRate))}"); - PlayerPacket packet = Marshal.PtrToStructure((IntPtr) data); - playerPacket = packet; - basePoint = packet.Position; - } - - // packet.SubAct = ""; - } else if (header.Type == PacketType.Cap) { - - } - } + PacketHeader header = MemoryMarshal.Read(owner.Memory.Span); + PacketType type = header.Type; + if (reboundPackets.All(x => x != type)) continue; + header.Id = ownId; + MemoryMarshal.Write(owner.Memory.Span, ref header); + await stream.WriteAsync(owner.Memory); } } @@ -97,5 +54,4 @@ await stream.WriteAsync(owner.Memory); coolHeader.Type = PacketType.Player; MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader); logger.Info("Connected"); -Task.Run(Funny); await S(); \ No newline at end of file