sckipc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: sckipc.h
  3. // Purpose: interface of wxTCPServer
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. See wxTCPConnection.
  9. */
  10. enum wxIPCFormat
  11. {
  12. wxIPC_INVALID = 0,
  13. wxIPC_TEXT = 1, /* CF_TEXT */
  14. wxIPC_BITMAP = 2, /* CF_BITMAP */
  15. wxIPC_METAFILE = 3, /* CF_METAFILEPICT */
  16. wxIPC_SYLK = 4,
  17. wxIPC_DIF = 5,
  18. wxIPC_TIFF = 6,
  19. wxIPC_OEMTEXT = 7, /* CF_OEMTEXT */
  20. wxIPC_DIB = 8, /* CF_DIB */
  21. wxIPC_PALETTE = 9,
  22. wxIPC_PENDATA = 10,
  23. wxIPC_RIFF = 11,
  24. wxIPC_WAVE = 12,
  25. wxIPC_UTF16TEXT = 13, /* CF_UNICODE */
  26. wxIPC_ENHMETAFILE = 14,
  27. wxIPC_FILENAME = 15, /* CF_HDROP */
  28. wxIPC_LOCALE = 16,
  29. wxIPC_UTF8TEXT = 17,
  30. wxIPC_UTF32TEXT = 18,
  31. wxIPC_UNICODETEXT,
  32. wxIPC_PRIVATE = 20
  33. };
  34. /**
  35. @class wxTCPServer
  36. A wxTCPServer object represents the server part of a client-server conversation.
  37. It emulates a DDE-style protocol, but uses TCP/IP which is available on most
  38. platforms.
  39. A DDE-based implementation for Windows is available using wxDDEServer.
  40. @library{wxnet}
  41. @category{net}
  42. @see wxTCPClient, wxTCPConnection, @ref overview_ipc
  43. */
  44. class wxTCPServer : public wxObject
  45. {
  46. public:
  47. /**
  48. Constructs a server object.
  49. */
  50. wxTCPServer();
  51. /**
  52. Registers the server using the given service name.
  53. Under Unix, the string must contain an integer id which is used as an
  54. Internet port number. @false is returned if the call failed
  55. (for example, the port number is already in use).
  56. */
  57. virtual bool Create(const wxString& service);
  58. /**
  59. When a client calls @b MakeConnection, the server receives the
  60. message and this member is called.
  61. The application should derive a member to intercept this message and
  62. return a connection object of either the standard wxTCPConnection type,
  63. or of a user-derived type.
  64. If the topic is "STDIO", the application may wish to refuse the connection.
  65. Under Unix, when a server is created the OnAcceptConnection message is
  66. always sent for standard input and output.
  67. */
  68. virtual wxConnectionBase* OnAcceptConnection(const wxString& topic);
  69. };
  70. /**
  71. @class wxTCPClient
  72. A wxTCPClient object represents the client part of a client-server conversation.
  73. It emulates a DDE-style protocol, but uses TCP/IP which is available on most
  74. platforms.
  75. A DDE-based implementation for Windows is available using wxDDEClient.
  76. To create a client which can communicate with a suitable server, you need
  77. to derive a class from wxTCPConnection and another from wxTCPClient.
  78. The custom wxTCPConnection class will intercept communications in
  79. a 'conversation' with a server, and the custom wxTCPServer is required
  80. so that a user-overridden wxTCPClient::OnMakeConnection() member can return
  81. a wxTCPConnection of the required class, when a connection is made.
  82. @library{wxnet}
  83. @category{net}
  84. @see wxTCPServer, wxTCPConnection, @ref overview_ipc
  85. */
  86. class wxTCPClient : public wxObject
  87. {
  88. public:
  89. /**
  90. Constructs a client object.
  91. */
  92. wxTCPClient();
  93. /**
  94. Tries to make a connection with a server specified by the host
  95. (a machine name under Unix), service name (must contain an integer
  96. port number under Unix), and a topic string.
  97. If the server allows a connection, a wxTCPConnection object will be returned.
  98. The type of wxTCPConnection returned can be altered by overriding
  99. the OnMakeConnection() member to return your own derived connection object.
  100. */
  101. virtual wxConnectionBase* MakeConnection(const wxString& host,
  102. const wxString& service,
  103. const wxString& topic);
  104. /**
  105. The type of wxTCPConnection returned from a MakeConnection() call can
  106. be altered by deriving the @b OnMakeConnection member to return your
  107. own derived connection object. By default, a wxTCPConnection
  108. object is returned.
  109. The advantage of deriving your own connection class is that it will
  110. enable you to intercept messages initiated by the server, such
  111. as wxTCPConnection::OnAdvise(). You may also want to store
  112. application-specific data in instances of the new class.
  113. */
  114. virtual wxConnectionBase* OnMakeConnection();
  115. /**
  116. Returns @true if this is a valid host name, @false otherwise.
  117. */
  118. virtual bool ValidHost(const wxString& host);
  119. };
  120. /**
  121. @class wxTCPConnection
  122. A wxTCPClient object represents the connection between a client and a server.
  123. It emulates a DDE-style protocol, but uses TCP/IP which is available on most
  124. platforms.
  125. A DDE-based implementation for Windows is available using wxDDEConnection.
  126. A wxTCPConnection object can be created by making a connection using a
  127. wxTCPClient object, or by the acceptance of a connection by a wxTCPServer object.
  128. The bulk of a conversation is controlled by calling members in a
  129. @b wxTCPConnection object or by overriding its members.
  130. An application should normally derive a new connection class from
  131. wxTCPConnection, in order to override the communication event handlers
  132. to do something interesting.
  133. @library{wxnet}
  134. @category{net}
  135. @see wxTCPClient, wxTCPServer, @ref overview_ipc
  136. */
  137. class wxTCPConnection : public wxObject
  138. {
  139. public:
  140. //@{
  141. /**
  142. Constructs a connection object.
  143. If no user-defined connection object is to be derived from wxTCPConnection,
  144. then the constructor should not be called directly, since the default
  145. connection object will be provided on requesting (or accepting) a connection.
  146. However, if the user defines his or her own derived connection object,
  147. the wxTCPServer::OnAcceptConnection and/or wxTCPClient::OnMakeConnection
  148. members should be replaced by functions which construct the new connection object.
  149. If the arguments of the wxTCPConnection constructor are void, then a default
  150. buffer is associated with the connection. Otherwise, the programmer must
  151. provide a buffer and size of the buffer for the connection object to use in
  152. transactions.
  153. */
  154. wxTCPConnection();
  155. wxTCPConnection(void* buffer, size_t size);
  156. //@}
  157. //@{
  158. /**
  159. Called by the server application to advise the client of a change in
  160. the data associated with the given item.
  161. Causes the client connection's OnAdvise() member to be called.
  162. Returns @true if successful.
  163. */
  164. bool Advise(const wxString& item, const void* data, size_t size,
  165. wxIPCFormat format = wxIPC_PRIVATE);
  166. bool Advise(const wxString& item, const char* data);
  167. bool Advise(const wxString& item, const wchar_t* data);
  168. bool Advise(const wxString& item, const wxString data);
  169. //@}
  170. /**
  171. Called by the client or server application to disconnect from the other
  172. program; it causes the OnDisconnect() message to be sent to the
  173. corresponding connection object in the other program.
  174. The default behaviour of @b OnDisconnect is to delete the
  175. connection, but the calling application must explicitly delete its
  176. side of the connection having called @b Disconnect.
  177. Returns @true if successful.
  178. */
  179. virtual bool Disconnect();
  180. //@{
  181. /**
  182. Called by the client application to execute a command on the server.
  183. Can also be used to transfer arbitrary data to the server (similar
  184. to Poke() in that respect). Causes the server connection's OnExecute()
  185. member to be called.
  186. Returns @true if successful.
  187. */
  188. bool Execute(const void* data, size_t size,
  189. wxIPCFormat format = wxIPC_PRIVATE);
  190. bool Execute(const char* data);
  191. bool Execute(const wchar_t* data);
  192. bool Execute(const wxString data);
  193. //@}
  194. /**
  195. Message sent to the client application when the server notifies it of a
  196. change in the data associated with the given item.
  197. */
  198. virtual bool OnAdvise(const wxString& topic,
  199. const wxString& item,
  200. const void* data,
  201. size_t size,
  202. wxIPCFormat format);
  203. /**
  204. Message sent to the client or server application when the other
  205. application notifies it to delete the connection.
  206. Default behaviour is to delete the connection object.
  207. */
  208. virtual bool OnDisconnect();
  209. /**
  210. Message sent to the server application when the client notifies it to
  211. execute the given data.
  212. Note that there is no item associated with this message.
  213. */
  214. virtual bool OnExecute(const wxString& topic, const void* data,
  215. size_t size,
  216. wxIPCFormat format);
  217. /**
  218. Message sent to the server application when the client notifies it to
  219. accept the given data.
  220. */
  221. virtual bool OnPoke(const wxString& topic, const wxString& item,
  222. const void* data,
  223. size_t size,
  224. wxIPCFormat format);
  225. /**
  226. Message sent to the server application when the client calls Request().
  227. The server should respond by returning a character string from @b OnRequest,
  228. or @NULL to indicate no data.
  229. */
  230. virtual const void* OnRequest(const wxString& topic,
  231. const wxString& item,
  232. size_t* size,
  233. wxIPCFormat format);
  234. /**
  235. Message sent to the server application by the client, when the client
  236. wishes to start an 'advise loop' for the given topic and item.
  237. The server can refuse to participate by returning @false.
  238. */
  239. virtual bool OnStartAdvise(const wxString& topic,
  240. const wxString& item);
  241. /**
  242. Message sent to the server application by the client, when the client
  243. wishes to stop an 'advise loop' for the given topic and item.
  244. The server can refuse to stop the advise loop by returning @false, although
  245. this doesn't have much meaning in practice.
  246. */
  247. virtual bool OnStopAdvise(const wxString& topic,
  248. const wxString& item);
  249. //@{
  250. /**
  251. Called by the client application to poke data into the server.
  252. Can be used to transfer arbitrary data to the server. Causes the server
  253. connection's OnPoke() member to be called. Returns @true if successful.
  254. */
  255. bool Poke(const wxString& item, const void* data, size_t size,
  256. wxIPCFormat format = wxIPC_PRIVATE);
  257. bool Poke(const wxString& item, const char* data);
  258. bool Poke(const wxString& item, const wchar_t* data);
  259. bool Poke(const wxString& item, const wxString data);
  260. //@}
  261. /**
  262. Called by the client application to request data from the server.
  263. Causes the server connection's OnRequest() member to be called.
  264. Returns a character string (actually a pointer to the connection's buffer) if
  265. successful, @NULL otherwise.
  266. */
  267. virtual const void* Request(const wxString& item, size_t* size = 0,
  268. wxIPCFormat format = wxIPC_TEXT);
  269. /**
  270. Called by the client application to ask if an advise loop can be started
  271. with the server.
  272. Causes the server connection's OnStartAdvise() member to be called.
  273. Returns @true if the server okays it, @false otherwise.
  274. */
  275. virtual bool StartAdvise(const wxString& item);
  276. /**
  277. Called by the client application to ask if an advise loop can be stopped.
  278. Causes the server connection's OnStopAdvise() member to be called.
  279. Returns @true if the server okays it, @false otherwise.
  280. */
  281. virtual bool StopAdvise(const wxString& item);
  282. };