#include "wx/defs.h"
#endif
-#if wxUSE_SOCKETS && wxUSE_IPC
+#if wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS
#include <stdlib.h>
#include <stdio.h>
wxConnectionBase *wxTCPClient::OnMakeConnection()
{
- return new wxTCPConnection;
+ return new wxTCPConnection();
}
// --------------------------------------------------------------------------
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) )
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))
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);
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);
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);
{
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;
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);
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;