private void AcceptClient(IAsyncResult ar) { bool restarted = false; try { Socket tempClient = _clientsListener.EndAccept(ar); _clientsListener.BeginAccept(new AsyncCallback(AcceptClient), null); restarted = true; // do not touch if (servers.Count == 0) { PlayerClient.SendClient_AlertMsg(tempClient, "Server is down, try again in a minute!", true); IPEndPoint clientInfo = (IPEndPoint)tempClient.RemoteEndPoint; RejectConnection(clientInfo.Address.ToString() + " is trying to login, but there's no available servers for him to join.", tempClient); } else if (clients.Count < Constants.MAX_CLIENTS_AT_ONCE) { Candidate c = new Candidate(); clients.Add(c); c.socket = tempClient; c.buffer = new byte[c.socket.ReceiveBufferSize]; c.id = clientsCustomID; c.connectionTime = DateTime.Now.Ticks; clientsCustomID++; c.socket.BeginReceive(c.buffer, 0, c.buffer.Length, SocketFlags.None, new AsyncCallback(OnClientReceive), c); } else RejectConnection("Rejecting Client -> too many clients attempting to log in...(Limit set to " + Constants.MAX_CLIENTS_AT_ONCE + ")", tempClient);// TODO: Make a queue and send it to client? } catch (Exception ex) { if (!restarted) _clientsListener.BeginAccept(new AsyncCallback(AcceptClient), null); appendLog("Exception in: Client Listener - AcceptClient() ->" + ex.Message); //MessageBox.Show(ex.Message, "Client Listener - AcceptClient()", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void RemoveClient(Candidate c) { if (c.socket != null && SocketConnected(c.socket)) { c.socket.Shutdown(SocketShutdown.Both); c.socket.Close(); } clients.Remove(c); }