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