[wx-dev] IPC issues

Brian Vanderburg II BrianVanderburg2 at aim.com
Sat Dec 1 21:48:36 PST 2007


This is just another possible idea.  It seems that in IPC, the data from =

one end to the other should remain in the same format (no conversions or =

changes to the data should take place).  This is not a problem for TCP.  =

It doesn't seem like it should be a problem for DDE except for Execute, =

but seems like the rest should be fine, that is, if an application pokes =

data as wxIPC_TEXT windows DDE doesn't seem like it does anything to it =

and sends it to the server as wxIPC_TEXT.

So since Execute seems to be the only problem, perhaps it should be =

changed to only support command strings.  The remaining functions can be =

kept as is, where char* will default to wxIPC_TEXT, and wchar_T will be =

wxIPC_UNICODETEXT, or the application can if sending text convert to =

utf8 and use the void* version of the other functions.  Execute should =

take only wxString

I've attached a patch illustrating this idea a little better.


Brian Vanderburg II

-------------- next part --------------
Index: include/wx/ipcbase.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- include/wx/ipcbase.h    (revision 50401)
+++ include/wx/ipcbase.h    (working copy)
@@ -62,21 +62,8 @@
   bool GetConnected() { return m_connected; }
 =

   // Calls that CLIENT can make
-  bool Execute(const void *data, size_t size, wxIPCFormat fmt =3D wxIPC_PR=
IVATE)
-      { return DoExecute(data, size, fmt); }
-  bool Execute(const char *s, size_t size =3D wxNO_LEN)
-      { return DoExecute(s, size =3D=3D wxNO_LEN ? strlen(s) + 1
-                                             : size, wxIPC_TEXT); }
-  bool Execute(const wchar_t *ws, size_t size =3D wxNO_LEN)
-      { return DoExecute(ws, size =3D=3D wxNO_LEN ? (wcslen(ws) + 1)*sizeo=
f(wchar_t)
-                                              : size, wxIPC_UNICODETEXT); }
-  bool Execute(const wxString& s)
-  {
-      const wxUTF8Buf buf =3D s.utf8_str();
-      return DoExecute(buf, strlen(buf) + 1, wxIPC_UTF8TEXT);
-  }
-  bool Execute(const wxCStrData& cs)
-      { return Execute(cs.AsString()); }
+  bool Execute(const wxString& command)
+      { return DoExecute(command); }
 =

   virtual const void *Request(const wxString& item,
                               size_t *size =3D NULL,
@@ -128,9 +115,7 @@
 =

   // Callbacks to SERVER - override at will
   virtual bool OnExecute(const wxString& WXUNUSED(topic),
-                         const void *WXUNUSED(data),
-                         size_t WXUNUSED(size),
-                         wxIPCFormat WXUNUSED(format))
+                         const wxString& WXUNUSED(command))
       { return false; }
 =

   virtual const void *OnRequest(const wxString& WXUNUSED(topic),
@@ -165,13 +150,12 @@
   // Callbacks to BOTH
   virtual bool OnDisconnect() { delete this; return true; }
 =

-
   // return a buffer at least this size, reallocating buffer if needed
   // returns NULL if using an inadequate user buffer which can't be resized
   void *GetBufferAtLeast(size_t bytes);
 =

 protected:
-  virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format=
) =3D 0;
+  virtual bool DoExecute(const wxString& command) =3D 0;
   virtual bool DoPoke(const wxString& item, const void *data, size_t size,
                       wxIPCFormat format) =3D 0;
   virtual bool DoAdvise(const wxString& item, const void *data, size_t siz=
e,
Index: include/wx/msw/dde.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- include/wx/msw/dde.h    (revision 50401)
+++ include/wx/msw/dde.h    (working copy)
@@ -57,7 +57,7 @@
   virtual bool Disconnect();
 =

 protected:
-  virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format=
);
+  virtual bool DoExecute(const wxString& command);
   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 siz=
e,
Index: include/wx/sckipc.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- include/wx/sckipc.h (revision 50401)
+++ include/wx/sckipc.h (working copy)
@@ -72,7 +72,7 @@
   virtual bool Disconnect(void);
 =

 protected:
-  virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format=
);
+  virtual bool DoExecute(const wxString& command);
   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 siz=
e,
Index: src/common/sckipc.cpp
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- src/common/sckipc.cpp   (revision 50401)
+++ src/common/sckipc.cpp   (working copy)
@@ -393,17 +393,19 @@
   return true;
 }
 =

-bool wxTCPConnection::DoExecute(const void *data, size_t size, wxIPCFormat=
 format)
+bool wxTCPConnection::DoExecute(const wxString& command)
 {
   if (!m_sock->IsConnected())
     return false;
 =

+  wxUTF8Buf buffer =3D command.utf8_str();
+  size_t size =3D strlen(buffer) + 1;
+
   // Prepare EXECUTE message
   m_codeco->Write8(IPC_EXECUTE);
-  m_codeco->Write8(format);
 =

   m_codeco->Write32(size);
-  m_sockstrm->Write(data, size);
+  m_sockstrm->Write((const void*)(const char*)data, size);
 =

   return true;
 }
@@ -555,17 +557,17 @@
   {
     void *data;
     size_t size;
-    wxIPCFormat format;
 =

-    format =3D (wxIPCFormat)codeci->Read8();
     size =3D codeci->Read32();
     =

     data =3D connection->GetBufferAtLeast( size );
     wxASSERT_MSG(data !=3D NULL,
                  _T("Buffer too small in wxTCPEventHandler::Client_OnReque=
st") );
     sockstrm->Read(data, size);
+    =

+    wxString cmd((char*)data, wxConvUTF8, size - 1); // don't embed null t=
erminator into string
 =

-    connection->OnExecute (topic_name, data, size, format);
+    connection->OnExecute (cmd);
 =

     break;
   }
Index: src/msw/dde.cpp
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- src/msw/dde.cpp (revision 50401)
+++ src/msw/dde.cpp (working copy)
@@ -548,94 +548,20 @@
 }
 =

 bool
-wxDDEConnection::DoExecute(const void *data, size_t size, wxIPCFormat form=
at)
+wxDDEConnection::DoExecute(const wxString& command)
 {
-    wxCHECK_MSG( format =3D=3D wxIPC_TEXT ||
-                 format =3D=3D wxIPC_UTF8TEXT ||
-                 format =3D=3D wxIPC_UNICODETEXT,
-                 false,
-                 _T("wxDDEServer::Execute() supports only text data") );
-
-    wxMemoryBuffer buffer;
-    LPBYTE realData wxDUMMY_INITIALIZE(NULL);
-    size_t realSize wxDUMMY_INITIALIZE(0);
-    wxMBConv *conv =3D NULL;
-
-    // Windows only supports either ANSI or UTF-16 format depending on the
-    // build, so we need to convert the data if it doesn't use it already
+    // Convert data to the correct format
+    wxCStrData data =3D command.c_str();
 #if wxUSE_UNICODE
-    if ( format =3D=3D wxIPC_TEXT )
-    {
-        conv =3D &wxConvLibc;
-    }
-    else if ( format =3D=3D wxIPC_UTF8TEXT )
-    {
-        conv =3D &wxConvUTF8;
-    }
-    else // no conversion necessary for wxIPC_UNICODETEXT
-    {
-        realData =3D (LPBYTE)data;
-        realSize =3D size;
-    }
+    const wchar_t* realData =3D (const wchar_t*)data;
+#else
+    const char* realData =3D (const char*)data;
+#endif
+    size_t realSize =3D wxStrlen(realData) + 1;
+    =

 =

-    if ( conv )
-    {
-        const char * const text =3D (const char *)data;
-        const size_t len =3D size/sizeof(char);
-
-        realSize =3D conv->ToWChar(NULL, 0, text, len);
-        if ( realSize =3D=3D wxCONV_FAILED )
-            return false;
-
-        realData =3D (LPBYTE)buffer.GetWriteBuf(realSize*sizeof(wchar_t));
-        if ( !realData )
-            return false;
-
-        realSize =3D conv->ToWChar((wchar_t *)realData, realSize, text, le=
n);
-        if ( realSize =3D=3D wxCONV_FAILED )
-            return false;
-    }
-#else // !wxUSE_UNICODE
-    if ( format =3D=3D wxIPC_UNICODETEXT )
-    {
-        conv =3D &wxConvLibc;
-    }
-    else if ( format =3D=3D wxIPC_UTF8TEXT )
-    {
-        // we could implement this in theory but it's not obvious how to p=
ass
-        // the format information and, basically, why bother -- just use
-        // Unicode build
-        wxFAIL_MSG( _T("UTF-8 text not supported in ANSI build") );
-
-        return false;
-    }
-    else // don't convert wxIPC_TEXT
-    {
-        realData =3D (LPBYTE)data;
-        realSize =3D size;
-    }
-
-    if ( conv )
-    {
-        const wchar_t * const wtext =3D (const wchar_t *)data;
-        const size_t len =3D size/sizeof(wchar_t);
-
-        realSize =3D conv->FromWChar(NULL, 0, wtext, len);
-        if ( realSize =3D=3D wxCONV_FAILED )
-            return false;
-
-        realData =3D (LPBYTE)buffer.GetWriteBuf(realSize*sizeof(char));
-        if ( !realData )
-            return false;
-
-        realSize =3D conv->FromWChar((char*)realData, realSize, wtext, len=
);
-        if ( realSize =3D=3D wxCONV_FAILED )
-            return false;
-    }
-#endif // wxUSE_UNICODE/!wxUSE_UNICODE
-
     DWORD result;
-    bool ok =3D DdeClientTransaction(realData,
+    bool ok =3D DdeClientTransaction((LPBYTE)realData,
                                     realSize,
                                     GetHConv(),
                                     NULL,
@@ -854,16 +780,26 @@
                     // XTYP_EXECUTE can be used for text only and the text=
 is
                     // always in ANSI format for ANSI build and Unicode fo=
rmat
                     // in Unicode build
+                    wxString cmd =3D wxEmptyString;
+                    =

                     #if wxUSE_UNICODE
-                        wFmt =3D wxIPC_UNICODETEXT;
+                        if(len >=3D sizeof(wchar_t))
+                        {
+                            len /=3D sizeof(wchar_t); // convert to charac=
ters
+                            len -=3D 1; // don't embed null terminator
+                            cmd =3D wxString((wchar_t*)data, (size_t)len);
+                        }
                     #else
-                        wFmt =3D wxIPC_TEXT;
+                        if(len >=3D sizeof(char))
+                        {
+                            len /=3D sizeof(char); // convert to characters
+                            len -=3D 1; // don't embed null terminator
+                            cmd =3D wxString((char*)data, (size_t)len);
+                        }
                     #endif
-
-                    if ( connection->OnExecute(connection->m_topicName,
-                                               data,
-                                               (int)len,
-                                               (wxIPCFormat)wFmt) )
+                    =

+                    =

+                    if ( connection->OnExecute(cmd) )
                     {
                         return (DDERETURN)(DWORD)DDE_FACK;
                     }


More information about the wx-dev mailing list