private void HandleIncomingConnection(IAsyncResult ar) { try { threadFinished.Set(); Socket handler = ((Socket)ar.AsyncState).EndAccept(ar); ServerConnectionStateObject clientState = new ServerConnectionStateObject() { workSocket = handler }; IPEndPoint remoteIP = (IPEndPoint)handler.RemoteEndPoint; clientState.endpointIP = remoteIP.Address.ToString(); clientState.endpointPort = remoteIP.Port.ToString(); if (Configuration.SpamProtection.SPAM_PROTECTION_ENABLED) { if (!AntispamProtection.CheckUser(clientState.endpointIP)) { return; } } Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Handling incoming connection request", ErrorSeverity.ERROR_INFO); if (ServerManager.ClientCanConnect()) { ServerManager.ConnectClient(); ReadAsyncDelayed(ar, clientState); } else { Debug.Logging.errlog(Utils.connectionInfo(clientState) + "CPU full: offloading incoming request to queue", ErrorSeverity.ERROR_WARNING); ServerManager.queuedClients.Enqueue(new DelayedQueueConnection(ar, clientState)); } } catch (Exception e) { Debug.Logging.errlog("Failed to connect client:\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING); } }
private void BeginManager() { while (true) { if (!ServerManager.ClientCanConnect() && ServerManager.GetOpenSlots() > 0) { Logging.errlog("Releasing clients from connection queue", ErrorSeverity.ERROR_INFO); for (int i = 1; i <= ServerManager.GetOpenSlots(); i++) { if (ServerManager.queuedClients.Count > 0) { DelayedQueueConnection connectionObject = ServerManager.queuedClients.Dequeue(); ServerManager.ConnectClient(); _socketManager.ReadAsyncDelayed(connectionObject.ar, connectionObject.clientState); } } } Thread.Sleep(250); } }