ipc.h 8.8 KB

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