void SocketEvents::OnSocketEvent(wxSocketEvent& event) { Socket* sock = (Socket*)event.GetClientData(); if (sock == NULL) { m_net_class.OnError(_T("sock = 0")); return; } if ( event.GetSocketEvent() == wxSOCKET_INPUT ) { m_net_class.OnDataReceived( *sock ); } else if ( event.GetSocketEvent() == wxSOCKET_LOST ) { m_net_class.OnDisconnected( *sock ); } else if ( event.GetSocketEvent() == wxSOCKET_CONNECTION ) { m_net_class.OnConnected( *sock ); } else { m_net_class.OnError(_T("Unknown socket event.")); } }
void SocketEvents::OnSocketEvent(wxSocketEvent& event) { Socket* sock = (Socket*)event.GetClientData(); try { ASSERT_LOGIC( sock != 0, _T("sock = 0") ); } catch (...) { return; } if ( event.GetSocketEvent() == wxSOCKET_INPUT ) { m_net_class.OnDataReceived( sock ); } else if ( event.GetSocketEvent() == wxSOCKET_LOST ) { m_net_class.OnDisconnected( sock ); } else if ( event.GetSocketEvent() == wxSOCKET_CONNECTION ) { m_net_class.OnConnected( sock ); } else { try { ASSERT_LOGIC( false, _T("Unknown socket event.")); } catch (...) { return; }; } }
void RMENet::HandleEvent(wxSocketEvent& evt) { NetworkConnection* connection = reinterpret_cast<NetworkConnection*>(evt.GetClientData()); switch(evt.GetSocketEvent()) { case wxSOCKET_LOST: // Connection was lost, either client disconnecting, or our socket breaking { // Generate event wxCommandEvent event(EVT_RMENET_CONNECTION_LOST); event.SetInt(0); event.SetString("GOOOFU"); event_dump->AddPendingEvent(event); } break; case wxSOCKET_OUTPUT: // We're ready to write to a socket { // Send waiting data connection->Send(); } break; case wxSOCKET_INPUT: // We got some data to be read. { NetworkMessage* nmsg = connection->Receive(); if(nmsg) { try { OnParsePacket(nmsg); FreeMessage(nmsg); } catch(std::runtime_error&) { FreeMessage(nmsg); Close(); } } } break; } }
void LiveServer::HandleEvent(wxSocketEvent& evt) { LivePeer* connection = reinterpret_cast<LivePeer*>(evt.GetClientData()); try { switch(evt.GetSocketEvent()) { case wxSOCKET_CONNECTION: // A client tries to connect! { wxSocketBase* client = serv->Accept(false); if(!client) { log->Message(wxT("Network error: Could not accept incoming connection.")); } LivePeer* connection = newd LivePeer(this, client); connection->parser = &LiveServer::OnParseLoginPackets; connecting_clients.push_back(connection); client->SetClientData(connection); client->SetEventHandler(*this, wxID_ANY); client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG | wxSOCKET_CONNECTION_FLAG | wxSOCKET_LOST_FLAG); client->Notify(true); } break; case wxSOCKET_LOST: // Connection was lost, either client disconnecting, or our socket breaking { if(connection->GetClientID() != 0) { client_mask &= ~(1 << connection->GetClientID()); editor->map.clearVisible(client_mask); } bool client = false; PeerList::iterator iter = connected_clients.begin(); while(iter != connected_clients.end()) { if(*iter == connection) { client = true; connected_clients.erase(iter); break; } else ++iter; } if(client) { // It was a client disconnecting. wxString str; str << wxT("Lost remote connection ") << connection->GetNick() << wxT(" (") << connection->GetHost() << wxT(")"); log->Message(str); log->UpdateClientList(connected_clients); delete connection; } else { // Our socket broke! log->Message(wxT("Network connection was interrupted. Can no longer continue to host.")); delete connection; Close(); } } break; case wxSOCKET_OUTPUT: // We're ready to write to a socket { //log->Message(wxT("Socket is writeable!")); if(connection) connection->Send(); } break; case wxSOCKET_INPUT: // We got some data to be read. { NetworkMessage* nmsg = connection->Receive(); if(nmsg) { ((this)->*(connection->parser))(connection, nmsg); FreeMessage(nmsg); } } break; } } catch(std::runtime_error& err) { connection->Close(); log->Message(wxT("Invalid packet received, the reported error was: ") + wxString(err.what(), wxConvUTF8)); } }