X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cdc59bb69e3a363d7bb6c3d97507faf918e1af30..bd1a676876fd11876afa661e0d0270912c9fc1bb:/src/common/sckipc.cpp diff --git a/src/common/sckipc.cpp b/src/common/sckipc.cpp index 28284b2695..3fea37e1ec 100644 --- a/src/common/sckipc.cpp +++ b/src/common/sckipc.cpp @@ -36,7 +36,7 @@ #include "wx/defs.h" #endif -#if wxUSE_SOCKETS && wxUSE_IPC +#if wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS #include #include @@ -191,7 +191,7 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host, wxConnectionBase *wxTCPClient::OnMakeConnection() { - return new wxTCPConnection; + return new wxTCPConnection(); } // -------------------------------------------------------------------------- @@ -200,28 +200,49 @@ wxConnectionBase *wxTCPClient::OnMakeConnection() wxTCPServer::wxTCPServer () : wxServerBase() { + m_server = NULL; } -bool wxTCPServer::Create(const wxString& server_name) +bool wxTCPServer::Create(const wxString& serverName) { - wxSocketServer *server; + // Destroy previous server, if any + if (m_server) + { + m_server->SetClientData(NULL); + m_server->Destroy(); + m_server = NULL; + } // wxIPV4address defaults to INADDR_ANY:0 wxIPV4address addr; - addr.Service(server_name); + addr.Service(serverName); + + // Create a socket listening on the specified port + m_server = new wxSocketServer(addr, SCKIPC_FLAGS); + + if (!m_server->Ok()) + { + m_server->Destroy(); + m_server = NULL; + + return FALSE; + } - // Create a socket listening on specified port - server = new wxSocketServer(addr, SCKIPC_FLAGS); - server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID); - server->SetClientData(this); - server->SetNotify(wxSOCKET_CONNECTION_FLAG); - server->Notify(TRUE); + m_server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID); + m_server->SetClientData(this); + m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); + m_server->Notify(TRUE); return TRUE; } wxTCPServer::~wxTCPServer() { + if (m_server) + { + m_server->SetClientData(NULL); + m_server->Destroy(); + } } wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) ) @@ -251,7 +272,11 @@ wxTCPConnection::~wxTCPConnection () wxDELETE(m_codeco); wxDELETE(m_sockstrm); - if (m_sock) m_sock->Destroy(); + if (m_sock) + { + m_sock->SetClientData(NULL); + m_sock->Destroy(); + } } void wxTCPConnection::Compress(bool WXUNUSED(on)) @@ -280,7 +305,7 @@ bool wxTCPConnection::Execute(const wxChar *data, int size, wxIPCFormat format) m_codeco->Write8(format); if (size < 0) - size = strlen(data) + 1; // includes final NUL + size = wxStrlen(data) + 1; // includes final NUL m_codeco->Write32(size); m_sockstrm->Write(data, size); @@ -328,7 +353,7 @@ bool wxTCPConnection::Poke (const wxString& item, wxChar *data, int size, wxIPCF m_codeco->Write8(format); if (size < 0) - size = strlen(data) + 1; // includes final NUL + size = wxStrlen(data) + 1; // includes final NUL m_codeco->Write32(size); m_sockstrm->Write(data, size); @@ -384,7 +409,7 @@ bool wxTCPConnection::Advise (const wxString& item, m_codeco->Write8(format); if (size < 0) - size = strlen(data) + 1; // includes final NUL + size = wxStrlen(data) + 1; // includes final NUL m_codeco->Write32(size); m_sockstrm->Write(data, size); @@ -405,7 +430,11 @@ void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event) { wxSocketBase *sock = event.GetSocket(); wxSocketNotify evt = event.GetSocketEvent(); - wxTCPConnection *connection = (wxTCPConnection *)(event.GetClientData()); + wxTCPConnection *connection = (wxTCPConnection *)(sock->GetClientData()); + + // This socket is being deleted; skip this event + if (!connection) + return; int msg = 0; wxDataInputStream *codeci; @@ -414,7 +443,7 @@ void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event) wxString topic_name = connection->m_topic; wxString item; - // The socket handler signals us that we lost the connection: destroy all. + // We lost the connection: destroy everything if (evt == wxSOCKET_LOST) { sock->Notify(FALSE); @@ -547,7 +576,11 @@ void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event) void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event) { wxSocketServer *server = (wxSocketServer *) event.GetSocket(); - wxTCPServer *ipcserv = (wxTCPServer *) event.GetClientData(); + wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData(); + + // This socket is being deleted; skip this event + if (!ipcserv) + return; if (event.GetSocketEvent() != wxSOCKET_CONNECTION) return;