]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tipc.tex
updated tags info
[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
23wxWindows has a high-level protocol based on Windows DDE.
24There are two implementations of this DDE-like protocol:
25one using real DDE running on Windows only, and another using TCP/IP (sockets) that runs
26on most platforms. Since the API is the same apart from the names of the classes, you
27should find it easy to switch between the two implementations.
28
29The following description refers to 'DDE' but remember that the equivalent wxTCP... classes
30can be used in much the same way.
31
32Three classes are central to the DDE API:
a660d684
KB
33
34\begin{enumerate}\itemsep=0pt
35\item wxDDEClient. This represents the client application, and is used
36only within a client program.
37\item wxDDEServer. This represents the server application, and is used
38only within a server program.
39\item wxDDEConnection. This represents the connection from the current
40client or server to the other application (server or client), and can be used
41in both server and client programs. Most DDE
42transactions operate on this object.
43\end{enumerate}
44
45Messages between applications are usually identified by three variables:
46connection object, topic name and item name. A data string is a fourth
47element of some messages. To create a connection (a conversation in
48Windows parlance), the client application sends the message
49MakeConnection to the client object, with a string service name to
50identify the server and a topic name to identify the topic for the
e2a6f233 51duration of the connection. Under Unix, the service name must contain an
a660d684
KB
52integer port identifier.
53
f6bcfd97 54The server then responds and either vetoes the connection or allows it.
a660d684
KB
55If allowed, a connection object is created which persists until the
56connection is closed. The connection object is then used for subsequent
57messages between client and server.
58
59To create a working server, the programmer must:
60
61\begin{enumerate}\itemsep=0pt
62\item Derive a class from wxDDEServer.
63\item Override the handler OnAcceptConnection for accepting or rejecting a connection,
64on the basis of the topic argument. This member must create and return a connection
65object if the connection is accepted.
66\item Create an instance of your server object, and call Create to
67activate it, giving it a service name.
68\item Derive a class from wxDDEConnection.
69\item Provide handlers for various messages that are sent to the server
70side of a wxDDEConnection.
71\end{enumerate}
72
73To create a working client, the programmer must:
74
75\begin{enumerate}\itemsep=0pt
76\item Derive a class from wxDDEClient.
77\item Override the handler OnMakeConnection to create and return
78an appropriate connection object.
79\item Create an instance of your client object.
80\item Derive a class from wxDDEConnection.
81\item Provide handlers for various messages that are sent to the client
82side of a wxDDEConnection.
83\item When appropriate, create a new connection by sending a MakeConnection
e2a6f233 84message to the client object, with arguments host name (processed in Unix only),
a660d684
KB
85service name, and topic name for this connection. The client object will call OnMakeConnection
86to create a connection object of the desired type.
87\item Use the wxDDEConnection member functions to send messages to the server.
88\end{enumerate}
89
90\subsection{Data transfer}
91
92These are the ways that data can be transferred from one application to
93another.
94
95\begin{itemize}\itemsep=0pt
96\item {\bf Execute:} the client calls the server with a data string representing
97a command to be executed. This succeeds or fails, depending on the
98server's willingness to answer. If the client wants to find the result
99of the Execute command other than success or failure, it has to explicitly
100call Request.
101\item {\bf Request:} the client asks the server for a particular data string
102associated with a given item string. If the server is unwilling to
103reply, the return value is NULL. Otherwise, the return value is a string
104(actually a pointer to the connection buffer, so it should not be
105deallocated by the application).
106\item {\bf Poke:} The client sends a data string associated with an item
107string directly to the server. This succeeds or fails.
108\item {\bf Advise:} The client asks to be advised of any change in data
109associated with a particular item. If the server agrees, the server will
110send an OnAdvise message to the client along with the item and data.
111\end{itemize}
112
113The default data type is wxCF\_TEXT (ASCII text), and the default data
114size is the length of the null-terminated string. Windows-specific data
115types could also be used on the PC.
116
117\subsection{Examples}
118
119See the sample programs {\it server}\/ and {\it client}\/ in the IPC
120samples directory. Run the server, then the client. This demonstrates
121using the Execute, Request, and Poke commands from the client, together
122with an Advise loop: selecting an item in the server list box causes
123that item to be highlighted in the client list box.
124
a660d684
KB
125\subsection{More DDE details}
126
127A wxDDEClient object represents the client part of a client-server DDE
128(Dynamic Data Exchange) conversation (available in both
e2a6f233 129Windows and Unix).
a660d684
KB
130
131To create a client which can communicate with a suitable server,
132you need to derive a class from wxDDEConnection and another from wxDDEClient.
133The custom wxDDEConnection class will intercept communications in
134a `conversation' with a server, and the custom wxDDEServer is required
f6bcfd97 135so that a user-overridden \helpref{wxDDEClient::OnMakeConnection}{wxddeclientonmakeconnection} member can return
a660d684
KB
136a wxDDEConnection of the required class, when a connection is made.
137
138For example:
139
140\begin{verbatim}
141class MyConnection: public wxDDEConnection
142{
143 public:
144 MyConnection(void)::wxDDEConnection(ipc_buffer, 3999) {}
145 ~MyConnection(void) { }
e2a6f233 146 bool OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
a660d684
KB
147 { wxMessageBox(topic, data); }
148};
149
150class MyClient: public wxDDEClient
151{
152 public:
153 MyClient(void) {}
e2a6f233 154 wxConnectionBase *OnMakeConnection(void) { return new MyConnection; }
a660d684
KB
155};
156
157\end{verbatim}
158
159Here, {\bf MyConnection} will respond to \helpref{OnAdvise}{wxddeconnectiononadvise} messages sent
160by the server.
161
e2a6f233 162When the client application starts, it must create an instance of the derived wxDDEClient. In the following, command line
a660d684
KB
163arguments are used to pass the host name (the name of the machine the server is running
164on) and the server name (identifying the server process). Calling \helpref{wxDDEClient::MakeConnection}{wxddeclientmakeconnection}\rtfsp
165implicitly creates an instance of {\bf MyConnection} if the request for a
166connection is accepted, and the client then requests an {\it Advise} loop
167from the server, where the server calls the client when data has changed.
168
169\begin{verbatim}
e2a6f233
JS
170 wxString server = "4242";
171 wxString hostName;
172 wxGetHostName(hostName);
a660d684
KB
173
174 // Create a new client
175 MyClient *client = new MyClient;
e2a6f233 176 connection = (MyConnection *)client->MakeConnection(hostName, server, "IPC TEST");
a660d684 177
e2a6f233 178 if (!connection)
a660d684
KB
179 {
180 wxMessageBox("Failed to make connection to server", "Client Demo Error");
181 return NULL;
182 }
e2a6f233 183 connection->StartAdvise("Item");
a660d684
KB
184\end{verbatim}
185
e2a6f233
JS
186Note that it is no longer necessary to call wxDDEInitialize or wxDDECleanUp, since
187wxWindows will do this itself if necessary.
a660d684 188