// Synchroniously communicates with client which is attempting to connect // first checks the handshake -> if its incorrect, sends out ERROR message and quits the client connection // second checks if there is room on the server -> if not, sends out ERROR message and disconnects the client private void AcceptClient(Socket s) { ClientHandler handler; lock (clientLock) { long currentNo = counter++; handler = new ClientHandler(s, currentNo, this, queue); // Try the handshake bool success = handler.Handshake(); if (!success) { handler.Close(); return; } // Check whether we accept players if (State != ServerState.READY) { handler.Send("ERROR The game is already running!"); handler.Close(); return; } // Check the player count if (clients.Count >= maxPlayers) { handler.Send("ERROR Server is full"); handler.Close(); return; } // Check the player name if (NameExists(handler.Name)) { handler.Send("ERROR Name is already in use on the server"); handler.Close(); return; } handler.Send("ACK " + currentNo); SendAll("CONNECTED " + handler.Id + " " + handler.Name); clients.Add(handler); handler.Send("CLIST " + GetClients(false)); handler.Send("RLIST " + GetClients(true)); } // Starts receiving information from the client handler.Init(); }
// Kicks the client with specified name public void Kick(string name) { ClientHandler target = null; foreach (ClientHandler ch in clients) { if (ch.Name == name) { target = ch; break; } } if (target != null) { Console.WriteLine("Player kicked: {0}", name); target.Send("KICKED"); target.Close(); } }