mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-21 18:55:17 +00:00
Logs cleaned up, and tested shine support
This commit is contained in:
parent
70c9ad88cd
commit
f0c3ae6963
5 changed files with 25 additions and 19 deletions
|
@ -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>(T packet, Client? sender = null) where T : unmanaged, IPacket {
|
||||
|
|
|
@ -44,7 +44,7 @@ server.PacketHandler += async (c, p) => {
|
|||
ConcurrentBag<int> playerBag = (ConcurrentBag<int>) 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;
|
||||
|
|
|
@ -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<byte> 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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<byte> owner = MemoryPool<byte>.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();
|
Loading…
Reference in a new issue