File: overview_ipc.html

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (159 lines) | stat: -rw-r--r-- 20,784 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>wxWidgets: Interprocess Communication</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page_container">
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0" style="width: 100%;">
 <tbody>
 <tr>
  <td id="projectlogo">
    <a href="http://www.wxwidgets.org/" target="_new">
      <img alt="wxWidgets" src="logo.png"/>
    </a>
  </td>
  <td style="padding-left: 0.5em; text-align: right;">
   <span id="projectnumber">Version: 3.0.2</span>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- Generated by Doxygen 1.8.2 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Categories</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="index.html">Documentation</a></li><li class="navelem"><a class="el" href="page_topics.html">Programming Guides</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Interprocess Communication </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#overview_ipc_datatransfer">Data Transfer</a></li>
<li class="level1"><a href="#overview_ipc_examples">Examples</a></li>
<li class="level1"><a href="#overview_ipc_dde">More DDE Details</a></li>
</ul>
</div>
<div class="textblock"><p>wxWidgets has a number of different classes to help with interprocess communication and network programming.</p>
<p>This section only discusses one family of classes &ndash; the DDE-like protocol &ndash; but here's a list of other useful classes:</p>
<ul>
<li><a class="el" href="classwx_socket_event.html" title="This event class contains information about socket events.">wxSocketEvent</a>, <a class="el" href="classwx_socket_base.html" title="wxSocketBase is the base class for all socket-related objects, and it defines all basic IO functional...">wxSocketBase</a>, <a class="el" href="classwx_socket_client.html">wxSocketClient</a>, <a class="el" href="classwx_socket_server.html">wxSocketServer</a> - Classes for the low-level TCP/IP API. </li>
<li><a class="el" href="classwx_protocol.html" title="Represents a protocol for use with wxURL.">wxProtocol</a>, <a class="el" href="classwx_u_r_l.html" title="wxURL is a specialization of wxURI for parsing URLs.">wxURL</a>, <a class="el" href="classwx_f_t_p.html" title="wxFTP can be used to establish a connection to an FTP server and perform all the usual operations...">wxFTP</a>, <a class="el" href="classwx_h_t_t_p.html" title="wxHTTP can be used to establish a connection to an HTTP server.">wxHTTP</a> - Classes for programming popular Internet protocols.</li>
</ul>
<p>wxWidgets' DDE-like protocol is a high-level protocol based on Windows DDE. There are two implementations of this DDE-like protocol: one using real DDE running on Windows only, and another using TCP/IP (sockets) that runs on most platforms. Since the API and virtually all of the behaviour is the same apart from the names of the classes, you should find it easy to switch between the two implementations.</p>
<p>Notice that by including <code>&lt;<a class="el" href="interface_2wx_2ipc_8h.html">wx/ipc.h</a>&gt;</code> you may define convenient synonyms for the IPC classes: <a class="el" href="classwx_server.html" title="A wxServer object represents the server part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxServer</a> for either <a class="el" href="classwx_d_d_e_server.html" title="A wxDDEServer object represents the server part of a client-server DDE (Dynamic Data Exchange) conver...">wxDDEServer</a> or <a class="el" href="classwx_t_c_p_server.html" title="A wxTCPServer object represents the server part of a client-server conversation.">wxTCPServer</a> depending on whether DDE-based or socket-based implementation is used and the same thing for <a class="el" href="classwx_client.html" title="A wxClient object represents the client part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxClient</a> and <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a>.</p>
<p>By default, the DDE implementation is used under Windows. DDE works within one computer only. If you want to use IPC between different workstations you should define <code>wxUSE_DDE_FOR_IPC</code> as 0 before including this header &ndash; this will force using TCP/IP implementation even under Windows.</p>
<p>The following description refers to wxWidgets, but remember that the equivalent wxTCP* and wxDDE* classes can be used in much the same way.</p>
<p>Three classes are central to the DDE-like API:</p>
<ul>
<li><a class="el" href="classwx_client.html" title="A wxClient object represents the client part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxClient</a> - This represents the client application, and is used only within a client program. </li>
<li><a class="el" href="classwx_server.html" title="A wxServer object represents the server part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxServer</a> - This represents the server application, and is used only within a server program. </li>
<li><a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> - This represents the connection from the client to the server. Both the client and the server use an instance of this class, one per connection. Most DDE transactions operate on this object.</li>
</ul>
<p>Messages between applications are usually identified by three variables: connection object, topic name and item name. A data string is a fourth element of some messages. To create a connection (a conversation in Windows parlance), the client application uses <a class="el" href="classwx_client.html#a6ca1cef89e834a724d34edf9872f545d" title="Tries to make a connection with a server by host (machine name under UNIX - use &#39;localhost&#39; for same ...">wxClient::MakeConnection</a> to send a message to the server object, with a string service name to identify the server and a topic name to identify the topic for the duration of the connection. Under Unix, the service name may be either an integer port identifier in which case an Internet domain socket will be used for the communications or a valid file name (which shouldn't exist and will be deleted afterwards) in which case a Unix domain socket is created.</p>
<p><b>SECURITY NOTE:</b> Using Internet domain sockets is extremely insecure for IPC as there is absolutely no access control for them, use Unix domain sockets whenever possible!</p>
<p>The server then responds and either vetoes the connection or allows it. If allowed, both the server and client objects create <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> objects which persist until the connection is closed. The connection object is then used for sending and receiving subsequent messages between client and server - overriding virtual functions in your class derived from <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> allows you to handle the DDE messages.</p>
<p>To create a working server, the programmer must:</p>
<ul>
<li>Derive a class from <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a>, providing handlers for various messages sent to the server side of a <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> (e.g. OnExecute, OnRequest, OnPoke). Only the handlers actually required by the application need to be overridden. </li>
<li>Derive a class from <a class="el" href="classwx_server.html" title="A wxServer object represents the server part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxServer</a>, overriding OnAcceptConnection to accept or reject a connection on the basis of the topic argument. This member must create and return an instance of the derived connection class if the connection is accepted. </li>
<li>Create an instance of your server object and call Create to activate it, giving it a service name.</li>
</ul>
<p>To create a working client, the programmer must:</p>
<ul>
<li>Derive a class from <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a>, providing handlers for various messages sent to the client side of a <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> (e.g. OnAdvise). Only the handlers actually required by the application need to be overridden. </li>
<li>Derive a class from <a class="el" href="classwx_client.html" title="A wxClient object represents the client part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxClient</a>, overriding OnMakeConnection to create and return an instance of the derived connection class. </li>
<li>Create an instance of your client object. </li>
<li>When appropriate, create a new connection using <a class="el" href="classwx_client.html#a6ca1cef89e834a724d34edf9872f545d" title="Tries to make a connection with a server by host (machine name under UNIX - use &#39;localhost&#39; for same ...">wxClient::MakeConnection</a>, with arguments host name (processed in Unix only, use 'localhost' for local computer), service name, and topic name for this connection. The client object will call OnMakeConnection to create a connection object of the derived class if the connection is successful. </li>
<li>Use the <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> member functions to send messages to the server.</li>
</ul>
<h1><a class="anchor" id="overview_ipc_datatransfer"></a>
Data Transfer</h1>
<p>These are the ways that data can be transferred from one application to another. These are methods of <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a>.</p>
<ul>
<li><b>Execute:</b> the client calls the server with a data string representing a command to be executed. This succeeds or fails, depending on the server's willingness to answer. If the client wants to find the result of the Execute command other than success or failure, it has to explicitly call Request. </li>
<li><b>Request:</b> the client asks the server for a particular data string associated with a given item string. If the server is unwilling to reply, the return value is <span class="literal">NULL</span>. Otherwise, the return value is a string (actually a pointer to the connection buffer, so it should not be deallocated by the application). </li>
<li><b>Poke:</b> The client sends a data string associated with an item string directly to the server. This succeeds or fails. </li>
<li><b>Advise:</b> The client asks to be advised of any change in data associated with a particular item. If the server agrees, the server will send an OnAdvise message to the client along with the item and data.</li>
</ul>
<p>The default data type is wxCF_TEXT (ASCII text), and the default data size is the length of the null-terminated string. Windows-specific data types could also be used on the PC.</p>
<h1><a class="anchor" id="overview_ipc_examples"></a>
Examples</h1>
<p>See the sample programs <em>server</em> and <em>client</em> in the IPC samples directory. Run the server, then the client. This demonstrates using the Execute, Request, and Poke commands from the client, together with an Advise loop: selecting an item in the server list box causes that item to be highlighted in the client list box.</p>
<h1><a class="anchor" id="overview_ipc_dde"></a>
More DDE Details</h1>
<p>A <a class="el" href="classwx_client.html" title="A wxClient object represents the client part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxClient</a> object initiates the client part of a client-server DDE-like (Dynamic Data Exchange) conversation (available in both Windows and Unix).</p>
<p>To create a client which can communicate with a suitable server, you need to derive a class from <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> and another from <a class="el" href="classwx_client.html" title="A wxClient object represents the client part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxClient</a>. The custom <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> class will receive communications in a 'conversation' with a server. and the custom <a class="el" href="classwx_server.html" title="A wxServer object represents the server part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxServer</a> is required so that a user-overridden <a class="el" href="classwx_client.html#a71328614681f8f50de8a561b5dcfbbde" title="Called by MakeConnection(), by default this simply returns a new wxConnection object.">wxClient::OnMakeConnection</a> member can return a <a class="el" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a> of the required class, when a connection is made.</p>
<p>For example:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>MyConnection: <span class="keyword">public</span> <a class="code" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a></div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    MyConnection(<span class="keywordtype">void</span>)::<a class="code" href="classwx_connection.html" title="A wxConnection object represents the connection between a client and a server.">wxConnection</a>() { }</div>
<div class="line">    ~MyConnection(<span class="keywordtype">void</span>) { }</div>
<div class="line"></div>
<div class="line">    <span class="keywordtype">bool</span> <a class="code" href="classwx_connection.html#a9dddf8014802c9643378b9bdb34bfc48" title="Message sent to the client application when the server notifies it of a change in the data associated...">OnAdvise</a>(<span class="keyword">const</span> <a class="code" href="classwx_string.html" title="String class for passing textual data to or receiving it from wxWidgets.">wxString</a>&amp; topic, <span class="keyword">const</span> <a class="code" href="classwx_string.html" title="String class for passing textual data to or receiving it from wxWidgets.">wxString</a>&amp; item, <span class="keywordtype">char</span> *data,</div>
<div class="line">                  <span class="keywordtype">int</span> size, <a class="code" href="ipcbase_8h.html#a95e8d5059caf54ddbaabdaf8224fe3f2" title="An enumeration for formats .">wxIPCFormat</a> format)</div>
<div class="line">    {</div>
<div class="line">        <a class="code" href="group__group__funcmacro__dialog.html#ga193c64ed4802e379799cdb42de252647" title="Show a general purpose message dialog.">wxMessageBox</a>(topic, data);</div>
<div class="line">    }</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>MyClient: <span class="keyword">public</span> <a class="code" href="classwx_client.html" title="A wxClient object represents the client part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxClient</a></div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    MyClient(<span class="keywordtype">void</span>) { }</div>
<div class="line"></div>
<div class="line">    <a class="code" href="classwx_connection_base.html">wxConnectionBase</a>* <a class="code" href="classwx_client.html#a71328614681f8f50de8a561b5dcfbbde" title="Called by MakeConnection(), by default this simply returns a new wxConnection object.">OnMakeConnection</a>(<span class="keywordtype">void</span>)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">return</span> <span class="keyword">new</span> MyConnection;</div>
<div class="line">    }</div>
<div class="line">};</div>
</div><!-- fragment --><p>Here, <em>MyConnection</em> will respond to OnAdvise messages sent by the server by displaying a message box.</p>
<p>When the client application starts, it must create an instance of the derived <a class="el" href="classwx_client.html" title="A wxClient object represents the client part of a client-server DDE-like (Dynamic Data Exchange) conv...">wxClient</a>. In the following, command line arguments are used to pass the host name (the name of the machine the server is running on) and the server name (identifying the server process). Calling <a class="el" href="classwx_client.html#a6ca1cef89e834a724d34edf9872f545d" title="Tries to make a connection with a server by host (machine name under UNIX - use &#39;localhost&#39; for same ...">wxClient::MakeConnection</a> implicitly creates an instance of <em>MyConnection</em> if the request for a connection is accepted, and the client then requests an <em>Advise</em> loop from the server (an Advise loop is where the server calls the client when data has changed).</p>
<div class="fragment"><div class="line"><a class="code" href="classwx_string.html" title="String class for passing textual data to or receiving it from wxWidgets.">wxString</a> server = <span class="stringliteral">&quot;4242&quot;</span>;</div>
<div class="line"><a class="code" href="classwx_string.html" title="String class for passing textual data to or receiving it from wxWidgets.">wxString</a> hostName;</div>
<div class="line"><a class="code" href="group__group__funcmacro__networkuseros.html#ga25dd53a4a5aab23305b8e4e9c5ff730b" title="Copies the current host machine&#39;s name into the supplied buffer.">wxGetHostName</a>(hostName);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create a new client</span></div>
<div class="line">MyClient *client = <span class="keyword">new</span> MyClient;</div>
<div class="line">connection = (MyConnection *)client-&gt;MakeConnection(hostName, server, <span class="stringliteral">&quot;IPC TEST&quot;</span>);</div>
<div class="line"></div>
<div class="line"><span class="keywordflow">if</span> (!connection)</div>
<div class="line">{</div>
<div class="line">    <a class="code" href="group__group__funcmacro__dialog.html#ga193c64ed4802e379799cdb42de252647" title="Show a general purpose message dialog.">wxMessageBox</a>(<span class="stringliteral">&quot;Failed to make connection to server&quot;</span>, <span class="stringliteral">&quot;Client Demo Error&quot;</span>);</div>
<div class="line">    <span class="keywordflow">return</span> NULL;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line">connection-&gt;StartAdvise(<span class="stringliteral">&quot;Item&quot;</span>);</div>
</div><!-- fragment --> </div></div><!-- contents -->

<address class="footer">
	<small>
		Generated on Thu Nov 27 2014 13:46:42 for wxWidgets by <a href="http://www.doxygen.org/index.html" target="_new">Doxygen</a> 1.8.2
	</small>
</address>
<script src="wxwidgets.js" type="text/javascript"></script>
</div><!-- #page_container -->
</body>
</html>