{"id":4272,"date":"2022-02-17T06:20:28","date_gmt":"2022-02-17T06:20:28","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4272"},"modified":"2022-02-17T06:20:29","modified_gmt":"2022-02-17T06:20:29","slug":"13h-wxwidgets-interprocess-communications","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/02\/17\/13h-wxwidgets-interprocess-communications\/","title":{"rendered":"13h. wxWidgets &#8211; Interprocess Communications"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Interprocess communication is useful when two programs need to communicate with each other. There are already existing networking protocols like TCP\/IP, HTTP, FTP which enable programs to talk to each other. But in this particular case, we are only interested in the scenario where the two programs which want to communicate are on the same computer. <\/p>\n\n\n\n<p>Though wxWidgets provides functionality for IPC (inter process communiction) to work across different computers, this post will only see how two programs residing on the same computer can talk to each other.<\/p>\n\n\n\n<p>wxWidgets supports two kinds of IPC:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Windows DDE based <\/strong>&#8211; This is a high level protocol which is based on the Dynamic Data Exchange protocol developed for MS Windows<\/li><li><strong>Sockets based<\/strong> &#8211; This is what programs running under Linux\/Unix run on. They use the local network protocol to open sockets between programs.<\/li><\/ul>\n\n\n\n<p>wxWidgets provides a high layer of abstraction for IPC by internally implementing one of the two above methods based on the underlying OS and how wxWidgets has been configured. For eg.<em>wxServer<\/em> automatically uses wxDDEServer or wxTCPServer in its internal implementation.<\/p>\n\n\n\n<p>Basically there are 3 objects which take part in IPC:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>wxClient &#8211; program which acts as a client <\/li><li>wxServer &#8211; program which acts as a server<\/li><li>wxConnection &#8211; the actual connection class which connects clients and server. Both client and server use the same wxConnection class but implement it differently.<\/li><\/ul>\n\n\n\n<p>The sample code below consists of two different sets of programs. One for the server and one for the client. Each set consists of three objects:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the application loader program which loads and launches the client\/server app<\/li><li>the client\/server app<\/li><li>the connection app for the client\/server<\/li><\/ul>\n\n\n\n<p>Both the programs run on the console and no GUI has been used in this case. You have to run the server app first and then run the client app in a separate terminal or window so that it can connect to the server. To terminate either of the two apps, press Ctrl-C<\/p>\n\n\n\n<p><strong>SAMPLE CODE &#8211; SERVER<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ip-server-conn.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include  &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/ipc.h&gt;\n\n#ifndef SERVERCONNECTION\n\nclass ServerConnection: public wxConnection {\n\tpublic:\n\t\tServerConnection();\n\t\t~ServerConnection();\n\t\tbool OnExec (const wxString &amp;topic, const wxString &amp;data);\n\t\tbool OnPoke (const wxString &amp;topic, const wxString &amp;item, const void *data, size_t size, wxIPCFormat format);\n\t\tvoid const* OnRequest (const wxString &amp;topic, const wxString &amp;item, size_t *size, wxIPCFormat format);\n\t\tbool OnAdvise (const wxString &amp;topic, const wxString &amp;item, const void *data, size_t size, wxIPCFormat format);\n\t\tbool DoAdvise(const wxString&amp; item, const void *data, size_t size, wxIPCFormat format);\n\t\tbool OnStopAdvise(const wxString&amp; topic, const wxString&amp; item);\n\t\tbool OnStartAdvise(const wxString&amp; topic, const wxString&amp; item);\n\t\tbool OnDisconnect ();\n\n\t\twxString m_advise;\n};\n#define SERVERCONNECTION 1\n#endif\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-server-conn.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;ip-server-conn.h&quot;\n\nServerConnection::ServerConnection(void):wxConnection() { \n  wxPuts(&quot;ServerConnection() created&quot;);\n}\n\nServerConnection::~ServerConnection(void) { \n\n}\n\nbool ServerConnection::OnExec (const wxString &amp;topic, const wxString &amp;data) {\n\t wxPuts(&quot;onExec&quot;);\n\t wxPuts(topic); wxPuts(data);\n\t return TRUE;\n\n}\n\nbool ServerConnection::OnPoke (const wxString &amp;topic, const wxString &amp;item, const void *data, size_t size, wxIPCFormat format) {\n\t wxPuts(&quot;OnPoke&quot;);\n\t return wxConnection::OnPoke(topic, item, data, size, format);\n\n}\nbool ServerConnection::OnDisconnect() {\n\twxPuts(&quot;OnDisconnect&quot;);\n}\n\nvoid const* ServerConnection::OnRequest (const wxString &amp;topic, const wxString &amp;item, size_t *size, wxIPCFormat format) {\n\twxPuts(&quot;onRequest&quot;);\n}\n\nbool ServerConnection::OnAdvise (const wxString &amp;topic, const wxString &amp;item, const void *data, size_t size, wxIPCFormat format) {\n\twxPuts(&quot;onadvise&quot;);\n\t  m_advise = item;\n    \treturn true;\n}\n\nbool ServerConnection::OnStartAdvise(const wxString&amp; topic, const wxString&amp; item)\n{\n    wxPuts(&quot;OnStartAdvise&quot;);\n    wxPuts(topic);\n    m_advise = item;\n    return true;\n}\n\nbool ServerConnection::OnStopAdvise(const wxString&amp; topic, const wxString&amp; item)\n{\n    wxPuts(&quot;OnStopAdvise&quot;);\n    m_advise.clear();\n    return true;\n}\n\nbool ServerConnection::DoAdvise(const wxString&amp; item, const void *data, size_t size, wxIPCFormat format)\n{\n    wxPuts(&quot;DoAdvise&quot;);\n    return wxConnection::DoAdvise(item, data, size, format);\n}\n\n\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-server.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include  &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/ipc.h&gt;\n#include &quot;ip-server-conn.h&quot;\n\nclass Server: public wxServer {\n\t\n\tpublic:\n\t    Server();\n\t    virtual ~Server();\n\n\t    void Disconnect();\n\t    bool IsConnected() { return m_connection != NULL; }\n\t    ServerConnection *GetConnection() { return m_connection; }\n\n\t    void Advise();\n\t    bool CanAdvise() { return m_connection &amp;&amp; !m_connection-&gt;m_advise.empty(); }\n\n\t    virtual wxConnectionBase *OnAcceptConnection(const wxString&amp; topic) wxOVERRIDE;\n\n\tprotected:\n\t    ServerConnection *m_connection;\n};\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-server.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;ip-server.h&quot;\n#include &quot;ip-server-conn.h&quot;\n\n\n\nServer::Server() : wxServer()\n{\n    m_connection = NULL;\n}\n\nServer::~Server()\n{\n    Disconnect();\n}\n\nwxConnectionBase *Server::OnAcceptConnection(const wxString&amp; topic)\n{\n    wxPrintf(&quot;OnAcceptConnection(%s)&quot;, topic);\n\n    if ( topic == wxT(&quot;IPC TEST&quot;) )\n    {\n        m_connection = new ServerConnection();\n        wxPuts(&quot;Connection accepted&quot;);\n        return m_connection;\n    }\n    \/\/else: unknown topic\n\n    wxPuts(&quot;Unknown topic, connection refused&quot;);\n    return NULL;\n}\n\nvoid Server::Disconnect()\n{\n    if ( m_connection )\n    {\n        wxDELETE(m_connection);\n        wxPuts(&quot;Disconnected client&quot;);\n    }\n}\n\nvoid Server::Advise()\n{\n    wxPuts(&quot;Advise()&quot;);\n    if ( CanAdvise() )\n    {\n\t \n        const wxDateTime now = wxDateTime::Now();\n\n        wxString str = wxString::FromUTF8(&quot;\\xd0\\x9f\\xd1\\x80\\xd0\\xb8\\xd0\\xb2\\xd0\\xb5\\xd1\\x82&quot;);\n        m_connection-&gt;Advise(m_connection-&gt;m_advise, str + &quot; (using UTF-8)&quot;);\n\n        str += &quot; (using wchar_t)&quot;;\n        m_connection-&gt;Advise(m_connection-&gt;m_advise,\n                             str.wc_str(), (str.length() + 1)*sizeof(wchar_t),\n                             wxIPC_UNICODETEXT);\n\n        \/\/ This one uses wxIPC_TEXT by default.\n        const wxString s = now.FormatTime() + &quot; &quot; + now.FormatDate();\n        m_connection-&gt;Advise(m_connection-&gt;m_advise, s.mb_str(), wxNO_LEN);\n\n        char bytes&#x5B;3] = { &#039;1&#039;, &#039;2&#039;, &#039;3&#039; };\n        m_connection-&gt;Advise(m_connection-&gt;m_advise, bytes, 3, wxIPC_PRIVATE);\n    } else \n\t    wxPuts(&quot;Unable to advise&quot;);\n}\n\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-server-main.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t\tvirtual bool OnInit();\n\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-server-main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;ip-server.h&quot;\n#include &quot;ip-server-conn.h&quot;\n#include &lt;wx\/utils.h&gt;\n#include &lt;wx\/ipc.h&gt;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\twxString serverName = &quot;8000&quot;;\n\tServer *server = new Server();\n\tserver-&gt;Create(serverName);\n\tif (!server-&gt;Create(serverName)) {\n\t    wxMessageBox(&quot;Failed to create server&quot;, &quot;Client Demo Error&quot;);\n\t    return NULL;\n\n\t}\n\treturn TRUE;\n\t\t\n}\n\n\n<\/pre><\/div>\n\n\n<p>The sample output is given below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"419\" height=\"164\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/server.png\" alt=\"\" class=\"wp-image-4279\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/server.png 419w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/server-300x117.png 300w\" sizes=\"auto, (max-width: 419px) 100vw, 419px\" \/><\/figure>\n\n\n\n<p><strong>SAMPLE CODE &#8211; CLIENT<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ip-client-conn.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include  &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/ipc.h&gt;\n\nclass ClientConnection: public wxConnection {\n\tpublic:\n\t\tClientConnection();\n\t\t~ClientConnection();\n\t\tbool OnAdvise(const wxString &amp;topic, const wxString &amp;item, char *data, int size, wxIPCFormat format);\n};\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-client-conn.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;ip-client-conn.h&quot;\n\nClientConnection::ClientConnection(void):wxConnection() { }\n\nClientConnection::~ClientConnection(void) { \n\n}\n\nbool ClientConnection::OnAdvise(const wxString&amp; topic, const wxString&amp; item, char *data,\n                  int size, wxIPCFormat format)   {\n        wxMessageBox(topic, data);\n}\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-client.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include  &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/ipc.h&gt;\n\nclass Client: public wxClient {\n\t\n\tpublic:\n\t\tClient();\n\t\twxConnectionBase *OnMakeConnection();\n};\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-client.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;ip-client.h&quot;\n#include &quot;ip-client-conn.h&quot;\n\nClient::Client(void): wxClient() { }\nwxConnectionBase* Client::OnMakeConnection(void)    {\n        return new ClientConnection();\n}\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-client-main.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t\tvirtual bool OnInit();\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">ip-client-main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;ip-client.h&quot;\n#include &quot;ip-client-conn.h&quot;\n#include &lt;wx\/utils.h&gt;\n#include &lt;wx\/ipc.h&gt;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\twxString server = &quot;8000&quot;;\n\twxString hostName = wxT(&quot;localhost&quot;);\n\tClient *client = new Client();\n\tClientConnection *connection = (ClientConnection *)client-&gt;MakeConnection(hostName, server, &quot;IPC TEST&quot;);\n\tif (!connection)\n\t{\n\t    wxMessageBox(&quot;Failed to make connection to server&quot;, &quot;Client Demo Error&quot;);\n\t    return NULL;\n\t}\n\twxPuts(&quot;starting advise..&quot;);\n\tconnection-&gt;StartAdvise(&quot;Item&quot;);\n\twxPuts(&quot;started advise&quot;);\n\twxSleep(5);\n\tconnection-&gt;StopAdvise(&quot;Item&quot;);\n\twxPuts(&quot;stopped advise&quot;);\n\n        wxString s = &quot;Date&quot;;\n\n        connection-&gt;Execute(s);\n\twxPuts(&quot;started execute&quot;);\n\n\twxSleep(5);\n\twxPuts(&quot;Called disconnect&quot;);\n\n\tconnection-&gt;Disconnect();\n\n\twxDELETE(client);\n\twxPuts(&quot;Client object deleted&quot;);\n\t\n\treturn TRUE;\n}\n\n\n<\/pre><\/div>\n\n\n<p>The sample output is given below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"533\" height=\"121\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/client.png\" alt=\"\" class=\"wp-image-4281\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/client.png 533w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/client-300x68.png 300w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Interprocess communication is useful when two programs need to communicate with each other. There are already existing networking protocols like TCP\/IP, HTTP, FTP which <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/02\/17\/13h-wxwidgets-interprocess-communications\/\" title=\"13h. wxWidgets &#8211; Interprocess Communications\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4272"}],"version-history":[{"count":9,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4272\/revisions"}],"predecessor-version":[{"id":4283,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4272\/revisions\/4283"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}