diff --git a/include/wx/sckipc.h b/include/wx/sckipc.h index 4d4164cd8c..205366d40b 100644 --- a/include/wx/sckipc.h +++ b/include/wx/sckipc.h @@ -55,87 +55,94 @@ class WXDLLIMPEXP_FWD_NET wxTCPClient; class WXDLLIMPEXP_NET wxTCPConnection : public wxConnectionBase { public: - wxTCPConnection(void *buffer, size_t size); - wxTCPConnection(); - virtual ~wxTCPConnection(); + wxTCPConnection() { Init(); } + wxTCPConnection(void *buffer, size_t size) + : wxConnectionBase(buffer, size) + { + Init(); + } - // To enable the compressor (NOTE: not implemented!) - void Compress(bool on); + virtual ~wxTCPConnection(); + // implement base class pure virtual methods + virtual const void *Request(const wxString& item, + size_t *size = NULL, + wxIPCFormat format = wxIPC_TEXT); + virtual bool StartAdvise(const wxString& item); + virtual bool StopAdvise(const wxString& item); + virtual bool Disconnect(void); + + // Will be used in the future to enable the compression but does nothing + // for now. + void Compress(bool on); - // implement base class pure virtual methods - virtual const void *Request(const wxString& item, - size_t *size = NULL, - wxIPCFormat format = wxIPC_TEXT); - virtual bool StartAdvise(const wxString& item); - virtual bool StopAdvise(const wxString& item); - virtual bool Disconnect(void); protected: - virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format); - virtual bool DoPoke(const wxString& item, const void *data, size_t size, - wxIPCFormat format); - virtual bool DoAdvise(const wxString& item, const void *data, size_t size, + virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format); + virtual bool DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format); + virtual bool DoAdvise(const wxString& item, const void *data, size_t size, + wxIPCFormat format); - wxSocketBase *m_sock; - wxSocketStream *m_sockstrm; - wxDataInputStream *m_codeci; - wxDataOutputStream *m_codeco; - wxString m_topic; + wxSocketBase *m_sock; + wxSocketStream *m_sockstrm; + wxDataInputStream *m_codeci; + wxDataOutputStream *m_codeco; + wxString m_topic; - friend class wxTCPServer; - friend class wxTCPClient; - friend class wxTCPEventHandler; +private: + // common part of both ctors + void Init(); - DECLARE_NO_COPY_CLASS(wxTCPConnection) - DECLARE_DYNAMIC_CLASS(wxTCPConnection) + friend class wxTCPServer; + friend class wxTCPClient; + friend class wxTCPEventHandler; + + DECLARE_NO_COPY_CLASS(wxTCPConnection) + DECLARE_DYNAMIC_CLASS(wxTCPConnection) }; class WXDLLIMPEXP_NET wxTCPServer : public wxServerBase { public: - wxTCPServer(); - virtual ~wxTCPServer(); + wxTCPServer(); + virtual ~wxTCPServer(); - // Returns false on error (e.g. port number is already in use) - virtual bool Create(const wxString& serverName); + // Returns false on error (e.g. port number is already in use) + virtual bool Create(const wxString& serverName); - virtual wxConnectionBase *OnAcceptConnection(const wxString& topic); - - wxTCPConnection *topLevelConnection; + virtual wxConnectionBase *OnAcceptConnection(const wxString& topic); protected: - wxSocketServer *m_server; + wxSocketServer *m_server; #ifdef __UNIX_LIKE__ - // the name of the file associated to the Unix domain socket, may be empty - wxString m_filename; + // the name of the file associated to the Unix domain socket, may be empty + wxString m_filename; #endif // __UNIX_LIKE__ - DECLARE_NO_COPY_CLASS(wxTCPServer) - DECLARE_DYNAMIC_CLASS(wxTCPServer) + DECLARE_NO_COPY_CLASS(wxTCPServer) + DECLARE_DYNAMIC_CLASS(wxTCPServer) }; class WXDLLIMPEXP_NET wxTCPClient : public wxClientBase { public: - wxTCPClient(); - virtual ~wxTCPClient(); + wxTCPClient(); - virtual bool ValidHost(const wxString& host); + virtual bool ValidHost(const wxString& host); - // Call this to make a connection. Returns NULL if cannot. - virtual wxConnectionBase *MakeConnection(const wxString& host, - const wxString& server, - const wxString& topic); + // Call this to make a connection. Returns NULL if cannot. + virtual wxConnectionBase *MakeConnection(const wxString& host, + const wxString& server, + const wxString& topic); - // Callbacks to CLIENT - override at will - virtual wxConnectionBase *OnMakeConnection(); + // Callbacks to CLIENT - override at will + virtual wxConnectionBase *OnMakeConnection(); private: - DECLARE_DYNAMIC_CLASS(wxTCPClient) + DECLARE_DYNAMIC_CLASS(wxTCPClient) }; #endif // wxUSE_SOCKETS && wxUSE_IPC diff --git a/src/common/sckipc.cpp b/src/common/sckipc.cpp index 9fdd8343ef..1d43462ec2 100644 --- a/src/common/sckipc.cpp +++ b/src/common/sckipc.cpp @@ -49,24 +49,21 @@ // macros and constants // -------------------------------------------------------------------------- -// It seems to be already defined somewhere in the Xt includes. -#ifndef __XT__ // Message codes enum { - IPC_EXECUTE = 1, - IPC_REQUEST, - IPC_POKE, - IPC_ADVISE_START, - IPC_ADVISE_REQUEST, - IPC_ADVISE, - IPC_ADVISE_STOP, - IPC_REQUEST_REPLY, - IPC_FAIL, - IPC_CONNECT, - IPC_DISCONNECT + IPC_EXECUTE = 1, + IPC_REQUEST, + IPC_POKE, + IPC_ADVISE_START, + IPC_ADVISE_REQUEST, + IPC_ADVISE, + IPC_ADVISE_STOP, + IPC_REQUEST_REPLY, + IPC_FAIL, + IPC_CONNECT, + IPC_DISCONNECT }; -#endif // All sockets will be created with the following flags #define SCKIPC_FLAGS (wxSOCKET_WAITALL|wxSOCKET_REUSEADDR) @@ -83,7 +80,8 @@ enum // get the address object for the given server name, the caller must delete it static wxSockAddress * -GetAddressFromName(const wxString& serverName, const wxString& host = wxEmptyString) +GetAddressFromName(const wxString& serverName, + const wxString& host = wxEmptyString) { // we always use INET sockets under non-Unix systems #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) @@ -116,19 +114,19 @@ GetAddressFromName(const wxString& serverName, const wxString& host = wxEmptyStr class wxTCPEventHandler : public wxEvtHandler { public: - wxTCPEventHandler() : wxEvtHandler() {} + wxTCPEventHandler() : wxEvtHandler() {} - void Client_OnRequest(wxSocketEvent& event); - void Server_OnRequest(wxSocketEvent& event); + void Client_OnRequest(wxSocketEvent& event); + void Server_OnRequest(wxSocketEvent& event); - DECLARE_EVENT_TABLE() - DECLARE_NO_COPY_CLASS(wxTCPEventHandler) + DECLARE_EVENT_TABLE() + DECLARE_NO_COPY_CLASS(wxTCPEventHandler) }; enum { - _CLIENT_ONREQUEST_ID = 1000, - _SERVER_ONREQUEST_ID + _CLIENT_ONREQUEST_ID = 1000, + _SERVER_ONREQUEST_ID }; static wxTCPEventHandler *gs_handler = NULL; @@ -145,172 +143,171 @@ IMPLEMENT_CLASS(wxTCPConnection, wxConnectionBase) // wxTCPClient // -------------------------------------------------------------------------- -wxTCPClient::wxTCPClient () : wxClientBase() -{ -} - -wxTCPClient::~wxTCPClient () +wxTCPClient::wxTCPClient() + : wxClientBase() { } bool wxTCPClient::ValidHost(const wxString& host) { - wxIPV4address addr; + wxIPV4address addr; - return addr.Hostname(host); + return addr.Hostname(host); } wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host, const wxString& serverName, const wxString& topic) { - wxSockAddress *addr = GetAddressFromName(serverName, host); - if ( !addr ) - return NULL; + wxSockAddress *addr = GetAddressFromName(serverName, host); + if ( !addr ) + return NULL; - wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS); - wxSocketStream *stream = new wxSocketStream(*client); - wxDataInputStream *data_is = new wxDataInputStream(*stream); - wxDataOutputStream *data_os = new wxDataOutputStream(*stream); + wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS); + wxSocketStream *stream = new wxSocketStream(*client); + wxDataInputStream *data_is = new wxDataInputStream(*stream); + wxDataOutputStream *data_os = new wxDataOutputStream(*stream); - bool ok = client->Connect(*addr); - delete addr; + bool ok = client->Connect(*addr); + delete addr; - if ( ok ) - { - unsigned char msg; - - // Send topic name, and enquire whether this has succeeded - data_os->Write8(IPC_CONNECT); - data_os->WriteString(topic); - - msg = data_is->Read8(); - - // OK! Confirmation. - if (msg == IPC_CONNECT) + if ( ok ) { - wxTCPConnection *connection = (wxTCPConnection *)OnMakeConnection (); + unsigned char msg; - if (connection) - { - if (connection->IsKindOf(CLASSINFO(wxTCPConnection))) + // Send topic name, and enquire whether this has succeeded + data_os->Write8(IPC_CONNECT); + data_os->WriteString(topic); + + msg = data_is->Read8(); + + // OK! Confirmation. + if (msg == IPC_CONNECT) { - connection->m_topic = topic; - connection->m_sock = client; - connection->m_sockstrm = stream; - connection->m_codeci = data_is; - connection->m_codeco = data_os; - client->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); - client->SetClientData(connection); - client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); - client->Notify(true); - return connection; + wxTCPConnection * + connection = (wxTCPConnection *)OnMakeConnection (); + + if (connection) + { + if (connection->IsKindOf(CLASSINFO(wxTCPConnection))) + { + connection->m_topic = topic; + connection->m_sock = client; + connection->m_sockstrm = stream; + connection->m_codeci = data_is; + connection->m_codeco = data_os; + client->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); + client->SetClientData(connection); + client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); + client->Notify(true); + return connection; + } + else + { + delete connection; + // and fall through to delete everything else + } + } } - else - { - delete connection; - // and fall through to delete everything else - } - } } - } - // Something went wrong, delete everything - delete data_is; - delete data_os; - delete stream; - client->Destroy(); + // Something went wrong, delete everything + delete data_is; + delete data_os; + delete stream; + client->Destroy(); - return NULL; + return NULL; } wxConnectionBase *wxTCPClient::OnMakeConnection() { - return new wxTCPConnection(); + return new wxTCPConnection(); } // -------------------------------------------------------------------------- // wxTCPServer // -------------------------------------------------------------------------- -wxTCPServer::wxTCPServer () : wxServerBase() +wxTCPServer::wxTCPServer() + : wxServerBase() { - m_server = NULL; + m_server = NULL; } bool wxTCPServer::Create(const wxString& serverName) { - // Destroy previous server, if any - if (m_server) - { - m_server->SetClientData(NULL); - m_server->Destroy(); - m_server = NULL; - } + // Destroy previous server, if any + if (m_server) + { + m_server->SetClientData(NULL); + m_server->Destroy(); + m_server = NULL; + } - wxSockAddress *addr = GetAddressFromName(serverName); - if ( !addr ) - return false; + wxSockAddress *addr = GetAddressFromName(serverName); + if ( !addr ) + return false; #ifdef __UNIX_LIKE__ - mode_t umaskOld; - if ( addr->Type() == wxSockAddress::UNIX ) - { - // ensure that the file doesn't exist as otherwise calling socket() would - // fail - int rc = remove(serverName.fn_str()); - if ( rc < 0 && errno != ENOENT ) - { - delete addr; + mode_t umaskOld; + if ( addr->Type() == wxSockAddress::UNIX ) + { + // ensure that the file doesn't exist as otherwise calling socket() + // would fail + int rc = remove(serverName.fn_str()); + if ( rc < 0 && errno != ENOENT ) + { + delete addr; - return false; - } + return false; + } - // also set the umask to prevent the others from reading our file - umaskOld = umask(077); - } - else - { - // unused anyhow but shut down the compiler warnings - umaskOld = 0; - } + // also set the umask to prevent the others from reading our file + umaskOld = umask(077); + } + else + { + // unused anyhow but shut down the compiler warnings + umaskOld = 0; + } #endif // __UNIX_LIKE__ - // Create a socket listening on the specified port - m_server = new wxSocketServer(*addr, SCKIPC_FLAGS); + // Create a socket listening on the specified port + m_server = new wxSocketServer(*addr, SCKIPC_FLAGS); #ifdef __UNIX_LIKE__ - if ( addr->Type() == wxSockAddress::UNIX ) - { - // restore the umask - umask(umaskOld); + if ( addr->Type() == wxSockAddress::UNIX ) + { + // restore the umask + umask(umaskOld); - // save the file name to remove it later - m_filename = serverName; - } + // save the file name to remove it later + m_filename = serverName; + } #endif // __UNIX_LIKE__ - delete addr; + delete addr; - if (!m_server->Ok()) - { - m_server->Destroy(); - m_server = NULL; + if (!m_server->Ok()) + { + m_server->Destroy(); + m_server = NULL; - return false; - } + return false; + } - m_server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID); - m_server->SetClientData(this); - m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); - m_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; + return true; } wxTCPServer::~wxTCPServer() { - if (m_server) + if ( m_server ) { m_server->SetClientData(NULL); m_server->Destroy(); @@ -327,183 +324,175 @@ wxTCPServer::~wxTCPServer() #endif // __UNIX_LIKE__ } -wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) ) +wxConnectionBase * +wxTCPServer::OnAcceptConnection(const wxString& WXUNUSED(topic)) { - return new wxTCPConnection(); + return new wxTCPConnection(); } // -------------------------------------------------------------------------- // wxTCPConnection // -------------------------------------------------------------------------- -wxTCPConnection::wxTCPConnection () : wxConnectionBase() +void wxTCPConnection::Init() { - m_sock = NULL; - m_sockstrm = NULL; - m_codeci = NULL; - m_codeco = NULL; + m_sock = NULL; + m_sockstrm = NULL; + m_codeci = NULL; + m_codeco = NULL; } -wxTCPConnection::wxTCPConnection(void *buffer, size_t size) - : wxConnectionBase(buffer, size) +wxTCPConnection::~wxTCPConnection() { - m_sock = NULL; - m_sockstrm = NULL; - m_codeci = NULL; - m_codeco = NULL; -} + Disconnect(); -wxTCPConnection::~wxTCPConnection () -{ - Disconnect(); + if ( m_sock ) + { + m_sock->SetClientData(NULL); + m_sock->Destroy(); + } - if (m_sock) - { - m_sock->SetClientData(NULL); - m_sock->Destroy(); - } - - /* Delete after destroy */ - wxDELETE(m_codeci); - wxDELETE(m_codeco); - wxDELETE(m_sockstrm); + /* Delete after destroy */ + wxDELETE(m_codeci); + wxDELETE(m_codeco); + wxDELETE(m_sockstrm); } void wxTCPConnection::Compress(bool WXUNUSED(on)) { - // Use wxLZWStream + // TODO } // Calls that CLIENT can make. -bool wxTCPConnection::Disconnect () +bool wxTCPConnection::Disconnect() { - if ( !GetConnected() ) - return true; - // Send the the disconnect message to the peer. - m_codeco->Write8(IPC_DISCONNECT); + if ( !GetConnected() ) + return true; - if ( m_sock ) - { - m_sock->Notify(false); - m_sock->Close(); - } + // Send the disconnect message to the peer. + m_codeco->Write8(IPC_DISCONNECT); - SetConnected(false); + if ( m_sock ) + { + m_sock->Notify(false); + m_sock->Close(); + } - return true; + SetConnected(false); + + return true; } -bool wxTCPConnection::DoExecute(const void *data, size_t size, wxIPCFormat format) +bool wxTCPConnection::DoExecute(const void *data, + size_t size, + wxIPCFormat format) { - if (!m_sock->IsConnected()) - return false; + if ( !m_sock->IsConnected() ) + return false; - // Prepare EXECUTE message - m_codeco->Write8(IPC_EXECUTE); - m_codeco->Write8(format); + // Prepare EXECUTE message + m_codeco->Write8(IPC_EXECUTE); + m_codeco->Write8(format); - m_codeco->Write32(size); - m_sockstrm->Write(data, size); + m_codeco->Write32(size); + m_sockstrm->Write(data, size); - return true; + return true; } -const void *wxTCPConnection::Request (const wxString& item, size_t *size, wxIPCFormat format) +const void *wxTCPConnection::Request(const wxString& item, + size_t *size, + wxIPCFormat format) { - if (!m_sock->IsConnected()) - return NULL; + if ( !m_sock->IsConnected() ) + return NULL; - m_codeco->Write8(IPC_REQUEST); - m_codeco->WriteString(item); - m_codeco->Write8(format); + m_codeco->Write8(IPC_REQUEST); + m_codeco->WriteString(item); + m_codeco->Write8(format); - // If Unpack doesn't initialize it. - int ret; + int ret = m_codeci->Read8(); + if ( ret == IPC_FAIL ) + return NULL; - ret = m_codeci->Read8(); - if (ret == IPC_FAIL) - return NULL; - else - { size_t s = m_codeci->Read32(); void *data = GetBufferAtLeast( s ); wxASSERT_MSG(data != NULL, - _T("Buffer too small in wxTCPConnection::Request") ); + _T("Buffer too small in wxTCPConnection::Request") ); m_sockstrm->Read(data, s); if (size) - *size = s; + *size = s; return data; - } } -bool wxTCPConnection::DoPoke (const wxString& item, const void *data, size_t size, wxIPCFormat format) +bool wxTCPConnection::DoPoke(const wxString& item, + const void *data, + size_t size, + wxIPCFormat format) { - if (!m_sock->IsConnected()) - return false; + if ( !m_sock->IsConnected() ) + return false; - m_codeco->Write8(IPC_POKE); - m_codeco->WriteString(item); - m_codeco->Write8(format); + m_codeco->Write8(IPC_POKE); + m_codeco->WriteString(item); + m_codeco->Write8(format); - m_codeco->Write32(size); - m_sockstrm->Write(data, size); + m_codeco->Write32(size); + m_sockstrm->Write(data, size); - return true; + return true; } bool wxTCPConnection::StartAdvise (const wxString& item) { - int ret; + if ( !m_sock->IsConnected() ) + return false; - if (!m_sock->IsConnected()) - return false; + m_codeco->Write8(IPC_ADVISE_START); + m_codeco->WriteString(item); - m_codeco->Write8(IPC_ADVISE_START); - m_codeco->WriteString(item); - - ret = m_codeci->Read8(); - - if (ret != IPC_FAIL) - return true; - else - return false; + int ret = m_codeci->Read8(); + if (ret != IPC_FAIL) + return true; + else + return false; } bool wxTCPConnection::StopAdvise (const wxString& item) { - int msg; + if ( !m_sock->IsConnected() ) + return false; - if (!m_sock->IsConnected()) - return false; + m_codeco->Write8(IPC_ADVISE_STOP); + m_codeco->WriteString(item); - m_codeco->Write8(IPC_ADVISE_STOP); - m_codeco->WriteString(item); + int ret = m_codeci->Read8(); - msg = m_codeci->Read8(); - - if (msg != IPC_FAIL) - return true; - else - return false; + if (ret != IPC_FAIL) + return true; + else + return false; } // Calls that SERVER can make -bool wxTCPConnection::DoAdvise (const wxString& item, - const void *data, size_t size, wxIPCFormat format) +bool wxTCPConnection::DoAdvise(const wxString& item, + const void *data, + size_t size, + wxIPCFormat format) { - if (!m_sock->IsConnected()) - return false; + if ( !m_sock->IsConnected() ) + return false; - m_codeco->Write8(IPC_ADVISE); - m_codeco->WriteString(item); - m_codeco->Write8(format); + m_codeco->Write8(IPC_ADVISE); + m_codeco->WriteString(item); + m_codeco->Write8(format); - m_codeco->Write32(size); - m_sockstrm->Write(data, size); + m_codeco->Write32(size); + m_sockstrm->Write(data, size); - return true; + return true; } // -------------------------------------------------------------------------- @@ -511,241 +500,242 @@ bool wxTCPConnection::DoAdvise (const wxString& item, // -------------------------------------------------------------------------- BEGIN_EVENT_TABLE(wxTCPEventHandler, wxEvtHandler) - EVT_SOCKET(_CLIENT_ONREQUEST_ID, wxTCPEventHandler::Client_OnRequest) - EVT_SOCKET(_SERVER_ONREQUEST_ID, wxTCPEventHandler::Server_OnRequest) + EVT_SOCKET(_CLIENT_ONREQUEST_ID, wxTCPEventHandler::Client_OnRequest) + EVT_SOCKET(_SERVER_ONREQUEST_ID, wxTCPEventHandler::Server_OnRequest) END_EVENT_TABLE() void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event) { - wxSocketBase *sock = event.GetSocket(); - if (!sock) { /* No socket, no glory */ - return ; - } - wxSocketNotify evt = event.GetSocketEvent(); - wxTCPConnection *connection = (wxTCPConnection *)(sock->GetClientData()); + wxSocketBase *sock = event.GetSocket(); + if (!sock) + return ; - // This socket is being deleted; skip this event - if (!connection) - return; + wxSocketNotify evt = event.GetSocketEvent(); + wxTCPConnection *connection = (wxTCPConnection *)(sock->GetClientData()); - wxDataInputStream *codeci; - wxDataOutputStream *codeco; - wxSocketStream *sockstrm; - wxString topic_name = connection->m_topic; - wxString item; + // This socket is being deleted; skip this event + if (!connection) + return; - // We lost the connection: destroy everything - if (evt == wxSOCKET_LOST) - { - sock->Notify(false); - sock->Close(); - connection->OnDisconnect(); - return; - } + wxDataInputStream *codeci; + wxDataOutputStream *codeco; + wxSocketStream *sockstrm; + wxString topic_name = connection->m_topic; + wxString item; - // Receive message number. - codeci = connection->m_codeci; - codeco = connection->m_codeco; - sockstrm = connection->m_sockstrm; - int msg = codeci->Read8(); - - switch (msg) - { - case IPC_EXECUTE: - { - void *data; - size_t size; - wxIPCFormat format; - - format = (wxIPCFormat)codeci->Read8(); - size = codeci->Read32(); - - data = connection->GetBufferAtLeast( size ); - wxASSERT_MSG(data != NULL, - _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") ); - sockstrm->Read(data, size); - - connection->OnExecute (topic_name, data, size, format); - - break; - } - case IPC_ADVISE: - { - item = codeci->ReadString(); - wxIPCFormat format = (wxIPCFormat)codeci->Read8(); - size_t size = codeci->Read32(); - void *data = connection->GetBufferAtLeast( size ); - wxASSERT_MSG(data != NULL, - _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") ); - sockstrm->Read(data, size); - - connection->OnAdvise (topic_name, item, data, size, format); - - break; - } - case IPC_ADVISE_START: - { - item = codeci->ReadString(); - - bool ok = connection->OnStartAdvise (topic_name, item); - if (ok) - codeco->Write8(IPC_ADVISE_START); - else - codeco->Write8(IPC_FAIL); - - break; - } - case IPC_ADVISE_STOP: - { - item = codeci->ReadString(); - - bool ok = connection->OnStopAdvise (topic_name, item); - if (ok) - codeco->Write8(IPC_ADVISE_STOP); - else - codeco->Write8(IPC_FAIL); - - break; - } - case IPC_POKE: - { - item = codeci->ReadString(); - wxIPCFormat format = (wxIPCFormat)codeci->Read8(); - size_t size = codeci->Read32(); - void *data = connection->GetBufferAtLeast( size ); - wxASSERT_MSG(data != NULL, - _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") ); - sockstrm->Read(data, size); - - connection->OnPoke (topic_name, item, data, size, format); - - break; - } - case IPC_REQUEST: - { - wxIPCFormat format; - - item = codeci->ReadString(); - format = (wxIPCFormat)codeci->Read8(); - - size_t user_size = wxNO_LEN; - const void *user_data = connection->OnRequest (topic_name, item, &user_size, format); - - if (user_data) + // We lost the connection: destroy everything + if (evt == wxSOCKET_LOST) { - codeco->Write8(IPC_REQUEST_REPLY); - - if (user_size == wxNO_LEN) - { - switch (format) - { - case wxIPC_TEXT: - case wxIPC_UTF8TEXT: - user_size = strlen((const char *)user_data) + 1; // includes final NUL - break; - case wxIPC_UNICODETEXT: - user_size = (wcslen((const wchar_t *)user_data) + 1) * sizeof(wchar_t); // includes final NUL - break; - default: - user_size = 0; - } - } - - codeco->Write32(user_size); - sockstrm->Write(user_data, user_size); + sock->Notify(false); + sock->Close(); + connection->OnDisconnect(); + return; } - else - codeco->Write8(IPC_FAIL); - break; - } - case IPC_DISCONNECT: - { - sock->Notify(false); - sock->Close(); - connection->SetConnected(false); - connection->OnDisconnect(); - break; - } - default: - codeco->Write8(IPC_FAIL); - break; - } + // Receive message number. + codeci = connection->m_codeci; + codeco = connection->m_codeco; + sockstrm = connection->m_sockstrm; + int msg = codeci->Read8(); + + switch (msg) + { + case IPC_EXECUTE: + { + void *data; + size_t size; + wxIPCFormat format; + + format = (wxIPCFormat)codeci->Read8(); + size = codeci->Read32(); + + data = connection->GetBufferAtLeast( size ); + wxASSERT_MSG(data != NULL, + "Buffer too small in wxTCPEventHandler::Client_OnRequest" ); + sockstrm->Read(data, size); + + connection->OnExecute (topic_name, data, size, format); + + break; + } + case IPC_ADVISE: + { + item = codeci->ReadString(); + wxIPCFormat format = (wxIPCFormat)codeci->Read8(); + size_t size = codeci->Read32(); + void *data = connection->GetBufferAtLeast( size ); + wxASSERT_MSG(data != NULL, + "Buffer too small in wxTCPEventHandler::Client_OnRequest" ); + sockstrm->Read(data, size); + + connection->OnAdvise (topic_name, item, data, size, format); + + break; + } + case IPC_ADVISE_START: + { + item = codeci->ReadString(); + + bool ok = connection->OnStartAdvise (topic_name, item); + if (ok) + codeco->Write8(IPC_ADVISE_START); + else + codeco->Write8(IPC_FAIL); + + break; + } + case IPC_ADVISE_STOP: + { + item = codeci->ReadString(); + + bool ok = connection->OnStopAdvise (topic_name, item); + if (ok) + codeco->Write8(IPC_ADVISE_STOP); + else + codeco->Write8(IPC_FAIL); + + break; + } + case IPC_POKE: + { + item = codeci->ReadString(); + wxIPCFormat format = (wxIPCFormat)codeci->Read8(); + size_t size = codeci->Read32(); + void *data = connection->GetBufferAtLeast( size ); + wxASSERT_MSG(data != NULL, + "Buffer too small in wxTCPEventHandler::Client_OnRequest" ); + sockstrm->Read(data, size); + + connection->OnPoke (topic_name, item, data, size, format); + + break; + } + case IPC_REQUEST: + { + wxIPCFormat format; + + item = codeci->ReadString(); + format = (wxIPCFormat)codeci->Read8(); + + size_t user_size = wxNO_LEN; + const void *user_data = connection->OnRequest(topic_name, + item, + &user_size, + format); + + if (user_data) + { + codeco->Write8(IPC_REQUEST_REPLY); + + if (user_size == wxNO_LEN) + { + switch (format) + { + case wxIPC_TEXT: + case wxIPC_UTF8TEXT: + user_size = strlen((const char *)user_data) + 1; // includes final NUL + break; + case wxIPC_UNICODETEXT: + user_size = (wcslen((const wchar_t *)user_data) + 1) * sizeof(wchar_t); // includes final NUL + break; + default: + user_size = 0; + } + } + + codeco->Write32(user_size); + sockstrm->Write(user_data, user_size); + } + else + codeco->Write8(IPC_FAIL); + + break; + } + case IPC_DISCONNECT: + { + sock->Notify(false); + sock->Close(); + connection->SetConnected(false); + connection->OnDisconnect(); + break; + } + default: + codeco->Write8(IPC_FAIL); + break; + } } void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event) { - wxSocketServer *server = (wxSocketServer *) event.GetSocket(); - if (!server) { /* No server, Then exit */ - return ; - } - wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData(); + wxSocketServer *server = (wxSocketServer *) event.GetSocket(); + if (!server) + return ; + wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData(); - // This socket is being deleted; skip this event - if (!ipcserv) - return; - - if (event.GetSocketEvent() != wxSOCKET_CONNECTION) - return; - - // Accept the connection, getting a new socket - wxSocketBase *sock = server->Accept(); - if (!sock) { /* No socket, no glory */ - return ; - } - if (!sock->Ok()) - { - sock->Destroy(); - return; - } - - wxSocketStream *stream = new wxSocketStream(*sock); - wxDataInputStream *codeci = new wxDataInputStream(*stream); - wxDataOutputStream *codeco = new wxDataOutputStream(*stream); - - int msg; - msg = codeci->Read8(); - - if (msg == IPC_CONNECT) - { - wxString topic_name; - topic_name = codeci->ReadString(); - - wxTCPConnection *new_connection = - (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name); - - if (new_connection) - { - if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) - { - // Acknowledge success - codeco->Write8(IPC_CONNECT); - new_connection->m_topic = topic_name; - new_connection->m_sock = sock; - new_connection->m_sockstrm = stream; - new_connection->m_codeci = codeci; - new_connection->m_codeco = codeco; - sock->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); - sock->SetClientData(new_connection); - sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); - sock->Notify(true); + // This socket is being deleted; skip this event + if (!ipcserv) + return; + + if (event.GetSocketEvent() != wxSOCKET_CONNECTION) + return; + + // Accept the connection, getting a new socket + wxSocketBase *sock = server->Accept(); + if (!sock) + return ; + if (!sock->Ok()) + { + sock->Destroy(); return; - } - else - { - delete new_connection; - // and fall through to delete everything else - } } - } - // Something went wrong, send failure message and delete everything - codeco->Write8(IPC_FAIL); + wxSocketStream *stream = new wxSocketStream(*sock); + wxDataInputStream *codeci = new wxDataInputStream(*stream); + wxDataOutputStream *codeco = new wxDataOutputStream(*stream); - delete codeco; - delete codeci; - delete stream; - sock->Destroy(); + int msg; + msg = codeci->Read8(); + + if (msg == IPC_CONNECT) + { + wxString topic_name; + topic_name = codeci->ReadString(); + + wxTCPConnection *new_connection = + (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name); + + if (new_connection) + { + if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) + { + // Acknowledge success + codeco->Write8(IPC_CONNECT); + new_connection->m_topic = topic_name; + new_connection->m_sock = sock; + new_connection->m_sockstrm = stream; + new_connection->m_codeci = codeci; + new_connection->m_codeco = codeco; + sock->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); + sock->SetClientData(new_connection); + sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); + sock->Notify(true); + return; + } + else + { + delete new_connection; + // and fall through to delete everything else + } + } + } + + // Something went wrong, send failure message and delete everything + codeco->Write8(IPC_FAIL); + + delete codeco; + delete codeci; + delete stream; + sock->Destroy(); } // -------------------------------------------------------------------------- @@ -754,15 +744,13 @@ void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event) class wxTCPEventHandlerModule: public wxModule { - DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule) - public: - bool OnInit() { gs_handler = new wxTCPEventHandler(); return true; } - void OnExit() { wxDELETE(gs_handler); } + virtual bool OnInit() { gs_handler = new wxTCPEventHandler; return true; } + virtual void OnExit() { wxDELETE(gs_handler); } + + DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule) }; IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule, wxModule) - -#endif - // wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS +#endif // wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS