Skip to content

Commit

Permalink
small refactorings
Browse files Browse the repository at this point in the history
- use brackets when possible
- set client.Id and client.Name earlier
- use client.Id instead of header.Id
- rename firstConn to isClientNew
  • Loading branch information
Istador authored and Sanae6 committed Apr 27, 2024
1 parent 61e6fcf commit dc20a9c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 42 deletions.
8 changes: 5 additions & 3 deletions Server/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public Client(Client other, Socket socket) {
}

public void Dispose() {
if (Socket?.Connected is true)
if (Socket?.Connected is true) {
Socket.Disconnect(false);
}
}


Expand All @@ -52,8 +53,8 @@ public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPa
PacketAttribute packetAttribute = Constants.PacketMap[typeof(T)];
try {
Server.FillPacket(new PacketHeader {
Id = sender?.Id ?? Id,
Type = packetAttribute.Type,
Id = sender?.Id ?? Id,
Type = packetAttribute.Type,
PacketSize = packet.Size
}, packet, memory.Memory);
}
Expand All @@ -69,6 +70,7 @@ public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPa
public async Task Send(Memory<byte> data, Client? sender) {
PacketHeader header = new PacketHeader();
header.Deserialize(data.Span);

if (!Connected && header.Type is not PacketType.Connect) {
Server.Logger.Error($"Didn't send {header.Type} to {Id} because they weren't connected yet");
return;
Expand Down
5 changes: 4 additions & 1 deletion Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ void logError(Task x) {
server.PacketHandler = (c, p) => {
switch (p) {
case GamePacket gamePacket: {
// crash player entering a banned stage
if (BanLists.Enabled && BanLists.IsStageBanned(gamePacket.Stage)) {
c.Logger.Warn($"Crashing player for entering banned stage {gamePacket.Stage}.");
BanLists.Crash(c, false, false, 500);
return false;
}

c.Logger.Info($"Got game packet {gamePacket.Stage}->{gamePacket.ScenarioNum}");

// reset lastPlayerPacket on stage changes
Expand Down Expand Up @@ -184,7 +186,7 @@ void logError(Task x) {
break;
}

case CostumePacket costumePacket:
case CostumePacket costumePacket: {
c.Logger.Info($"Got costume packet: {costumePacket.BodyName}, {costumePacket.CapName}");
c.Metadata["lastCostumePacket"] = costumePacket;
c.CurrentCostume = costumePacket;
Expand All @@ -193,6 +195,7 @@ void logError(Task x) {
#pragma warning restore CS4014
c.Metadata["loadedSave"] = true;
break;
}

case ShinePacket shinePacket: {
if (!Settings.Instance.Shines.Enabled) return false;
Expand Down
102 changes: 64 additions & 38 deletions Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public async Task Listen(CancellationToken? token = null) {
Socket socket = token.HasValue ? await serverSocket.AcceptAsync(token.Value) : await serverSocket.AcceptAsync();
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

// is the IPv4 address banned?
if (BanLists.Enabled && BanLists.IsIPv4Banned(((IPEndPoint) socket.RemoteEndPoint!).Address!)) {
Logger.Warn($"Ignoring banned IPv4 address {socket.RemoteEndPoint}");
continue;
}

Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}");

// start sub thread to handle client
try {
#pragma warning disable CS4014
Task.Run(() => HandleSocket(socket))
Expand Down Expand Up @@ -78,15 +80,17 @@ public static void FillPacket<T>(PacketHeader header, T packet, Memory<byte> mem
public delegate void PacketReplacer<in T>(Client from, Client to, T value); // replacer must send

public void BroadcastReplace<T>(T packet, Client sender, PacketReplacer<T> packetReplacer) where T : struct, IPacket {
foreach (Client client in Clients.Where(client => client.Connected && sender.Id != client.Id)) packetReplacer(sender, client, packet);
foreach (Client client in Clients.Where(client => client.Connected && sender.Id != client.Id)) {
packetReplacer(sender, client, packet);
}
}

public async Task Broadcast<T>(T packet, Client sender) where T : struct, IPacket {
IMemoryOwner<byte> memory = MemoryPool<byte>.Shared.RentZero(Constants.HeaderSize + packet.Size);
PacketHeader header = new PacketHeader {
Id = sender?.Id ?? Guid.Empty,
Type = Constants.PacketMap[typeof(T)].Type,
PacketSize = packet.Size
Id = sender?.Id ?? Guid.Empty,
Type = Constants.PacketMap[typeof(T)].Type,
PacketSize = packet.Size,
};
FillPacket(header, packet, memory.Memory);
await Broadcast(memory, sender);
Expand All @@ -96,9 +100,9 @@ public Task Broadcast<T>(T packet) where T : struct, IPacket {
return Task.WhenAll(Clients.Where(c => c.Connected).Select(async client => {
IMemoryOwner<byte> memory = MemoryPool<byte>.Shared.RentZero(Constants.HeaderSize + packet.Size);
PacketHeader header = new PacketHeader {
Id = client.Id,
Type = Constants.PacketMap[typeof(T)].Type,
PacketSize = packet.Size
Id = client.Id,
Type = Constants.PacketMap[typeof(T)].Type,
PacketSize = packet.Size,
};
FillPacket(header, packet, memory.Memory);
await client.Send(memory.Memory, client);
Expand Down Expand Up @@ -137,6 +141,7 @@ private async void HandleSocket(Socket socket) {
await client.Send(new InitPacket {
MaxPlayers = Settings.Instance.Server.MaxPlayers
});

bool first = true;
try {
while (true) {
Expand All @@ -159,99 +164,112 @@ async Task<bool> Read(Memory<byte> readMem, int readSize, int readOffset) {
return true;
}

if (!await Read(memory.Memory[..Constants.HeaderSize], Constants.HeaderSize, 0))
if (!await Read(memory.Memory[..Constants.HeaderSize], Constants.HeaderSize, 0)) {
break;
}
PacketHeader header = GetHeader(memory.Memory.Span[..Constants.HeaderSize]);
Range packetRange = Constants.HeaderSize..(Constants.HeaderSize + header.PacketSize);
if (header.PacketSize > 0) {
IMemoryOwner<byte> memTemp = memory; // header to copy to new memory
memory = memoryPool.Rent(Constants.HeaderSize + header.PacketSize);
memTemp.Memory.Span[..Constants.HeaderSize].CopyTo(memory.Memory.Span[..Constants.HeaderSize]);
memTemp.Dispose();
if (!await Read(memory.Memory, header.PacketSize, Constants.HeaderSize))
if (!await Read(memory.Memory, header.PacketSize, Constants.HeaderSize)) {
break;
}
}

if (client.Ignored) {
memory.Dispose();
continue;
}

// connection initialization
if (first) {
first = false;
if (header.Type != PacketType.Connect) throw new Exception($"First packet was not init, instead it was {header.Type}");
first = false; // only do this once

// first client packet has to be the client init
if (header.Type != PacketType.Connect) {
throw new Exception($"First packet was not init, instead it was {header.Type} ({remote})");
}

ConnectPacket connect = new ConnectPacket();
connect.Deserialize(memory.Memory.Span[packetRange]);

bool wasFirst = connect.ConnectionType == ConnectPacket.ConnectionTypes.FirstConnection;
client.Id = header.Id;
client.Name = connect.ClientName;

if (BanLists.Enabled && BanLists.IsProfileBanned(header.Id)) {
client.Id = header.Id;
client.Name = connect.ClientName;
// is the profile ID banned?
if (BanLists.Enabled && BanLists.IsProfileBanned(client.Id)) {
client.Ignored = true;
client.Logger.Warn($"Ignoring banned profile ID {header.Id}");
client.Logger.Warn($"Ignoring banned profile ID {client.Id}");
memory.Dispose();
continue;
}

bool wasFirst = connect.ConnectionType == ConnectPacket.ConnectionTypes.FirstConnection;

// add client to the set of connected players
lock (Clients) {
// is the server full?
if (Clients.Count(x => x.Connected) == Settings.Instance.Server.MaxPlayers) {
client.Logger.Error($"Turned away as server is at max clients");
memory.Dispose();
goto disconnect;
}

bool firstConn = true;
// detect and handle reconnections
bool isClientNew = true;
switch (connect.ConnectionType) {
case ConnectPacket.ConnectionTypes.FirstConnection:
case ConnectPacket.ConnectionTypes.Reconnecting: {
client.Id = header.Id;
if (FindExistingClient(header.Id) is { } oldClient) {
firstConn = false;
if (FindExistingClient(client.Id) is { } oldClient) {
isClientNew = false;
client = new Client(oldClient, socket);
client.Name = connect.ClientName;
Clients.Remove(oldClient);
Clients.Add(client);
if (oldClient.Connected) {
oldClient.Logger.Info($"Disconnecting already connected client {oldClient.Socket?.RemoteEndPoint} for {client.Socket?.RemoteEndPoint}");
oldClient.Dispose();
}
} else {
}
else {
connect.ConnectionType = ConnectPacket.ConnectionTypes.FirstConnection;
}

break;
}
default:
throw new Exception($"Invalid connection type {connect.ConnectionType}");
default: {
throw new Exception($"Invalid connection type {connect.ConnectionType} for {client.Name} ({client.Id}/{remote})");
}
}

client.Name = connect.ClientName;
client.Connected = true;
if (firstConn) {

if (isClientNew) {
// do any cleanup required when it comes to new clients
List<Client> toDisconnect = Clients.FindAll(c => c.Id == header.Id && c.Connected && c.Socket != null);
Clients.RemoveAll(c => c.Id == header.Id);
List<Client> toDisconnect = Clients.FindAll(c => c.Id == client.Id && c.Connected && c.Socket != null);
Clients.RemoveAll(c => c.Id == client.Id);

Clients.Add(client);

Parallel.ForEachAsync(toDisconnect, (c, token) => c.Socket!.DisconnectAsync(false, token));
// done disconnecting and removing stale clients with the same id

ClientJoined?.Invoke(client, connect);
// a new connection, not a reconnect, for an existing client
} else if (wasFirst) {
}
// a known client reconnects, but with a new first connection (e.g. after a restart)
else if (wasFirst) {
client.CleanMetadataOnNewConnection();
}
}

// for all other clients that are already connected
List<Client> otherConnectedPlayers = Clients.FindAll(c => c.Id != header.Id && c.Connected && c.Socket != null);
List<Client> otherConnectedPlayers = Clients.FindAll(c => c.Id != client.Id && c.Connected && c.Socket != null);
await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
IMemoryOwner<byte> tempBuffer = MemoryPool<byte>.Shared.RentZero(Constants.HeaderSize + (other.CurrentCostume.HasValue ? Math.Max(connect.Size, other.CurrentCostume.Value.Size) : connect.Size));

// make the other client known to the (new) client
// make the other client known to the new client
PacketHeader connectHeader = new PacketHeader {
Id = other.Id,
Type = PacketType.Connect,
Expand All @@ -266,7 +284,7 @@ await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
connectPacket.Serialize(tempBuffer.Memory.Span[Constants.HeaderSize..]);
await client.Send(tempBuffer.Memory[..(Constants.HeaderSize + connect.Size)], null);

// tell the (new) client what costume the other client has
// tell the new client what costume the other client has
if (other.CurrentCostume.HasValue) {
connectHeader.Type = PacketType.Costume;
connectHeader.PacketSize = other.CurrentCostume.Value.Size;
Expand All @@ -277,7 +295,7 @@ await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {

tempBuffer.Dispose();

// make the other client reset their puppet cache for this client, if it is a new connection (after restart)
// make the other client reset their puppet cache for this new client, if it is a new connection (after restart)
if (wasFirst) {
await SendEmptyPackets(client, other);
}
Expand All @@ -287,22 +305,29 @@ await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {

// send missing or outdated packets from others to the new client
await ResendPackets(client);
} else if (header.Id != client.Id && client.Id != Guid.Empty) {
}
else if (header.Id != client.Id && client.Id != Guid.Empty) {
throw new Exception($"Client {client.Name} sent packet with invalid client id {header.Id} instead of {client.Id}");
}

try {
// parse the packet
IPacket packet = (IPacket) Activator.CreateInstance(Constants.PacketIdMap[header.Type])!;
packet.Deserialize(memory.Memory.Span[Constants.HeaderSize..(Constants.HeaderSize + packet.Size)]);

// process the packet
if (PacketHandler?.Invoke(client, packet) is false) {
// don't broadcast the packet to everyone
memory.Dispose();
continue;
}
}
catch (Exception e) {
client.Logger.Error($"Packet handler warning: {e}");
}

#pragma warning disable CS4014
// broadcast the packet to everyone
Broadcast(memory, client)
.ContinueWith(x => { if (x.Exception != null) { Logger.Error(x.Exception.ToString()); } });
#pragma warning restore CS4014
Expand All @@ -311,7 +336,8 @@ await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
catch (Exception e) {
if (e is SocketException {SocketErrorCode: SocketError.ConnectionReset}) {
client.Logger.Info($"Disconnected from the server: Connection reset");
} else {
}
else {
client.Logger.Error($"Disconnecting due to exception: {e}");
if (socket.Connected) {
#pragma warning disable CS4014
Expand All @@ -324,6 +350,7 @@ await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
memory?.Dispose();
}

// client disconnected
disconnect:
if (client.Name != "Unknown User" && client.Id != Guid.Parse("00000000-0000-0000-0000-000000000000")) {
Logger.Info($"Client {remote} ({client.Name}/{client.Id}) disconnected from the server");
Expand All @@ -333,7 +360,6 @@ await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
}

bool wasConnected = client.Connected;
// Clients.Remove(client)
client.Connected = false;
try {
client.Dispose();
Expand All @@ -359,7 +385,7 @@ async Task trySendPack<T>(Client other, T? packet) where T : struct, IPacket {
}
};
async Task trySendMeta<T>(Client other, string packetType) where T : struct, IPacket {
if (! other.Metadata.ContainsKey(packetType)) { return; }
if (!other.Metadata.ContainsKey(packetType)) { return; }
await trySendPack<T>(other, (T) other.Metadata[packetType]!);
};
await Parallel.ForEachAsync(this.ClientsConnected, async (other, _) => {
Expand Down

0 comments on commit dc20a9c

Please sign in to comment.