diff --git a/Server/Client.cs b/Server/Client.cs index a67e981..60a0753 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -19,10 +19,16 @@ public class Client : IDisposable { public Guid Id; public Socket? Socket; public Server Server { get; init; } - public Logger Logger { get; init; } = new Logger("Unknown User"); + public Logger Logger { get; } + public Client(Socket socket) { + Socket = socket; + Logger = new Logger(socket.RemoteEndPoint?.ToString() ?? "Unknown User???"); + } + public void Dispose() { - Socket?.Disconnect(false); + if (Socket?.Connected is true) + Socket.Disconnect(false); } public async Task Send(T packet, Client? sender = null) where T : unmanaged, IPacket { diff --git a/Server/Program.cs b/Server/Program.cs index ae59899..703205f 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -44,7 +44,7 @@ server.PacketHandler += async (c, p) => { ConcurrentBag playerBag = (ConcurrentBag) c.Metadata["shineSync"]; shineBag.Add(shinePacket.ShineId); if (playerBag.Contains(shinePacket.ShineId)) return; - Console.WriteLine($"{c.Name} got {shinePacket.ShineId}"); + c.Logger.Info($"Got shine {shinePacket.ShineId}"); playerBag.Add(shinePacket.ShineId); SyncShineBag(); break; diff --git a/Server/Server.cs b/Server/Server.cs index 5490ce3..614117c 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -26,7 +26,7 @@ public class Server { while (true) { Socket socket = await serverSocket.AcceptAsync(); - Logger.Warn("ok"); + Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}"); try { if (Clients.Count > Constants.MaxClients) { @@ -35,7 +35,7 @@ public class Server { continue; } - HandleSocket(socket); + Task.Run(() => HandleSocket(socket)); } catch { // super ignore this } @@ -86,7 +86,7 @@ public class Server { private async void HandleSocket(Socket socket) { - Client client = new Client { Socket = socket, Server = this }; + Client client = new Client(socket) { Server = this }; IMemoryOwner memory = null!; bool first = true; try { @@ -96,7 +96,7 @@ public class Server { if (size == 0) { // treat it as a disconnect and exit Logger.Info($"Socket {socket.RemoteEndPoint} disconnected."); - await socket.DisconnectAsync(false); + if (socket.Connected) await socket.DisconnectAsync(false); break; } @@ -159,7 +159,8 @@ public class Server { }; MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader); ConnectPacket connectPacket = new ConnectPacket { - ConnectionType = ConnectionTypes.FirstConnection // doesn't matter what it is :) + ConnectionType = ConnectionTypes.FirstConnection, // doesn't matter what it is :) + ClientName = other.Name[..Constants.CostumeNameSize] }; connectPacket.Serialize(tempBuffer.Memory.Span[Constants.HeaderSize..]); await client.Send(tempBuffer.Memory, null); @@ -173,7 +174,7 @@ public class Server { tempBuffer.Dispose(); }); - Logger.Info($"Client {socket.RemoteEndPoint} ({client.Id}) connected."); + Logger.Info($"Client {client.Name} ({client.Id}/{socket.RemoteEndPoint}) connected."); } if (header.Type == PacketType.Costume) { @@ -185,7 +186,7 @@ public class Server { } try { - if (header.Type is not PacketType.Cap and not PacketType.Player) Logger.Warn($"lol {header.Type}"); + // 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..]); PacketHandler?.Invoke(client, packet); @@ -193,14 +194,14 @@ public class Server { // ignore failed packet deserialization! } - await Broadcast(memory, client); + Broadcast(memory, client); } } catch (Exception e) { if (e is SocketException { SocketErrorCode: SocketError.ConnectionReset }) { - Logger.Info($"Client {socket.RemoteEndPoint} ({client.Id}) disconnected from the server"); + client.Logger.Info($"Client {socket.RemoteEndPoint} ({client.Id}) disconnected from the server"); } else { - Logger.Error($"Exception on socket {socket.RemoteEndPoint} ({client.Id}) and disconnecting for: {e}"); - Task.Run(() => socket.DisconnectAsync(false)); + client.Logger.Error($"Exception on socket {socket.RemoteEndPoint} ({client.Id}) and disconnecting for: {e}"); + if (socket.Connected) Task.Run(() => socket.DisconnectAsync(false)); } memory?.Dispose(); diff --git a/Shared/Constants.cs b/Shared/Constants.cs index aea83bc..132c76c 100644 --- a/Shared/Constants.cs +++ b/Shared/Constants.cs @@ -7,7 +7,7 @@ namespace Shared; public static class Constants { public const int MaxPacketSize = 256; - public const int MaxClients = 4; + public const int MaxClients = 10; public const int CostumeNameSize = 0x20; // dictionary of packet types to packet diff --git a/TestClient/Program.cs b/TestClient/Program.cs index 3c524ba..16ef433 100644 --- a/TestClient/Program.cs +++ b/TestClient/Program.cs @@ -13,8 +13,6 @@ 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; PacketType[] reboundPackets = { @@ -66,9 +64,10 @@ PacketHeader coolHeader = new PacketHeader { IMemoryOwner owner = MemoryPool.Shared.Rent(Constants.MaxPacketSize); MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader); ConnectPacket connect = new ConnectPacket { - ConnectionType = ConnectionTypes.FirstConnection + ConnectionType = ConnectionTypes.Reconnecting, + ClientName = "Test Sanae" }; -MemoryMarshal.Write(owner.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize], ref connect); +connect.Serialize(owner.Memory.Span[Constants.HeaderSize..Constants.MaxPacketSize]); await stream.WriteAsync(owner.Memory); logger.Info("Connected"); await S(); \ No newline at end of file