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