0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-25 04:35:18 +00:00

Logs cleaned up, and tested shine support

This commit is contained in:
Sanae 2022-02-15 14:26:50 -06:00
parent 70c9ad88cd
commit f0c3ae6963
5 changed files with 25 additions and 19 deletions

View file

@ -19,10 +19,16 @@ public class Client : IDisposable {
public Guid Id; public Guid Id;
public Socket? Socket; public Socket? Socket;
public Server Server { get; init; } 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() { public void Dispose() {
Socket?.Disconnect(false); if (Socket?.Connected is true)
Socket.Disconnect(false);
} }
public async Task Send<T>(T packet, Client? sender = null) where T : unmanaged, IPacket { public async Task Send<T>(T packet, Client? sender = null) where T : unmanaged, IPacket {

View file

@ -44,7 +44,7 @@ server.PacketHandler += async (c, p) => {
ConcurrentBag<int> playerBag = (ConcurrentBag<int>) c.Metadata["shineSync"]; ConcurrentBag<int> playerBag = (ConcurrentBag<int>) c.Metadata["shineSync"];
shineBag.Add(shinePacket.ShineId); shineBag.Add(shinePacket.ShineId);
if (playerBag.Contains(shinePacket.ShineId)) return; if (playerBag.Contains(shinePacket.ShineId)) return;
Console.WriteLine($"{c.Name} got {shinePacket.ShineId}"); c.Logger.Info($"Got shine {shinePacket.ShineId}");
playerBag.Add(shinePacket.ShineId); playerBag.Add(shinePacket.ShineId);
SyncShineBag(); SyncShineBag();
break; break;

View file

@ -26,7 +26,7 @@ public class Server {
while (true) { while (true) {
Socket socket = await serverSocket.AcceptAsync(); Socket socket = await serverSocket.AcceptAsync();
Logger.Warn("ok"); Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}");
try { try {
if (Clients.Count > Constants.MaxClients) { if (Clients.Count > Constants.MaxClients) {
@ -35,7 +35,7 @@ public class Server {
continue; continue;
} }
HandleSocket(socket); Task.Run(() => HandleSocket(socket));
} catch { } catch {
// super ignore this // super ignore this
} }
@ -86,7 +86,7 @@ public class Server {
private async void HandleSocket(Socket socket) { private async void HandleSocket(Socket socket) {
Client client = new Client { Socket = socket, Server = this }; Client client = new Client(socket) { Server = this };
IMemoryOwner<byte> memory = null!; IMemoryOwner<byte> memory = null!;
bool first = true; bool first = true;
try { try {
@ -96,7 +96,7 @@ public class Server {
if (size == 0) { if (size == 0) {
// treat it as a disconnect and exit // treat it as a disconnect and exit
Logger.Info($"Socket {socket.RemoteEndPoint} disconnected."); Logger.Info($"Socket {socket.RemoteEndPoint} disconnected.");
await socket.DisconnectAsync(false); if (socket.Connected) await socket.DisconnectAsync(false);
break; break;
} }
@ -159,7 +159,8 @@ public class Server {
}; };
MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader); MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader);
ConnectPacket connectPacket = new ConnectPacket { 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..]); connectPacket.Serialize(tempBuffer.Memory.Span[Constants.HeaderSize..]);
await client.Send(tempBuffer.Memory, null); await client.Send(tempBuffer.Memory, null);
@ -173,7 +174,7 @@ public class Server {
tempBuffer.Dispose(); 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) { if (header.Type == PacketType.Costume) {
@ -185,7 +186,7 @@ public class Server {
} }
try { 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])!; IPacket packet = (IPacket) Activator.CreateInstance(Constants.PacketIdMap[header.Type])!;
packet.Deserialize(memory.Memory.Span[Constants.HeaderSize..]); packet.Deserialize(memory.Memory.Span[Constants.HeaderSize..]);
PacketHandler?.Invoke(client, packet); PacketHandler?.Invoke(client, packet);
@ -193,14 +194,14 @@ public class Server {
// ignore failed packet deserialization! // ignore failed packet deserialization!
} }
await Broadcast(memory, client); Broadcast(memory, client);
} }
} catch (Exception e) { } catch (Exception e) {
if (e is SocketException { SocketErrorCode: SocketError.ConnectionReset }) { 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 { } else {
Logger.Error($"Exception on socket {socket.RemoteEndPoint} ({client.Id}) and disconnecting for: {e}"); client.Logger.Error($"Exception on socket {socket.RemoteEndPoint} ({client.Id}) and disconnecting for: {e}");
Task.Run(() => socket.DisconnectAsync(false)); if (socket.Connected) Task.Run(() => socket.DisconnectAsync(false));
} }
memory?.Dispose(); memory?.Dispose();

View file

@ -7,7 +7,7 @@ namespace Shared;
public static class Constants { public static class Constants {
public const int MaxPacketSize = 256; public const int MaxPacketSize = 256;
public const int MaxClients = 4; public const int MaxClients = 10;
public const int CostumeNameSize = 0x20; public const int CostumeNameSize = 0x20;
// dictionary of packet types to packet // dictionary of packet types to packet

View file

@ -13,8 +13,6 @@ Guid otherId = Guid.Parse("d5feae62-2e71-1000-88fd-597ea147ae88");
Logger logger = new Logger("Client"); Logger logger = new Logger("Client");
NetworkStream stream = client.GetStream(); NetworkStream stream = client.GetStream();
int e = 0;
double d = 0;
Vector3 basePoint = Vector3.Zero; Vector3 basePoint = Vector3.Zero;
PacketType[] reboundPackets = { PacketType[] reboundPackets = {
@ -66,9 +64,10 @@ PacketHeader coolHeader = new PacketHeader {
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize); IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(Constants.MaxPacketSize);
MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader); MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader);
ConnectPacket connect = new ConnectPacket { 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); await stream.WriteAsync(owner.Memory);
logger.Info("Connected"); logger.Info("Connected");
await S(); await S();