]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tipc.tex
SWIGged updates for wxGTK
[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
0dbfd66d
VZ
51duration of the connection. Under Unix, the service name may be either an
52integer port identifier in which case an Internet domain socket will be used
53for the communications or a valid file name (which shouldn't exist and will be
54deleted afterwards) in which case a Unix domain socket is created.
55
56{\bf SECURITY NOTE:} Using Internet domain sockets if extremely insecure for
57IPC as there is absolutely no access control for them, use Unix domain sockets
58whenever possible!
a660d684 59
f6bcfd97 60The server then responds and either vetoes the connection or allows it.
a660d684
KB
61If allowed, a connection object is created which persists until the
62connection is closed. The connection object is then used for subsequent
63messages between client and server.
64
65To create a working server, the programmer must:
66
67\begin{enumerate}\itemsep=0pt
68\item Derive a class from wxDDEServer.
69\item Override the handler OnAcceptConnection for accepting or rejecting a connection,
70on the basis of the topic argument. This member must create and return a connection
71object if the connection is accepted.
72\item Create an instance of your server object, and call Create to
73activate it, giving it a service name.
74\item Derive a class from wxDDEConnection.
75\item Provide handlers for various messages that are sent to the server
76side of a wxDDEConnection.
77\end{enumerate}
78
79To create a working client, the programmer must:
80
81\begin{enumerate}\itemsep=0pt
82\item Derive a class from wxDDEClient.
83\item Override the handler OnMakeConnection to create and return
84an appropriate connection object.
85\item Create an instance of your client object.
86\item Derive a class from wxDDEConnection.
87\item Provide handlers for various messages that are sent to the client
88side of a wxDDEConnection.
89\item When appropriate, create a new connection by sending a MakeConnection
e2a6f233 90message to the client object, with arguments host name (processed in Unix only),
a660d684
KB
91service name, and topic name for this connection. The client object will call OnMakeConnection
92to create a connection object of the desired type.
93\item Use the wxDDEConnection member functions to send messages to the server.
94\end{enumerate}
95
96\subsection{Data transfer}
97
98These are the ways that data can be transferred from one application to
99another.
100
101\begin{itemize}\itemsep=0pt
102\item {\bf Execute:} the client calls the server with a data string representing
103a command to be executed. This succeeds or fails, depending on the
104server's willingness to answer. If the client wants to find the result
105of the Execute command other than success or failure, it has to explicitly
106call Request.
107\item {\bf Request:} the client asks the server for a particular data string
108associated with a given item string. If the server is unwilling to
109reply, the return value is NULL. Otherwise, the return value is a string
110(actually a pointer to the connection buffer, so it should not be
111deallocated by the application).
112\item {\bf Poke:} The client sends a data string associated with an item
113string directly to the server. This succeeds or fails.
114\item {\bf Advise:} The client asks to be advised of any change in data
115associated with a particular item. If the server agrees, the server will
116send an OnAdvise message to the client along with the item and data.
117\end{itemize}
118
119The default data type is wxCF\_TEXT (ASCII text), and the default data
120size is the length of the null-terminated string. Windows-specific data
121types could also be used on the PC.
122
123\subsection{Examples}
124
125See the sample programs {\it server}\/ and {\it client}\/ in the IPC
126samples directory. Run the server, then the client. This demonstrates
127using the Execute, Request, and Poke commands from the client, together
128with an Advise loop: selecting an item in the server list box causes
129that item to be highlighted in the client list box.
130
a660d684
KB
131\subsection{More DDE details}
132
133A wxDDEClient object represents the client part of a client-server DDE
134(Dynamic Data Exchange) conversation (available in both
e2a6f233 135Windows and Unix).
a660d684
KB
136
137To create a client which can communicate with a suitable server,
138you need to derive a class from wxDDEConnection and another from wxDDEClient.
139The custom wxDDEConnection class will intercept communications in
140a `conversation' with a server, and the custom wxDDEServer is required
f6bcfd97 141so that a user-overridden \helpref{wxDDEClient::OnMakeConnection}{wxddeclientonmakeconnection} member can return
a660d684
KB
142a wxDDEConnection of the required class, when a connection is made.
143
144For example:
145
146\begin{verbatim}
147class MyConnection: public wxDDEConnection
148{
149 public:
150 MyConnection(void)::wxDDEConnection(ipc_buffer, 3999) {}
151 ~MyConnection(void) { }
e2a6f233 152 bool OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
a660d684
KB
153 { wxMessageBox(topic, data); }
154};
155
156class MyClient: public wxDDEClient
157{
158 public:
159 MyClient(void) {}
e2a6f233 160 wxConnectionBase *OnMakeConnection(void) { return new MyConnection; }
a660d684
KB
161};
162
163\end{verbatim}
164
165Here, {\bf MyConnection} will respond to \helpref{OnAdvise}{wxddeconnectiononadvise} messages sent
166by the server.
167
e2a6f233 168When the client application starts, it must create an instance of the derived wxDDEClient. In the following, command line
a660d684
KB
169arguments are used to pass the host name (the name of the machine the server is running
170on) and the server name (identifying the server process). Calling \helpref{wxDDEClient::MakeConnection}{wxddeclientmakeconnection}\rtfsp
171implicitly creates an instance of {\bf MyConnection} if the request for a
172connection is accepted, and the client then requests an {\it Advise} loop
173from the server, where the server calls the client when data has changed.
174
175\begin{verbatim}
e2a6f233
JS
176 wxString server = "4242";
177 wxString hostName;
178 wxGetHostName(hostName);
a660d684
KB
179
180 // Create a new client
181 MyClient *client = new MyClient;
e2a6f233 182 connection = (MyConnection *)client->MakeConnection(hostName, server, "IPC TEST");
a660d684 183
e2a6f233 184 if (!connection)
a660d684
KB
185 {
186 wxMessageBox("Failed to make connection to server", "Client Demo Error");
187 return NULL;
188 }
e2a6f233 189 connection->StartAdvise("Item");
a660d684
KB
190\end{verbatim}
191
e2a6f233
JS
192Note that it is no longer necessary to call wxDDEInitialize or wxDDECleanUp, since
193wxWindows will do this itself if necessary.
a660d684 194