Use correct maxplayers field

This commit is contained in:
Sanae 2022-03-22 03:20:49 -06:00
parent 8645db12fa
commit 50adf23be3
1 changed files with 14 additions and 6 deletions

View File

@ -45,9 +45,11 @@ public class Server {
try { try {
serverSocket.Shutdown(SocketShutdown.Both); serverSocket.Shutdown(SocketShutdown.Both);
} catch { }
catch {
// ignored // ignored
} finally { }
finally {
serverSocket.Close(); serverSocket.Close();
} }
@ -125,7 +127,7 @@ public class Server {
try { try {
while (true) { while (true) {
memory = memoryPool.Rent(Constants.HeaderSize); memory = memoryPool.Rent(Constants.HeaderSize);
async Task<bool> Read(Memory<byte> readMem, int readSize, int readOffset) { async Task<bool> Read(Memory<byte> readMem, int readSize, int readOffset) {
readSize += readOffset; readSize += readOffset;
while (readOffset < readSize) { while (readOffset < readSize) {
@ -167,11 +169,12 @@ public class Server {
connect.Deserialize(memory.Memory.Span[packetRange]); connect.Deserialize(memory.Memory.Span[packetRange]);
lock (Clients) { lock (Clients) {
client.Name = connect.ClientName; client.Name = connect.ClientName;
if (Clients.Count(x => x.Connected) == Constants.MaxClients) { if (Clients.Count(x => x.Connected) == Settings.Instance.Server.MaxPlayers) {
client.Logger.Error($"Turned away as server is at max clients"); client.Logger.Error($"Turned away as server is at max clients");
memory.Dispose(); memory.Dispose();
goto disconnect; goto disconnect;
} }
bool firstConn = false; bool firstConn = false;
switch (connect.ConnectionType) { switch (connect.ConnectionType) {
case ConnectPacket.ConnectionTypes.FirstConnection: { case ConnectPacket.ConnectionTypes.FirstConnection: {
@ -181,6 +184,7 @@ public class Server {
newClient.Socket = client.Socket; newClient.Socket = client.Socket;
client = newClient; client = newClient;
} }
break; break;
} }
case ConnectPacket.ConnectionTypes.Reconnecting: { case ConnectPacket.ConnectionTypes.Reconnecting: {
@ -281,6 +285,7 @@ public class Server {
memory?.Dispose(); memory?.Dispose();
} }
disconnect: disconnect:
Logger.Info($"Client {socket.RemoteEndPoint} ({client.Name}/{client.Id}) disconnected from the server"); Logger.Info($"Client {socket.RemoteEndPoint} ({client.Name}/{client.Id}) disconnected from the server");
@ -288,7 +293,10 @@ public class Server {
client.Connected = false; client.Connected = false;
try { try {
client.Dispose(); client.Dispose();
} catch {/*lol*/} }
catch { /*lol*/
}
Task.Run(() => Broadcast(new DisconnectPacket(), client)); Task.Run(() => Broadcast(new DisconnectPacket(), client));
} }
@ -296,4 +304,4 @@ public class Server {
//no need to error check, the client will disconnect when the packet is invalid :) //no need to error check, the client will disconnect when the packet is invalid :)
return MemoryMarshal.Read<PacketHeader>(data); return MemoryMarshal.Read<PacketHeader>(data);
} }
} }