PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/plug_wx/interface/wx/ipc.h

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