]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/ipc.h
Fix broken and missing DataView interface items for Phoenix
[wxWidgets.git] / docs / doxygen / overviews / ipc.h
CommitLineData
15b6757b 1/////////////////////////////////////////////////////////////////////////////
077a2828 2// Name: ipc.h
15b6757b
FM
3// Purpose: topic overview
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
15b6757b
FM
7/////////////////////////////////////////////////////////////////////////////
8
880efa2a 9/**
36c9828f 10
880efa2a 11@page overview_ipc Interprocess Communication
077a2828 12
831e1028 13@tableofcontents
30738aae 14
077a2828
BP
15wxWidgets has a number of different classes to help with interprocess
16communication and network programming. This section only discusses one family
17of classes -- the DDE-like protocol -- but here's a list of other useful
18classes:
19
20@li wxSocketEvent, wxSocketBase, wxSocketClient, wxSocketServer - Classes for
21 the low-level TCP/IP API.
22@li wxProtocol, wxURL, wxFTP, wxHTTP - Classes for programming popular
23 Internet protocols.
24
25wxWidgets' DDE-like protocol is a high-level protocol based on Windows DDE.
26There are two implementations of this DDE-like protocol: one using real DDE
27running on Windows only, and another using TCP/IP (sockets) that runs on most
28platforms. Since the API and virtually all of the behaviour is the same apart
29from the names of the classes, you should find it easy to switch between the
30two implementations.
31
32Notice that by including @c @<wx/ipc.h@> you may define convenient synonyms for
33the IPC classes: wxServer for either wxDDEServer or wxTCPServer depending on
34whether DDE-based or socket-based implementation is used and the same thing for
35wxClient and wxConnection.
36
37By default, the DDE implementation is used under Windows. DDE works within one
38computer only. If you want to use IPC between different workstations you should
39define @c wxUSE_DDE_FOR_IPC as 0 before including this header -- this will
40force using TCP/IP implementation even under Windows.
41
42The following description refers to wxWidgets, but remember that the equivalent
43wxTCP* and wxDDE* classes can be used in much the same way.
44
45Three classes are central to the DDE-like API:
46
47@li wxClient - This represents the client application, and is used only within
48 a client program.
49@li wxServer - This represents the server application, and is used only within
50 a server program.
51@li wxConnection - This represents the connection from the client to the
52 server. Both the client and the server use an instance of this class, one
53 per connection. Most DDE transactions operate on this object.
54
55Messages between applications are usually identified by three variables:
56connection object, topic name and item name. A data string is a fourth element
57of some messages. To create a connection (a conversation in Windows parlance),
58the client application uses wxClient::MakeConnection to send a message to the
59server object, with a string service name to identify the server and a topic
60name to identify the topic for the duration of the connection. Under Unix, the
61service name may be either an integer port identifier in which case an Internet
62domain socket will be used for the communications or a valid file name (which
63shouldn't exist and will be deleted afterwards) in which case a Unix domain
64socket is created.
65
66<b>SECURITY NOTE:</b> Using Internet domain sockets is extremely insecure for
67IPC as there is absolutely no access control for them, use Unix domain sockets
68whenever possible!
69
70The server then responds and either vetoes the connection or allows it. If
71allowed, both the server and client objects create wxConnection objects which
72persist until the connection is closed. The connection object is then used for
73sending and receiving subsequent messages between client and server -
74overriding virtual functions in your class derived from wxConnection allows you
75to handle the DDE messages.
76
77To create a working server, the programmer must:
78
79@li Derive a class from wxConnection, providing handlers for various messages
80 sent to the server side of a wxConnection (e.g. OnExecute, OnRequest,
81 OnPoke). Only the handlers actually required by the application need to be
82 overridden.
83@li Derive a class from wxServer, overriding OnAcceptConnection to accept or
84 reject a connection on the basis of the topic argument. This member must
85 create and return an instance of the derived connection class if the
86 connection is accepted.
87@li Create an instance of your server object and call Create to activate it,
88 giving it a service name.
89
90To create a working client, the programmer must:
91
92@li Derive a class from wxConnection, providing handlers for various messages
93 sent to the client side of a wxConnection (e.g. OnAdvise). Only the
94 handlers actually required by the application need to be overridden.
95@li Derive a class from wxClient, overriding OnMakeConnection to create and
96 return an instance of the derived connection class.
97@li Create an instance of your client object.
98@li When appropriate, create a new connection using wxClient::MakeConnection,
99 with arguments host name (processed in Unix only, use 'localhost' for local
100 computer), service name, and topic name for this connection. The client
101 object will call OnMakeConnection to create a connection object of the
102 derived class if the connection is successful.
103@li Use the wxConnection member functions to send messages to the server.
104
105
106@section overview_ipc_datatransfer Data Transfer
107
108These are the ways that data can be transferred from one application to
109another. These are methods of wxConnection.
110
111@li <b>Execute:</b> the client calls the server with a data string representing
112 a command to be executed. This succeeds or fails, depending on the server's
113 willingness to answer. If the client wants to find the result of the
114 Execute command other than success or failure, it has to explicitly call
115 Request.
116@li <b>Request:</b> the client asks the server for a particular data string
117 associated with a given item string. If the server is unwilling to reply,
118 the return value is @NULL. Otherwise, the return value is a string
119 (actually a pointer to the connection buffer, so it should not be
120 deallocated by the application).
121@li <b>Poke:</b> The client sends a data string associated with an item string
122 directly to the server. This succeeds or fails.
123@li <b>Advise:</b> The client asks to be advised of any change in data
124 associated with a particular item. If the server agrees, the server will
125 send an OnAdvise message to the client along with the item and data.
126
127The default data type is wxCF_TEXT (ASCII text), and the default data size is
128the length of the null-terminated string. Windows-specific data types could
129also be used on the PC.
130
131
132@section overview_ipc_examples Examples
133
134See the sample programs @e server and @e client in the IPC samples directory.
135Run the server, then the client. This demonstrates using the Execute, Request,
136and Poke commands from the client, together with an Advise loop: selecting an
137item in the server list box causes that item to be highlighted in the client
138list box.
139
140
141@section overview_ipc_dde More DDE Details
142
143A wxClient object initiates the client part of a client-server DDE-like
144(Dynamic Data Exchange) conversation (available in both Windows and Unix).
145
146To create a client which can communicate with a suitable server, you need to
147derive a class from wxConnection and another from wxClient. The custom
148wxConnection class will receive communications in a 'conversation' with a
149server. and the custom wxServer is required so that a user-overridden
150wxClient::OnMakeConnection member can return a wxConnection of the required
151class, when a connection is made.
152
153For example:
154
155@code
156class MyConnection: public wxConnection
157{
158public:
159 MyConnection(void)::wxConnection() { }
160 ~MyConnection(void) { }
161
162 bool OnAdvise(const wxString& topic, const wxString& item, char *data,
163 int size, wxIPCFormat format)
164 {
165 wxMessageBox(topic, data);
166 }
167};
168
169class MyClient: public wxClient
170{
171public:
172 MyClient(void) { }
173
174 wxConnectionBase* OnMakeConnection(void)
175 {
176 return new MyConnection;
177 }
178};
179@endcode
180
181Here, @e MyConnection will respond to OnAdvise messages sent by the server by
182displaying a message box.
183
184When the client application starts, it must create an instance of the derived
185wxClient. In the following, command line arguments are used to pass the host
186name (the name of the machine the server is running on) and the server name
187(identifying the server process). Calling wxClient::MakeConnection implicitly
188creates an instance of @e MyConnection if the request for a connection is
189accepted, and the client then requests an @e Advise loop from the server (an
190Advise loop is where the server calls the client when data has changed).
191
192@code
193wxString server = "4242";
194wxString hostName;
195wxGetHostName(hostName);
196
197// Create a new client
198MyClient *client = new MyClient;
199connection = (MyConnection *)client->MakeConnection(hostName, server, "IPC TEST");
200
201if (!connection)
202{
203 wxMessageBox("Failed to make connection to server", "Client Demo Error");
204 return NULL;
205}
206
207connection->StartAdvise("Item");
208@endcode
209
210*/