]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/sckipc.cpp
Committing in .
[wxWidgets.git] / src / common / sckipc.cpp
index 55fcb3104bd01e34ecd048fd674e32200300c691..9034bc595b019c4b539ffa0403dfd3ae43d2faa5 100644 (file)
 #pragma implementation "sckipc.h"
 #endif
 
 #pragma implementation "sckipc.h"
 #endif
 
-#include <stdlib.h>
-#include <stdio.h>
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
 
 
-#ifdef WXPREC
-#include <wx/wxprec.h>
-#else
-#include <wx/wx.h>
+#ifdef __BORLANDC__
+#pragma hdrstop
 #endif
 
 #endif
 
+#if wxUSE_SOCKETS
+
+#ifndef WX_PRECOMP
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+
 #include "wx/socket.h"
 #include "wx/sckipc.h"
 
 #include "wx/socket.h"
 #include "wx/sckipc.h"
 
@@ -54,22 +60,22 @@ enum {
 #endif
 
 void Server_OnRequest(wxSocketServer& server,
 #endif
 
 void Server_OnRequest(wxSocketServer& server,
-                     wxSocketBase::wxRequestEvent evt,
+                     wxSocketNotify evt,
                      char *cdata);
 void Client_OnRequest(wxSocketBase& sock,
                      char *cdata);
 void Client_OnRequest(wxSocketBase& sock,
-                     wxSocketBase::wxRequestEvent evt,
+                     wxSocketNotify evt,
                      char *cdata);
 
 // ---------------------------------------------------------------------------
 // wxTCPClient
 // ---------------------------------------------------------------------------
 
                      char *cdata);
 
 // ---------------------------------------------------------------------------
 // wxTCPClient
 // ---------------------------------------------------------------------------
 
-wxTCPClient::wxTCPClient (void)
+wxTCPClient::wxTCPClient ()
   : wxClientBase()
 {
 }
 
   : wxClientBase()
 {
 }
 
-wxTCPClient::~wxTCPClient (void)
+wxTCPClient::~wxTCPClient ()
 {
 }
 
 {
 }
 
@@ -85,12 +91,12 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
                                                const wxString& topic)
 {
   wxIPV4address addr;
                                                const wxString& topic)
 {
   wxIPV4address addr;
-  wxSocketHandler *hsock = &wxSocketHandler::Master();
-  wxSocketClient *client = hsock->CreateClient();
+  wxSocketClient *client = new wxSocketClient();
   wxSocketStream *stream = new wxSocketStream(*client);
   wxSocketStream *stream = new wxSocketStream(*client);
-  wxDataStream data_s(*stream);
+  wxDataInputStream data_is(*stream);
+  wxDataOutputStream data_os(*stream);
   
   
-  client->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
+  client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
   addr.Service(server_name);
   addr.Hostname(host);
 
   addr.Service(server_name);
   addr.Hostname(host);
 
@@ -103,10 +109,10 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
   // Send topic name, and enquire whether this has succeeded
   unsigned char msg;
   
   // Send topic name, and enquire whether this has succeeded
   unsigned char msg;
   
-  data_s.Write8(IPC_CONNECT);
-  data_s.WriteString(topic);
+  data_os.Write8(IPC_CONNECT);
+  data_os.WriteString(topic);
   
   
-  msg = data_s.Read8();
+  msg = data_is.Read8();
   
   // OK! Confirmation.
   if (msg == IPC_CONNECT) {
   
   // OK! Confirmation.
   if (msg == IPC_CONNECT) {
@@ -141,7 +147,7 @@ wxConnectionBase *wxTCPClient::OnMakeConnection()
 // wxTCPServer
 // ---------------------------------------------------------------------------
 
 // wxTCPServer
 // ---------------------------------------------------------------------------
 
-wxTCPServer::wxTCPServer (void)
+wxTCPServer::wxTCPServer ()
   : wxServerBase()
 {
 }
   : wxServerBase()
 {
 }
@@ -149,26 +155,25 @@ wxTCPServer::wxTCPServer (void)
 bool wxTCPServer::Create(const wxString& server_name)
 {
   wxIPV4address addr;
 bool wxTCPServer::Create(const wxString& server_name)
 {
   wxIPV4address addr;
-  wxSocketHandler *hsock = &wxSocketHandler::Master();
   wxSocketServer *server;
 
   addr.Service(server_name);
 
   // Create a socket listening on specified port
   wxSocketServer *server;
 
   addr.Service(server_name);
 
   // Create a socket listening on specified port
-  server = hsock->CreateServer(addr);
+  server = new wxSocketServer(addr);
   server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest);
   server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest);
-  server->SetNotify(wxSocketBase::REQ_ACCEPT);
+  server->SetNotify(wxSOCKET_CONNECTION_FLAG);
 
   server->CallbackData((char *)this);
 
   return TRUE;
 }
 
 
   server->CallbackData((char *)this);
 
   return TRUE;
 }
 
-wxTCPServer::~wxTCPServer (void)
+wxTCPServer::~wxTCPServer()
 {
 }
 
 {
 }
 
-wxConnectionBase *wxTCPServer::OnAcceptConnection(const wxString& topic)
+wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) )
 {
   return new wxTCPConnection();
 }
 {
   return new wxTCPConnection();
 }
@@ -177,74 +182,79 @@ wxConnectionBase *wxTCPServer::OnAcceptConnection(const wxString& topic)
 // wxTCPConnection
 // ---------------------------------------------------------------------------
 
 // wxTCPConnection
 // ---------------------------------------------------------------------------
 
-wxTCPConnection::wxTCPConnection (void)
+wxTCPConnection::wxTCPConnection ()
   : wxConnectionBase(),
   : wxConnectionBase(),
-    m_sock(NULL), m_sockstrm(NULL), m_codec(NULL)
+    m_sock(NULL), m_sockstrm(NULL), m_codeci(NULL), m_codeco(NULL)
+{
+}
+
+wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer), int WXUNUSED(size))
 {
 }
 
 {
 }
 
-wxTCPConnection::~wxTCPConnection (void)
+wxTCPConnection::~wxTCPConnection ()
 {
   wxDELETE(m_sock);
 {
   wxDELETE(m_sock);
-  wxDELETE(m_codec);
+  wxDELETE(m_codeci);
+  wxDELETE(m_codeco);
   wxDELETE(m_sockstrm);
 }
 
   wxDELETE(m_sockstrm);
 }
 
-void wxTCPConnection::Compress(bool on)
+void wxTCPConnection::Compress(bool WXUNUSED(on))
 {
   // Use wxLZWStream
 }
 
 // Calls that CLIENT can make.
 {
   // Use wxLZWStream
 }
 
 // Calls that CLIENT can make.
-bool wxTCPConnection::Disconnect (void)
+bool wxTCPConnection::Disconnect ()
 {
   // Send the the disconnect message to the peer.
 {
   // Send the the disconnect message to the peer.
-  m_codec->Write8(IPC_DISCONNECT);
+  m_codeco->Write8(IPC_DISCONNECT);
   m_sock->Close();
 
   return TRUE;
 }
 
   m_sock->Close();
 
   return TRUE;
 }
 
-bool wxTCPConnection::Execute (char *data, int size, wxDataFormat format)
+bool wxTCPConnection::Execute (const wxChar *data, int size, wxIPCFormat format)
 {
   if (!m_sock->IsConnected())
     return FALSE;
 
   // Prepare EXECUTE message
 {
   if (!m_sock->IsConnected())
     return FALSE;
 
   // Prepare EXECUTE message
-  m_codec->Write8(IPC_EXECUTE);
-  m_codec->Write8(format);
+  m_codeco->Write8(IPC_EXECUTE);
+  m_codeco->Write8(format);
   if (size < 0)
   if (size < 0)
-    m_codec->WriteString(data);
+    m_codeco->WriteString(data);
   else {
   else {
-    m_codec->Write32(size);
-    m_codec->Write(data, size);
+    m_codeco->Write32(size);
+    m_sockstrm->Write(data, size);
   }
 
   return TRUE;
 }
 
   }
 
   return TRUE;
 }
 
-char *wxTCPConnection::Request (const wxString& item, int *size, wxDataFormat format)
+char *wxTCPConnection::Request (const wxString& item, int *size, wxIPCFormat format)
 {
   if (!m_sock->IsConnected())
     return NULL;
 
 {
   if (!m_sock->IsConnected())
     return NULL;
 
-  m_codec->Write8(IPC_REQUEST);
-  m_codec->WriteString(item);
-  m_codec->Write8(format);
+  m_codeco->Write8(IPC_REQUEST);
+  m_codeco->WriteString(item);
+  m_codeco->Write8(format);
 
   // If Unpack doesn't initialize it.
   int ret;
 
 
   // If Unpack doesn't initialize it.
   int ret;
 
-  ret = m_codec->Read8();
+  ret = m_codeci->Read8();
   if (ret == IPC_FAIL)
     return NULL;
   else {
     size_t s;
     char *data = NULL;
 
   if (ret == IPC_FAIL)
     return NULL;
   else {
     size_t s;
     char *data = NULL;
 
-    s = m_codec->Read32();
+    s = m_codeci->Read32();
     data = new char[s];
     data = new char[s];
-    m_codec->Read(data, s);
+    m_sockstrm->Read(data, s);
 
     if (size)
       *size = s;
 
     if (size)
       *size = s;
@@ -252,19 +262,19 @@ char *wxTCPConnection::Request (const wxString& item, int *size, wxDataFormat fo
   }
 }
 
   }
 }
 
-bool wxTCPConnection::Poke (const wxString& item, char *data, int size, wxDataFormat format)
+bool wxTCPConnection::Poke (const wxString& item, wxChar *data, int size, wxIPCFormat format)
 {
   if (!m_sock->IsConnected())
     return FALSE;
 
 {
   if (!m_sock->IsConnected())
     return FALSE;
 
-  m_codec->Write8(IPC_POKE);
-  m_codec->WriteString(item);
-  m_codec->Write8(format);
+  m_codeco->Write8(IPC_POKE);
+  m_codeco->WriteString(item);
+  m_codeco->Write8(format);
   if (size < 0)
   if (size < 0)
-    m_codec->WriteString(data);
+    m_codeco->WriteString(data);
   else {
   else {
-    m_codec->Write32(size);
-    m_codec->Write(data, size);
+    m_codeco->Write32(size);
+    m_sockstrm->Write(data, size);
   }
 
   return TRUE;
   }
 
   return TRUE;
@@ -277,10 +287,10 @@ bool wxTCPConnection::StartAdvise (const wxString& item)
   if (!m_sock->IsConnected())
     return FALSE;
 
   if (!m_sock->IsConnected())
     return FALSE;
 
-  m_codec->Write8(IPC_ADVISE_START);
-  m_codec->WriteString(item);
+  m_codeco->Write8(IPC_ADVISE_START);
+  m_codeco->WriteString(item);
 
 
-  ret = m_codec->Read8();
+  ret = m_codeci->Read8();
 
   if (ret != IPC_FAIL)
     return TRUE;
 
   if (ret != IPC_FAIL)
     return TRUE;
@@ -295,10 +305,10 @@ bool wxTCPConnection::StopAdvise (const wxString& item)
   if (!m_sock->IsConnected())
     return FALSE;
 
   if (!m_sock->IsConnected())
     return FALSE;
 
-  m_codec->Write8(IPC_ADVISE_STOP);
-  m_codec->WriteString(item);
+  m_codeco->Write8(IPC_ADVISE_STOP);
+  m_codeco->WriteString(item);
 
 
-  msg = m_codec->Read8();
+  msg = m_codeci->Read8();
 
   if (msg != IPC_FAIL)
     return TRUE;
 
   if (msg != IPC_FAIL)
     return TRUE;
@@ -308,54 +318,58 @@ bool wxTCPConnection::StopAdvise (const wxString& item)
 
 // Calls that SERVER can make
 bool wxTCPConnection::Advise (const wxString& item,
 
 // Calls that SERVER can make
 bool wxTCPConnection::Advise (const wxString& item,
-                              char *data, int size, wxDataFormat format)
+                              wxChar *data, int size, wxIPCFormat format)
 {
   if (!m_sock->IsConnected())
     return FALSE;
 
 {
   if (!m_sock->IsConnected())
     return FALSE;
 
-  m_codec->Write8(IPC_ADVISE);
-  m_codec->WriteString(item);
-  m_codec->Write8(format);
+  m_codeco->Write8(IPC_ADVISE);
+  m_codeco->WriteString(item);
+  m_codeco->Write8(format);
   if (size < 0)
   if (size < 0)
-    m_codec->WriteString(data);
+    m_codeco->WriteString(data);
   else {
   else {
-    m_codec->Write32(size);
-    m_codec->Write(data, size);
+    m_codeco->Write32(size);
+    m_sockstrm->Write(data, size);
   }
 
   return TRUE;
 }
 
   }
 
   return TRUE;
 }
 
-void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
+void Client_OnRequest(wxSocketBase& sock, wxSocketNotify evt,
                      char *cdata)
 {
   int msg = 0;
   wxTCPConnection *connection = (wxTCPConnection *)cdata;
                      char *cdata)
 {
   int msg = 0;
   wxTCPConnection *connection = (wxTCPConnection *)cdata;
-  wxDataStream *codec;
+  wxDataInputStream *codeci;
+  wxDataOutputStream *codeco; 
+  wxSocketStream *sockstrm;
   wxString topic_name = connection->m_topic;
   wxString item;
 
   // The socket handler signals us that we lost the connection: destroy all.
   wxString topic_name = connection->m_topic;
   wxString item;
 
   // The socket handler signals us that we lost the connection: destroy all.
-  if (evt == wxSocketBase::EVT_LOST) {
+  if (evt == wxSOCKET_LOST) {
     sock.Close();
     connection->OnDisconnect();
     return;
   }
 
   // Receive message number.
     sock.Close();
     connection->OnDisconnect();
     return;
   }
 
   // Receive message number.
-  codec = connection->m_codec;
-  msg = codec->Read8();
+  codeci = connection->m_codeci;
+  codeco = connection->m_codeco;
+  sockstrm = connection->m_sockstrm;
+  msg = codeci->Read8();
 
   switch (msg) {
   case IPC_EXECUTE: {
     char *data;
     size_t size; 
 
   switch (msg) {
   case IPC_EXECUTE: {
     char *data;
     size_t size; 
-    wxDataFormat format;
+    wxIPCFormat format;
     
     
-    format = (wxDataFormat)codec->Read8();
-    size = codec->Read32();
+    format = (wxIPCFormat)codeci->Read8();
+    size = codeci->Read32();
     data = new char[size];
     data = new char[size];
-    codec->Read(data, size);
+    sockstrm->Read(data, size);
 
     connection->OnExecute (topic_name, data, size, format);
 
 
     connection->OnExecute (topic_name, data, size, format);
 
@@ -365,13 +379,13 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
   case IPC_ADVISE: {
     char *data;
     size_t size;
   case IPC_ADVISE: {
     char *data;
     size_t size;
-    wxDataFormat format;
+    wxIPCFormat format;
 
 
-    item = codec->ReadString();
-    format = (wxDataFormat)codec->Read8();
-    size = codec->Read32();
+    item = codeci->ReadString();
+    format = (wxIPCFormat)codeci->Read8();
+    size = codeci->Read32();
     data = new char[size];
     data = new char[size];
-    codec->Read(data, size);
+    sockstrm->Read(data, size);
     
     connection->OnAdvise (topic_name, item, data, size, format);
 
     
     connection->OnAdvise (topic_name, item, data, size, format);
 
@@ -379,37 +393,37 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
     break;
   }
   case IPC_ADVISE_START: {
     break;
   }
   case IPC_ADVISE_START: {
-    item = codec->ReadString();
+    item = codeci->ReadString();
 
     bool ok = connection->OnStartAdvise (topic_name, item);
     if (ok)
 
     bool ok = connection->OnStartAdvise (topic_name, item);
     if (ok)
-      codec->Write8(IPC_ADVISE_START);
+      codeco->Write8(IPC_ADVISE_START);
     else
     else
-      codec->Write8(IPC_FAIL);
+      codeco->Write8(IPC_FAIL);
 
     break;
   }
   case IPC_ADVISE_STOP: {
 
     break;
   }
   case IPC_ADVISE_STOP: {
-    item = codec->ReadString();
+    item = codeci->ReadString();
 
     bool ok = connection->OnStopAdvise (topic_name, item);
     if (ok)
 
     bool ok = connection->OnStopAdvise (topic_name, item);
     if (ok)
-      codec->Write8(IPC_ADVISE_STOP);
+      codeco->Write8(IPC_ADVISE_STOP);
     else
     else
-      codec->Write8(IPC_FAIL);
+      codeco->Write8(IPC_FAIL);
 
     break;
   }
   case IPC_POKE: {
 
     break;
   }
   case IPC_POKE: {
-    wxDataFormat format;
+    wxIPCFormat format;
     size_t size;
     size_t size;
-    char *data;
+    wxChar *data;
 
 
-    item = codec->ReadString();
-    format = (wxDataFormat)codec->Read8();
-    size = codec->Read32();
-    data = new char[size];
-    codec->Read(data, size);
+    item = codeci->ReadString();
+    format = (wxIPCFormat)codeci->Read8();
+    size = codeci->Read32();
+    data = new wxChar[size];
+    sockstrm->Read(data, size);
     
     connection->OnPoke (topic_name, item, data, size, format);
 
     
     connection->OnPoke (topic_name, item, data, size, format);
 
@@ -418,23 +432,23 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
     break;
   }
   case IPC_REQUEST: {
     break;
   }
   case IPC_REQUEST: {
-    wxDataFormat format;
+    wxIPCFormat format;
 
 
-    item = codec->ReadString();
-    format = (wxDataFormat)codec->Read8();
+    item = codeci->ReadString();
+    format = (wxIPCFormat)codeci->Read8();
 
     int user_size = -1;
     char *user_data = connection->OnRequest (topic_name, item, &user_size, format);
 
     if (user_data) {
 
     int user_size = -1;
     char *user_data = connection->OnRequest (topic_name, item, &user_size, format);
 
     if (user_data) {
-      codec->Write8(IPC_REQUEST_REPLY);
+      codeco->Write8(IPC_REQUEST_REPLY);
       if (user_size != -1) {
       if (user_size != -1) {
-        codec->Write32(user_size);
-        codec->Write(user_data, user_size);
+        codeco->Write32(user_size);
+        sockstrm->Write(user_data, user_size);
       } else
       } else
-        codec->WriteString(user_data);
+        codeco->WriteString(user_data);
     } else
     } else
-      codec->Write8(IPC_FAIL);
+      codeco->Write8(IPC_FAIL);
 
     break;
   }
 
     break;
   }
@@ -444,38 +458,40 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
     break;
   }
   default:
     break;
   }
   default:
-    codec->Write8(IPC_FAIL);
+    codeco->Write8(IPC_FAIL);
     break;
   }
 }
 
 void Server_OnRequest(wxSocketServer& server,
     break;
   }
 }
 
 void Server_OnRequest(wxSocketServer& server,
-                     wxSocketBase::wxRequestEvent evt, char *cdata)
+                     wxSocketNotify evt, char *cdata)
 {
   wxTCPServer *ipcserv = (wxTCPServer *)cdata;
   wxSocketStream *stream;
 {
   wxTCPServer *ipcserv = (wxTCPServer *)cdata;
   wxSocketStream *stream;
-  wxDataStream *codec;
+  wxDataInputStream *codeci;
+  wxDataOutputStream *codeco;
 
 
-  if (evt != wxSocketBase::EVT_ACCEPT)
+  if (evt != wxSOCKET_CONNECTION)
     return;
 
   /* Accept the connection, getting a new socket */
   wxSocketBase *sock = server.Accept();
   sock->Notify(FALSE);
     return;
 
   /* Accept the connection, getting a new socket */
   wxSocketBase *sock = server.Accept();
   sock->Notify(FALSE);
-  sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
+  sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
 
   stream = new wxSocketStream(*sock);
 
   stream = new wxSocketStream(*sock);
-  codec = new wxDataStream(*stream);
+  codeci = new wxDataInputStream(*stream);
+  codeco = new wxDataOutputStream(*stream);
 
   if (!sock->Ok())
     return;
 
   int msg;
 
   if (!sock->Ok())
     return;
 
   int msg;
-  msg = codec->Read8();
+  msg = codeci->Read8();
 
   if (msg == IPC_CONNECT) {
     wxString topic_name;
 
   if (msg == IPC_CONNECT) {
     wxString topic_name;
-    topic_name = codec->ReadString();
+    topic_name = codeci->ReadString();
 
     /* Register new socket with the notifier */
     wxTCPConnection *new_connection =
 
     /* Register new socket with the notifier */
     wxTCPConnection *new_connection =
@@ -483,21 +499,25 @@ void Server_OnRequest(wxSocketServer& server,
     if (new_connection) {
       if (!new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) {
         delete new_connection;
     if (new_connection) {
       if (!new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) {
         delete new_connection;
-        codec->Write8(IPC_FAIL);
+        codeco->Write8(IPC_FAIL);
         return;
       }
       // Acknowledge success
         return;
       }
       // Acknowledge success
-      codec->Write8(IPC_CONNECT);
+      codeco->Write8(IPC_CONNECT);
       
       new_connection->m_topic = topic_name;
       new_connection->m_sockstrm = stream;
       
       new_connection->m_topic = topic_name;
       new_connection->m_sockstrm = stream;
-      new_connection->m_codec = codec;
+      new_connection->m_codeci = codeci;
+      new_connection->m_codeco = codeco;
       sock->Callback(Client_OnRequest);
       sock->CallbackData((char *)new_connection);
       sock->Notify(TRUE);
     } else {
       // Send failure message
       sock->Callback(Client_OnRequest);
       sock->CallbackData((char *)new_connection);
       sock->Notify(TRUE);
     } else {
       // Send failure message
-      codec->Write8(IPC_FAIL);
+      codeco->Write8(IPC_FAIL);
     }
   }
 }
     }
   }
 }
+
+#endif
+  // wxUSE_SOCKETS