]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: sckipc.cpp | |
3 | // Purpose: Interprocess communication implementation (wxSocket version) | |
4 | // Author: Julian Smart, Guilhem Lavaux | |
5 | // Modified by: Guilhem Lavaux (big rewrite) May 1997, 1998 | |
6 | // Created: 1993 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart 1993, Guilhem Lavaux 1997, 1998 | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "sckipc.h" | |
14 | #endif | |
15 | ||
fcc6dddd JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
f4ada568 | 18 | |
fcc6dddd JS |
19 | #ifdef __BORLANDC__ |
20 | #pragma hdrstop | |
f4ada568 GL |
21 | #endif |
22 | ||
35a4dab7 GL |
23 | #if wxUSE_SOCKETS |
24 | ||
fcc6dddd JS |
25 | #ifndef WX_PRECOMP |
26 | #endif | |
27 | ||
28 | #include <stdlib.h> | |
29 | #include <stdio.h> | |
30 | ||
f4ada568 GL |
31 | #include "wx/socket.h" |
32 | #include "wx/sckipc.h" | |
33 | ||
34 | #ifdef __BORLANDC__ | |
35 | #pragma hdrstop | |
36 | #endif | |
37 | ||
38 | #if !USE_SHARED_LIBRARY | |
39 | IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase) | |
40 | IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase) | |
41 | IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection, wxConnectionBase) | |
42 | #endif | |
43 | ||
44 | // It seems to be already defined somewhere in the Xt includes. | |
45 | #ifndef __XT__ | |
46 | // Message codes | |
47 | enum { | |
48 | IPC_EXECUTE = 1, | |
49 | IPC_REQUEST, | |
50 | IPC_POKE, | |
51 | IPC_ADVISE_START, | |
52 | IPC_ADVISE_REQUEST, | |
53 | IPC_ADVISE, | |
54 | IPC_ADVISE_STOP, | |
55 | IPC_REQUEST_REPLY, | |
56 | IPC_FAIL, | |
57 | IPC_CONNECT, | |
58 | IPC_DISCONNECT | |
59 | }; | |
60 | #endif | |
61 | ||
62 | void Server_OnRequest(wxSocketServer& server, | |
aa8fb7a0 | 63 | wxSocketNotify evt, |
f4ada568 GL |
64 | char *cdata); |
65 | void Client_OnRequest(wxSocketBase& sock, | |
aa8fb7a0 | 66 | wxSocketNotify evt, |
f4ada568 GL |
67 | char *cdata); |
68 | ||
69 | // --------------------------------------------------------------------------- | |
70 | // wxTCPClient | |
71 | // --------------------------------------------------------------------------- | |
72 | ||
6e31e940 | 73 | wxTCPClient::wxTCPClient () |
f4ada568 GL |
74 | : wxClientBase() |
75 | { | |
76 | } | |
77 | ||
6e31e940 | 78 | wxTCPClient::~wxTCPClient () |
f4ada568 GL |
79 | { |
80 | } | |
81 | ||
82 | bool wxTCPClient::ValidHost(const wxString& host) | |
83 | { | |
84 | wxIPV4address addr; | |
85 | ||
86 | return addr.Hostname(host); | |
87 | } | |
88 | ||
89 | wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host, | |
90 | const wxString& server_name, | |
91 | const wxString& topic) | |
92 | { | |
93 | wxIPV4address addr; | |
a324a7bc | 94 | wxSocketClient *client = new wxSocketClient(); |
f4ada568 | 95 | wxSocketStream *stream = new wxSocketStream(*client); |
75ed1d15 GL |
96 | wxDataInputStream data_is(*stream); |
97 | wxDataOutputStream data_os(*stream); | |
f4ada568 | 98 | |
aa8fb7a0 | 99 | client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); |
f4ada568 GL |
100 | addr.Service(server_name); |
101 | addr.Hostname(host); | |
102 | ||
103 | if (!client->Connect(addr)) { | |
104 | delete client; | |
105 | return NULL; | |
106 | } | |
107 | client->Notify(FALSE); | |
108 | ||
109 | // Send topic name, and enquire whether this has succeeded | |
110 | unsigned char msg; | |
111 | ||
75ed1d15 GL |
112 | data_os.Write8(IPC_CONNECT); |
113 | data_os.WriteString(topic); | |
f4ada568 | 114 | |
75ed1d15 | 115 | msg = data_is.Read8(); |
f4ada568 GL |
116 | |
117 | // OK! Confirmation. | |
118 | if (msg == IPC_CONNECT) { | |
119 | wxTCPConnection *connection = (wxTCPConnection *)OnMakeConnection (); | |
120 | if (connection) { | |
121 | if (!connection->IsKindOf(CLASSINFO(wxTCPConnection))) { | |
122 | delete connection; | |
123 | return NULL; | |
124 | } | |
125 | connection->m_topic = topic; | |
126 | client->Callback(Client_OnRequest); | |
127 | client->CallbackData((char *)connection); | |
128 | client->Notify(TRUE); | |
129 | return connection; | |
130 | } else { | |
131 | delete client; | |
132 | return NULL; | |
133 | } | |
134 | } else { | |
135 | delete client; | |
136 | return NULL; | |
137 | } | |
138 | return NULL; | |
139 | } | |
140 | ||
141 | wxConnectionBase *wxTCPClient::OnMakeConnection() | |
142 | { | |
143 | return new wxTCPConnection; | |
144 | } | |
145 | ||
146 | // --------------------------------------------------------------------------- | |
147 | // wxTCPServer | |
148 | // --------------------------------------------------------------------------- | |
149 | ||
6e31e940 | 150 | wxTCPServer::wxTCPServer () |
f4ada568 GL |
151 | : wxServerBase() |
152 | { | |
153 | } | |
154 | ||
155 | bool wxTCPServer::Create(const wxString& server_name) | |
156 | { | |
157 | wxIPV4address addr; | |
f4ada568 GL |
158 | wxSocketServer *server; |
159 | ||
160 | addr.Service(server_name); | |
161 | ||
162 | // Create a socket listening on specified port | |
a324a7bc | 163 | server = new wxSocketServer(addr); |
f4ada568 | 164 | server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest); |
aa8fb7a0 | 165 | server->SetNotify(wxSOCKET_CONNECTION_FLAG); |
f4ada568 GL |
166 | |
167 | server->CallbackData((char *)this); | |
168 | ||
169 | return TRUE; | |
170 | } | |
171 | ||
6e31e940 | 172 | wxTCPServer::~wxTCPServer() |
f4ada568 GL |
173 | { |
174 | } | |
175 | ||
e22036dc | 176 | wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) ) |
f4ada568 GL |
177 | { |
178 | return new wxTCPConnection(); | |
179 | } | |
180 | ||
181 | // --------------------------------------------------------------------------- | |
182 | // wxTCPConnection | |
183 | // --------------------------------------------------------------------------- | |
184 | ||
6e31e940 | 185 | wxTCPConnection::wxTCPConnection () |
f4ada568 | 186 | : wxConnectionBase(), |
75ed1d15 | 187 | m_sock(NULL), m_sockstrm(NULL), m_codeci(NULL), m_codeco(NULL) |
f4ada568 GL |
188 | { |
189 | } | |
190 | ||
6e31e940 | 191 | wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer), int WXUNUSED(size)) |
7921cf2b JS |
192 | { |
193 | } | |
194 | ||
6e31e940 | 195 | wxTCPConnection::~wxTCPConnection () |
f4ada568 GL |
196 | { |
197 | wxDELETE(m_sock); | |
75ed1d15 GL |
198 | wxDELETE(m_codeci); |
199 | wxDELETE(m_codeco); | |
f4ada568 GL |
200 | wxDELETE(m_sockstrm); |
201 | } | |
202 | ||
cb43b372 | 203 | void wxTCPConnection::Compress(bool WXUNUSED(on)) |
f4ada568 GL |
204 | { |
205 | // Use wxLZWStream | |
206 | } | |
207 | ||
208 | // Calls that CLIENT can make. | |
6e31e940 | 209 | bool wxTCPConnection::Disconnect () |
f4ada568 GL |
210 | { |
211 | // Send the the disconnect message to the peer. | |
75ed1d15 | 212 | m_codeco->Write8(IPC_DISCONNECT); |
f4ada568 GL |
213 | m_sock->Close(); |
214 | ||
215 | return TRUE; | |
216 | } | |
217 | ||
e90c1d2a | 218 | bool wxTCPConnection::Execute (const wxChar *data, int size, wxIPCFormat format) |
f4ada568 GL |
219 | { |
220 | if (!m_sock->IsConnected()) | |
221 | return FALSE; | |
222 | ||
223 | // Prepare EXECUTE message | |
75ed1d15 GL |
224 | m_codeco->Write8(IPC_EXECUTE); |
225 | m_codeco->Write8(format); | |
f4ada568 | 226 | if (size < 0) |
75ed1d15 | 227 | m_codeco->WriteString(data); |
f4ada568 | 228 | else { |
75ed1d15 | 229 | m_codeco->Write32(size); |
fae05df5 | 230 | m_sockstrm->Write(data, size); |
f4ada568 GL |
231 | } |
232 | ||
233 | return TRUE; | |
234 | } | |
235 | ||
0d2a2b60 | 236 | char *wxTCPConnection::Request (const wxString& item, int *size, wxIPCFormat format) |
f4ada568 GL |
237 | { |
238 | if (!m_sock->IsConnected()) | |
239 | return NULL; | |
240 | ||
75ed1d15 GL |
241 | m_codeco->Write8(IPC_REQUEST); |
242 | m_codeco->WriteString(item); | |
243 | m_codeco->Write8(format); | |
f4ada568 GL |
244 | |
245 | // If Unpack doesn't initialize it. | |
246 | int ret; | |
247 | ||
75ed1d15 | 248 | ret = m_codeci->Read8(); |
f4ada568 GL |
249 | if (ret == IPC_FAIL) |
250 | return NULL; | |
251 | else { | |
252 | size_t s; | |
253 | char *data = NULL; | |
254 | ||
75ed1d15 | 255 | s = m_codeci->Read32(); |
f4ada568 | 256 | data = new char[s]; |
fae05df5 | 257 | m_sockstrm->Read(data, s); |
f4ada568 GL |
258 | |
259 | if (size) | |
260 | *size = s; | |
261 | return data; | |
262 | } | |
263 | } | |
264 | ||
783b6cfd | 265 | bool wxTCPConnection::Poke (const wxString& item, wxChar *data, int size, wxIPCFormat format) |
f4ada568 GL |
266 | { |
267 | if (!m_sock->IsConnected()) | |
268 | return FALSE; | |
269 | ||
75ed1d15 GL |
270 | m_codeco->Write8(IPC_POKE); |
271 | m_codeco->WriteString(item); | |
272 | m_codeco->Write8(format); | |
f4ada568 | 273 | if (size < 0) |
75ed1d15 | 274 | m_codeco->WriteString(data); |
f4ada568 | 275 | else { |
75ed1d15 | 276 | m_codeco->Write32(size); |
fae05df5 | 277 | m_sockstrm->Write(data, size); |
f4ada568 GL |
278 | } |
279 | ||
280 | return TRUE; | |
281 | } | |
282 | ||
283 | bool wxTCPConnection::StartAdvise (const wxString& item) | |
284 | { | |
285 | int ret; | |
286 | ||
287 | if (!m_sock->IsConnected()) | |
288 | return FALSE; | |
289 | ||
75ed1d15 GL |
290 | m_codeco->Write8(IPC_ADVISE_START); |
291 | m_codeco->WriteString(item); | |
f4ada568 | 292 | |
75ed1d15 | 293 | ret = m_codeci->Read8(); |
f4ada568 GL |
294 | |
295 | if (ret != IPC_FAIL) | |
296 | return TRUE; | |
297 | else | |
298 | return FALSE; | |
299 | } | |
300 | ||
301 | bool wxTCPConnection::StopAdvise (const wxString& item) | |
302 | { | |
303 | int msg; | |
304 | ||
305 | if (!m_sock->IsConnected()) | |
306 | return FALSE; | |
307 | ||
75ed1d15 GL |
308 | m_codeco->Write8(IPC_ADVISE_STOP); |
309 | m_codeco->WriteString(item); | |
f4ada568 | 310 | |
75ed1d15 | 311 | msg = m_codeci->Read8(); |
f4ada568 GL |
312 | |
313 | if (msg != IPC_FAIL) | |
314 | return TRUE; | |
315 | else | |
316 | return FALSE; | |
317 | } | |
318 | ||
319 | // Calls that SERVER can make | |
320 | bool wxTCPConnection::Advise (const wxString& item, | |
783b6cfd | 321 | wxChar *data, int size, wxIPCFormat format) |
f4ada568 GL |
322 | { |
323 | if (!m_sock->IsConnected()) | |
324 | return FALSE; | |
325 | ||
75ed1d15 GL |
326 | m_codeco->Write8(IPC_ADVISE); |
327 | m_codeco->WriteString(item); | |
328 | m_codeco->Write8(format); | |
f4ada568 | 329 | if (size < 0) |
75ed1d15 | 330 | m_codeco->WriteString(data); |
f4ada568 | 331 | else { |
75ed1d15 | 332 | m_codeco->Write32(size); |
fae05df5 | 333 | m_sockstrm->Write(data, size); |
f4ada568 GL |
334 | } |
335 | ||
336 | return TRUE; | |
337 | } | |
338 | ||
aa8fb7a0 | 339 | void Client_OnRequest(wxSocketBase& sock, wxSocketNotify evt, |
f4ada568 GL |
340 | char *cdata) |
341 | { | |
342 | int msg = 0; | |
343 | wxTCPConnection *connection = (wxTCPConnection *)cdata; | |
75ed1d15 GL |
344 | wxDataInputStream *codeci; |
345 | wxDataOutputStream *codeco; | |
fae05df5 | 346 | wxSocketStream *sockstrm; |
f4ada568 GL |
347 | wxString topic_name = connection->m_topic; |
348 | wxString item; | |
349 | ||
350 | // The socket handler signals us that we lost the connection: destroy all. | |
aa8fb7a0 | 351 | if (evt == wxSOCKET_LOST) { |
f4ada568 GL |
352 | sock.Close(); |
353 | connection->OnDisconnect(); | |
354 | return; | |
355 | } | |
356 | ||
357 | // Receive message number. | |
75ed1d15 GL |
358 | codeci = connection->m_codeci; |
359 | codeco = connection->m_codeco; | |
fae05df5 | 360 | sockstrm = connection->m_sockstrm; |
75ed1d15 | 361 | msg = codeci->Read8(); |
f4ada568 GL |
362 | |
363 | switch (msg) { | |
364 | case IPC_EXECUTE: { | |
365 | char *data; | |
366 | size_t size; | |
0d2a2b60 | 367 | wxIPCFormat format; |
f4ada568 | 368 | |
0d2a2b60 | 369 | format = (wxIPCFormat)codeci->Read8(); |
75ed1d15 | 370 | size = codeci->Read32(); |
f4ada568 | 371 | data = new char[size]; |
fae05df5 | 372 | sockstrm->Read(data, size); |
f4ada568 GL |
373 | |
374 | connection->OnExecute (topic_name, data, size, format); | |
375 | ||
376 | delete [] data; | |
377 | break; | |
378 | } | |
379 | case IPC_ADVISE: { | |
380 | char *data; | |
381 | size_t size; | |
0d2a2b60 | 382 | wxIPCFormat format; |
f4ada568 | 383 | |
75ed1d15 | 384 | item = codeci->ReadString(); |
0d2a2b60 | 385 | format = (wxIPCFormat)codeci->Read8(); |
75ed1d15 | 386 | size = codeci->Read32(); |
f4ada568 | 387 | data = new char[size]; |
fae05df5 | 388 | sockstrm->Read(data, size); |
f4ada568 GL |
389 | |
390 | connection->OnAdvise (topic_name, item, data, size, format); | |
391 | ||
392 | delete [] data; | |
393 | break; | |
394 | } | |
395 | case IPC_ADVISE_START: { | |
75ed1d15 | 396 | item = codeci->ReadString(); |
f4ada568 GL |
397 | |
398 | bool ok = connection->OnStartAdvise (topic_name, item); | |
399 | if (ok) | |
75ed1d15 | 400 | codeco->Write8(IPC_ADVISE_START); |
f4ada568 | 401 | else |
75ed1d15 | 402 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
403 | |
404 | break; | |
405 | } | |
406 | case IPC_ADVISE_STOP: { | |
75ed1d15 | 407 | item = codeci->ReadString(); |
f4ada568 GL |
408 | |
409 | bool ok = connection->OnStopAdvise (topic_name, item); | |
410 | if (ok) | |
75ed1d15 | 411 | codeco->Write8(IPC_ADVISE_STOP); |
f4ada568 | 412 | else |
75ed1d15 | 413 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
414 | |
415 | break; | |
416 | } | |
417 | case IPC_POKE: { | |
0d2a2b60 | 418 | wxIPCFormat format; |
f4ada568 | 419 | size_t size; |
783b6cfd | 420 | wxChar *data; |
f4ada568 | 421 | |
75ed1d15 | 422 | item = codeci->ReadString(); |
0d2a2b60 | 423 | format = (wxIPCFormat)codeci->Read8(); |
75ed1d15 | 424 | size = codeci->Read32(); |
783b6cfd | 425 | data = new wxChar[size]; |
fae05df5 | 426 | sockstrm->Read(data, size); |
f4ada568 GL |
427 | |
428 | connection->OnPoke (topic_name, item, data, size, format); | |
429 | ||
430 | delete [] data; | |
431 | ||
432 | break; | |
433 | } | |
434 | case IPC_REQUEST: { | |
0d2a2b60 | 435 | wxIPCFormat format; |
f4ada568 | 436 | |
75ed1d15 | 437 | item = codeci->ReadString(); |
0d2a2b60 | 438 | format = (wxIPCFormat)codeci->Read8(); |
f4ada568 GL |
439 | |
440 | int user_size = -1; | |
441 | char *user_data = connection->OnRequest (topic_name, item, &user_size, format); | |
442 | ||
443 | if (user_data) { | |
75ed1d15 | 444 | codeco->Write8(IPC_REQUEST_REPLY); |
f4ada568 | 445 | if (user_size != -1) { |
75ed1d15 | 446 | codeco->Write32(user_size); |
fae05df5 | 447 | sockstrm->Write(user_data, user_size); |
f4ada568 | 448 | } else |
75ed1d15 | 449 | codeco->WriteString(user_data); |
f4ada568 | 450 | } else |
75ed1d15 | 451 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
452 | |
453 | break; | |
454 | } | |
455 | case IPC_DISCONNECT: { | |
456 | sock.Close(); | |
457 | connection->OnDisconnect(); | |
458 | break; | |
459 | } | |
460 | default: | |
75ed1d15 | 461 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
462 | break; |
463 | } | |
464 | } | |
465 | ||
466 | void Server_OnRequest(wxSocketServer& server, | |
aa8fb7a0 | 467 | wxSocketNotify evt, char *cdata) |
f4ada568 GL |
468 | { |
469 | wxTCPServer *ipcserv = (wxTCPServer *)cdata; | |
470 | wxSocketStream *stream; | |
75ed1d15 GL |
471 | wxDataInputStream *codeci; |
472 | wxDataOutputStream *codeco; | |
f4ada568 | 473 | |
aa8fb7a0 | 474 | if (evt != wxSOCKET_CONNECTION) |
f4ada568 GL |
475 | return; |
476 | ||
477 | /* Accept the connection, getting a new socket */ | |
478 | wxSocketBase *sock = server.Accept(); | |
479 | sock->Notify(FALSE); | |
aa8fb7a0 | 480 | sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); |
f4ada568 GL |
481 | |
482 | stream = new wxSocketStream(*sock); | |
75ed1d15 GL |
483 | codeci = new wxDataInputStream(*stream); |
484 | codeco = new wxDataOutputStream(*stream); | |
f4ada568 GL |
485 | |
486 | if (!sock->Ok()) | |
487 | return; | |
488 | ||
489 | int msg; | |
75ed1d15 | 490 | msg = codeci->Read8(); |
f4ada568 GL |
491 | |
492 | if (msg == IPC_CONNECT) { | |
493 | wxString topic_name; | |
75ed1d15 | 494 | topic_name = codeci->ReadString(); |
f4ada568 GL |
495 | |
496 | /* Register new socket with the notifier */ | |
497 | wxTCPConnection *new_connection = | |
498 | (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name); | |
499 | if (new_connection) { | |
500 | if (!new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) { | |
501 | delete new_connection; | |
75ed1d15 | 502 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
503 | return; |
504 | } | |
505 | // Acknowledge success | |
75ed1d15 | 506 | codeco->Write8(IPC_CONNECT); |
f4ada568 GL |
507 | |
508 | new_connection->m_topic = topic_name; | |
509 | new_connection->m_sockstrm = stream; | |
75ed1d15 GL |
510 | new_connection->m_codeci = codeci; |
511 | new_connection->m_codeco = codeco; | |
f4ada568 GL |
512 | sock->Callback(Client_OnRequest); |
513 | sock->CallbackData((char *)new_connection); | |
514 | sock->Notify(TRUE); | |
515 | } else { | |
516 | // Send failure message | |
75ed1d15 | 517 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
518 | } |
519 | } | |
520 | } | |
35a4dab7 GL |
521 | |
522 | #endif | |
523 | // wxUSE_SOCKETS |