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