void CServerConsole::OnSocketEvent(wxSocketEvent& event){ wxSocketBase *sock = event.GetSocket(); switch(event.GetSocketEvent()){ case wxSOCKET_LOST:{ Log(wxString::FromAscii("Client disconnected")); sock->Destroy(); break; } case wxSOCKET_INPUT:{ // Disable input events, not to trigger wxSocketEvent again sock->SetNotify(wxSOCKET_LOST_FLAG); ClientHandshake handshake; sock->ReadMsg(&handshake, sizeof(handshake)); if (sock->Error()){ Log(wxString::FromAscii("Error receiving client message")); sock->Close(); return; } Log(wxString::FromAscii("Got a handshake")); int iClientID = handshake.id; if ((iClientID == -1) || ((iClientID > 0) && !IsClientIDValid(iClientID))){ iClientID = CreateNewClient(iClientID); } ServerReply reply; reply.id = iClientID; reply.status = 1; if (!IsClientIDValid(iClientID)) reply.status = -1; sock->WriteMsg(&reply, sizeof(reply)); if (sock->Error()){ Log(wxString::FromAscii("Error sending a reply")); return; } Log(wxString::FromAscii("Sent a reply")); if (!IsClientIDValid(iClientID)) return; ReceiveClientCommand(iClientID, sock); // Enable input events again. sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG); break; } default: wxASSERT(0); } }
void CClientMgr::RemovePendingAccept(CClient* pClient, sockaddr_in *RemoteSockaddr) { if (NULL != pClient) { EnterCriticalSection(&m_csPendingSection); m_oPendingAcceptClientList.remove(pClient); LeaveCriticalSection(&m_csPendingSection); InterlockedDecrement(&m_lPendingAcceptCount); CreateNewClient( pClient, RemoteSockaddr); } }