]> git.saurik.com Git - wxWidgets.git/blob - interface/ipc.h
b878a1912d46f13ac4c3c2009cd1af6361ef319b
[wxWidgets.git] / interface / ipc.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ipc.h
3 // Purpose: documentation for wxConnection class
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxConnection
11 @wxheader{ipc.h}
12
13 A wxConnection object represents the connection between a client
14 and a server. It is created by making a connection using a
15 wxClient object, or by the acceptance of a
16 connection by a wxServer object. The
17 bulk of a DDE-like (Dynamic Data Exchange) conversation is
18 controlled by calling members in a @b wxConnection object or
19 by overriding its members. The actual DDE-based implementation
20 using wxDDEConnection is available on Windows only, but a
21 platform-independent, socket-based version of this API is
22 available using wxTCPConnection, which has the same API.
23
24 An application should normally derive a new connection class from
25 wxConnection, in order to override the communication event
26 handlers to do something interesting.
27
28 @library{wxbase}
29 @category{FIXME}
30
31 @seealso
32 wxClient, wxServer, @ref overview_ipcoverview "Interprocess communications
33 overview"
34 */
35 class wxConnection : public wxObject
36 {
37 public:
38 //@{
39 /**
40 Constructs a connection object. If no user-defined connection
41 object is to be derived from wxConnection, then the constructor
42 should not be called directly, since the default connection
43 object will be provided on requesting (or accepting) a
44 connection. However, if the user defines his or her own derived
45 connection object, the wxServer::OnAcceptConnection
46 and/or wxClient::OnMakeConnection
47 members should be replaced by functions which construct the new
48 connection object.
49
50 If the arguments of the wxConnection constructor are void then
51 the wxConnection object manages its own connection buffer,
52 allocating memory as needed. A programmer-supplied buffer cannot
53 be increased if necessary, and the program will assert if it is
54 not large enough. The programmer-supplied buffer is included
55 mainly for backwards compatibility.
56 */
57 wxConnection();
58 wxConnection(void* buffer, size_t size);
59 //@}
60
61 //@{
62 /**
63 Called by the server application to advise the client of a change
64 in the data associated with the given item. Causes the client
65 connection's OnAdvise() member
66 to be called. Returns @true if successful.
67 */
68 bool Advise(const wxString& item, const void* data, size_t size,
69 wxIPCFormat format = wxIPC_PRIVATE);
70 bool Advise(const wxString& item, const char* data);
71 bool Advise(const wxString& item, const wchar_t* data);
72 bool Advise(const wxString& item, const wxString data);
73 //@}
74
75 /**
76 Called by the client or server application to disconnect from the
77 other program; it causes the OnDisconnect()
78 message to be sent to the corresponding connection object in the
79 other program. Returns @true if successful or already disconnected.
80 The application that calls @b Disconnect must explicitly delete
81 its side of the connection.
82 */
83 bool Disconnect();
84
85 //@{
86 /**
87 Called by the client application to execute a command on the
88 server. Can also be used to transfer arbitrary data to the server
89 (similar to Poke() in
90 that respect). Causes the server connection's OnExec()
91 member to be called. Returns @true if successful.
92 */
93 bool Execute(const void* data, size_t size,
94 wxIPCFormat format = wxIPC_PRIVATE);
95 bool Execute(const char* data);
96 bool Execute(const wchar_t* data);
97 bool Execute(const wxString data);
98 //@}
99
100 /**
101 Message sent to the client application when the server notifies
102 it of a change in the data associated with the given item, using
103 Advise().
104 */
105 virtual bool OnAdvise(const wxString& topic,
106 const wxString& item,
107 const void* data,
108 size_t size,
109 wxIPCFormat format);
110
111 /**
112 Message sent to the client or server application when the other
113 application notifies it to end the connection. The default
114 behaviour is to delete the connection object and return @true, so
115 applications should generally override @b OnDisconnect
116 (finally calling the inherited method as well) so that they know
117 the connection object is no longer available.
118 */
119 virtual bool OnDisconnect();
120
121 /**
122 Message sent to the server application when the client notifies
123 it to execute the given data, using Execute().
124 Note that there is no item associated with this message.
125 */
126 virtual bool OnExec(const wxString& topic, const wxString& data);
127
128 /**
129 Message sent to the server application when the client notifies it to
130 accept the given data.
131 */
132 virtual bool OnPoke(const wxString& topic, const wxString& item,
133 const void* data,
134 size_t size,
135 wxIPCFormat format);
136
137 /**
138 Message sent to the server application when the client calls
139 Request(). The
140 server's OnRequest() method
141 should respond by returning a character string, or @NULL to
142 indicate no data, and setting *size. The character string must of
143 course persist after the call returns.
144 */
145 virtual const void* OnRequest(const wxString& topic,
146 const wxString& item,
147 size_t * size,
148 wxIPCFormat format);
149
150 /**
151 Message sent to the server application by the client, when the client
152 wishes to start an 'advise loop' for the given topic and item. The
153 server can refuse to participate by returning @false.
154 */
155 virtual bool OnStartAdvise(const wxString& topic,
156 const wxString& item);
157
158 /**
159 Message sent to the server application by the client, when the client
160 wishes to stop an 'advise loop' for the given topic and item. The
161 server can refuse to stop the advise loop by returning @false, although
162 this doesn't have much meaning in practice.
163 */
164 virtual bool OnStopAdvise(const wxString& topic,
165 const wxString& item);
166
167 //@{
168 /**
169 Called by the client application to poke data into the server.
170 Can be used to transfer arbitrary data to the server. Causes the
171 server connection's OnPoke() member to
172 be called. If size is -1 the size is computed from the string
173 length of data.
174
175 Returns @true if successful.
176 */
177 bool Poke(const wxString& item, const void* data, size_t size,
178 wxIPCFormat format = wxIPC_PRIVATE);
179 bool Poke(const wxString& item, const char* data);
180 bool Poke(const wxString& item, const wchar_t* data);
181 bool Poke(const wxString& item, const wxString data);
182 //@}
183
184 /**
185 Called by the client application to request data from the server.
186 Causes the server connection's OnRequest()
187 member to be called. Size may be @NULL or a pointer to a variable
188 to receive the size of the requested item.
189
190 Returns a character string (actually a pointer to the
191 connection's buffer) if successful, @NULL otherwise. This buffer
192 does not need to be deleted.
193 */
194 const void* Request(const wxString& item, size_t * size,
195 wxIPCFormat format = wxIPC_TEXT);
196
197 /**
198 Called by the client application to ask if an advise loop can be
199 started with the server. Causes the server connection's
200 OnStartAdvise()
201 member to be called. Returns @true if the server okays it, @false
202 otherwise.
203 */
204 bool StartAdvise(const wxString& item);
205
206 /**
207 Called by the client application to ask if an advise loop can be
208 stopped. Causes the server connection's OnStopAdvise()
209 member to be called. Returns @true if the server okays it, @false
210 otherwise.
211 */
212 bool StopAdvise(const wxString& item);
213 };
214
215
216 /**
217 @class wxClient
218 @wxheader{ipc.h}
219
220 A wxClient object represents the client part of a client-server
221 DDE-like (Dynamic Data Exchange) conversation. The actual
222 DDE-based implementation using wxDDEClient is available on Windows
223 only, but a platform-independent, socket-based version of this
224 API is available using wxTCPClient, which has the same API.
225
226 To create a client which can communicate with a suitable server,
227 you need to derive a class from wxConnection and another from
228 wxClient. The custom wxConnection class will intercept
229 communications in a 'conversation' with a server, and the custom
230 wxClient is required so that a user-overridden
231 wxClient::OnMakeConnection
232 member can return a wxConnection of the required class, when a
233 connection is made. Look at the IPC sample and the
234 @ref overview_ipcoverview "Interprocess communications overview" for
235 an example of how to do this.
236
237 @library{wxbase}
238 @category{FIXME}
239
240 @seealso
241 wxServer, wxConnection, @ref overview_ipcoverview "Interprocess communications
242 overview"
243 */
244 class wxClient : public wxObject
245 {
246 public:
247 /**
248 Constructs a client object.
249 */
250 wxClient();
251
252 /**
253 Tries to make a connection with a server by host (machine name
254 under UNIX - use 'localhost' for same machine; ignored when using
255 native DDE in Windows), service name and topic string. If the
256 server allows a connection, a wxConnection object will be
257 returned. The type of wxConnection returned can be altered by
258 overriding the
259 OnMakeConnection()
260 member to return your own derived connection object.
261
262 Under Unix, the service name may be either an integer port
263 identifier in which case an Internet domain socket will be used
264 for the communications, or a valid file name (which shouldn't
265 exist and will be deleted afterwards) in which case a Unix domain
266 socket is created.
267
268 @b SECURITY NOTE: Using Internet domain sockets if extremely
269 insecure for IPC as there is absolutely no access control for
270 them, use Unix domain sockets whenever possible!
271 */
272 wxConnectionBase * MakeConnection(const wxString& host,
273 const wxString& service,
274 const wxString& topic);
275
276 /**
277 Called by MakeConnection(), by
278 default this simply returns a new wxConnection object. Override
279 this method to return a wxConnection descendant customised for the
280 application.
281
282 The advantage of deriving your own connection class is that it
283 will enable you to intercept messages initiated by the server,
284 such as wxConnection::OnAdvise. You
285 may also want to store application-specific data in instances of
286 the new class.
287 */
288 wxConnectionBase * OnMakeConnection();
289
290 /**
291 Returns @true if this is a valid host name, @false otherwise. This always
292 returns @true under MS Windows.
293 */
294 bool ValidHost(const wxString& host);
295 };
296
297
298 /**
299 @class wxServer
300 @wxheader{ipc.h}
301
302 A wxServer object represents the server part of a client-server
303 DDE-like (Dynamic Data Exchange) conversation. The actual
304 DDE-based implementation using wxDDEServer is available on Windows
305 only, but a platform-independent, socket-based version of this
306 API is available using wxTCPServer, which has the same API.
307
308 To create a server which can communicate with a suitable client,
309 you need to derive a class from wxConnection and another from
310 wxServer. The custom wxConnection class will intercept
311 communications in a 'conversation' with a client, and the custom
312 wxServer is required so that a user-overridden wxServer::OnAcceptConnection
313 member can return a wxConnection of the required class, when a
314 connection is made. Look at the IPC sample and the @ref overview_ipcoverview
315 "Interprocess communications overview" for
316 an example of how to do this.
317
318 @library{wxbase}
319 @category{FIXME}
320
321 @seealso
322 wxClient, wxConnection, IPC, overview
323 */
324 class wxServer
325 {
326 public:
327 /**
328 Constructs a server object.
329 */
330 wxServer();
331
332 /**
333 Registers the server using the given service name. Under Unix,
334 the service name may be either an integer port identifier in
335 which case an Internet domain socket will be used for the
336 communications, or a valid file name (which shouldn't exist and
337 will be deleted afterwards) in which case a Unix domain socket is
338 created. @false is returned if the call failed (for example, the
339 port number is already in use).
340 */
341 bool Create(const wxString& service);
342
343 /**
344 When a client calls @b MakeConnection, the server receives the
345 message and this member is called. The application should derive a
346 member to intercept this message and return a connection object of
347 either the standard wxConnection type, or (more likely) of a
348 user-derived type.
349
350 If the topic is @b STDIO, the application may wish to refuse the
351 connection. Under UNIX, when a server is created the
352 OnAcceptConnection message is always sent for standard input and
353 output, but in the context of DDE messages it doesn't make a lot
354 of sense.
355 */
356 virtual wxConnectionBase * OnAcceptConnection(const wxString& topic);
357 };