]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: sckipc.cpp | |
3 | // Purpose: Interprocess communication implementation (wxSocket version) | |
0834112f | 4 | // Author: Julian Smart |
f4ada568 | 5 | // Modified by: Guilhem Lavaux (big rewrite) May 1997, 1998 |
0834112f | 6 | // Guillermo Rodriguez (updated for wxSocket v2) Jan 2000 |
cdc59bb6 | 7 | // (callbacks deprecated) Mar 2000 |
0dbfd66d | 8 | // Vadim Zeitlin (added support for Unix sockets) Apr 2002 |
f4ada568 GL |
9 | // Created: 1993 |
10 | // RCS-ID: $Id$ | |
0834112f GRG |
11 | // Copyright: (c) Julian Smart 1993 |
12 | // (c) Guilhem Lavaux 1997, 1998 | |
13 | // (c) 2000 Guillermo Rodriguez <guille@iies.es> | |
f4ada568 GL |
14 | // Licence: wxWindows license |
15 | ///////////////////////////////////////////////////////////////////////////// | |
16 | ||
cdc59bb6 GRG |
17 | // ========================================================================== |
18 | // declarations | |
19 | // ========================================================================== | |
20 | ||
21 | // -------------------------------------------------------------------------- | |
22 | // headers | |
23 | // -------------------------------------------------------------------------- | |
24 | ||
f4ada568 GL |
25 | #ifdef __GNUG__ |
26 | #pragma implementation "sckipc.h" | |
27 | #endif | |
28 | ||
fcc6dddd JS |
29 | // For compilers that support precompilation, includes "wx.h". |
30 | #include "wx/wxprec.h" | |
f4ada568 | 31 | |
fcc6dddd JS |
32 | #ifdef __BORLANDC__ |
33 | #pragma hdrstop | |
f4ada568 GL |
34 | #endif |
35 | ||
fcc6dddd | 36 | #ifndef WX_PRECOMP |
0dbfd66d | 37 | #include "wx/log.h" |
fcc6dddd JS |
38 | #endif |
39 | ||
f6bcfd97 | 40 | #if wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS |
07e829dc | 41 | |
fcc6dddd JS |
42 | #include <stdlib.h> |
43 | #include <stdio.h> | |
0dbfd66d | 44 | #include <errno.h> |
fcc6dddd | 45 | |
f4ada568 GL |
46 | #include "wx/socket.h" |
47 | #include "wx/sckipc.h" | |
cdc59bb6 GRG |
48 | #include "wx/module.h" |
49 | #include "wx/event.h" | |
f4ada568 | 50 | |
cdc59bb6 GRG |
51 | // -------------------------------------------------------------------------- |
52 | // macros and constants | |
53 | // -------------------------------------------------------------------------- | |
f4ada568 GL |
54 | |
55 | // It seems to be already defined somewhere in the Xt includes. | |
56 | #ifndef __XT__ | |
57 | // Message codes | |
cdc59bb6 GRG |
58 | enum |
59 | { | |
f4ada568 GL |
60 | IPC_EXECUTE = 1, |
61 | IPC_REQUEST, | |
62 | IPC_POKE, | |
63 | IPC_ADVISE_START, | |
64 | IPC_ADVISE_REQUEST, | |
65 | IPC_ADVISE, | |
66 | IPC_ADVISE_STOP, | |
67 | IPC_REQUEST_REPLY, | |
68 | IPC_FAIL, | |
69 | IPC_CONNECT, | |
70 | IPC_DISCONNECT | |
71 | }; | |
72 | #endif | |
73 | ||
d3ea6527 | 74 | // All sockets will be created with the following flags |
a24cc774 | 75 | #define SCKIPC_FLAGS (wxSOCKET_WAITALL) |
d3ea6527 | 76 | |
0dbfd66d VZ |
77 | // headers needed for umask() |
78 | #ifdef __UNIX_LIKE__ | |
79 | #include <sys/types.h> | |
80 | #include <sys/stat.h> | |
81 | #endif // __UNIX_LIKE__ | |
82 | ||
83 | // ---------------------------------------------------------------------------- | |
84 | // private functions | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | // get the address object for the given server name, the caller must delete it | |
88 | static wxSockAddress * | |
89 | GetAddressFromName(const wxString& serverName, const wxString& host = _T("")) | |
90 | { | |
91 | // we always use INET sockets under non-Unix systems | |
26a25f95 | 92 | #if defined(__UNIX__) && !defined(__WXMAC__) |
0dbfd66d VZ |
93 | // under Unix, if the server name looks like a path, create a AF_UNIX |
94 | // socket instead of AF_INET one | |
95 | if ( serverName.Find(_T('/')) != wxNOT_FOUND ) | |
96 | { | |
97 | wxUNIXaddress *addr = new wxUNIXaddress; | |
98 | addr->Filename(serverName); | |
99 | ||
100 | return addr; | |
101 | } | |
102 | #endif // Unix/!Unix | |
103 | { | |
104 | wxIPV4address *addr = new wxIPV4address; | |
105 | addr->Service(serverName); | |
106 | if ( !host.empty() ) | |
107 | { | |
108 | addr->Hostname(host); | |
109 | } | |
110 | ||
111 | return addr; | |
112 | } | |
113 | } | |
114 | ||
cdc59bb6 GRG |
115 | // -------------------------------------------------------------------------- |
116 | // wxTCPEventHandler stuff (private class) | |
117 | // -------------------------------------------------------------------------- | |
118 | ||
119 | class wxTCPEventHandler : public wxEvtHandler | |
120 | { | |
121 | public: | |
122 | wxTCPEventHandler() : wxEvtHandler() {}; | |
123 | ||
124 | void Client_OnRequest(wxSocketEvent& event); | |
125 | void Server_OnRequest(wxSocketEvent& event); | |
126 | ||
127 | DECLARE_EVENT_TABLE() | |
128 | }; | |
129 | ||
130 | enum | |
131 | { | |
132 | _CLIENT_ONREQUEST_ID = 1000, | |
133 | _SERVER_ONREQUEST_ID | |
134 | }; | |
135 | ||
136 | static wxTCPEventHandler *gs_handler = NULL; | |
137 | ||
138 | // ========================================================================== | |
139 | // implementation | |
140 | // ========================================================================== | |
141 | ||
142 | IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase) | |
143 | IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase) | |
144 | IMPLEMENT_CLASS(wxTCPConnection, wxConnectionBase) | |
145 | ||
146 | // -------------------------------------------------------------------------- | |
f4ada568 | 147 | // wxTCPClient |
cdc59bb6 | 148 | // -------------------------------------------------------------------------- |
f4ada568 | 149 | |
cdc59bb6 | 150 | wxTCPClient::wxTCPClient () : wxClientBase() |
f4ada568 GL |
151 | { |
152 | } | |
153 | ||
6e31e940 | 154 | wxTCPClient::~wxTCPClient () |
f4ada568 GL |
155 | { |
156 | } | |
157 | ||
158 | bool wxTCPClient::ValidHost(const wxString& host) | |
159 | { | |
160 | wxIPV4address addr; | |
161 | ||
162 | return addr.Hostname(host); | |
163 | } | |
164 | ||
165 | wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host, | |
0dbfd66d | 166 | const wxString& serverName, |
f4ada568 GL |
167 | const wxString& topic) |
168 | { | |
d3ea6527 | 169 | wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS); |
f4ada568 | 170 | wxSocketStream *stream = new wxSocketStream(*client); |
0834112f GRG |
171 | wxDataInputStream *data_is = new wxDataInputStream(*stream); |
172 | wxDataOutputStream *data_os = new wxDataOutputStream(*stream); | |
173 | ||
0dbfd66d VZ |
174 | wxSockAddress *addr = GetAddressFromName(serverName, host); |
175 | if ( !addr ) | |
176 | return NULL; | |
177 | ||
178 | bool ok = client->Connect(*addr); | |
179 | delete addr; | |
f4ada568 | 180 | |
0dbfd66d | 181 | if ( ok ) |
0834112f GRG |
182 | { |
183 | unsigned char msg; | |
26a25f95 | 184 | |
0834112f GRG |
185 | // Send topic name, and enquire whether this has succeeded |
186 | data_os->Write8(IPC_CONNECT); | |
187 | data_os->WriteString(topic); | |
26a25f95 | 188 | |
0834112f GRG |
189 | msg = data_is->Read8(); |
190 | ||
191 | // OK! Confirmation. | |
192 | if (msg == IPC_CONNECT) | |
193 | { | |
194 | wxTCPConnection *connection = (wxTCPConnection *)OnMakeConnection (); | |
195 | ||
196 | if (connection) | |
197 | { | |
3adb47a9 | 198 | if (connection->IsKindOf(CLASSINFO(wxTCPConnection))) |
0834112f GRG |
199 | { |
200 | connection->m_topic = topic; | |
201 | connection->m_sock = client; | |
202 | connection->m_sockstrm = stream; | |
203 | connection->m_codeci = data_is; | |
204 | connection->m_codeco = data_os; | |
cdc59bb6 GRG |
205 | client->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); |
206 | client->SetClientData(connection); | |
0834112f GRG |
207 | client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); |
208 | client->Notify(TRUE); | |
209 | return connection; | |
210 | } | |
3adb47a9 GRG |
211 | else |
212 | { | |
213 | delete connection; | |
214 | // and fall through to delete everything else | |
215 | } | |
f4ada568 | 216 | } |
f4ada568 | 217 | } |
f4ada568 | 218 | } |
0834112f | 219 | |
3adb47a9 | 220 | // Something went wrong, delete everything |
0834112f GRG |
221 | delete data_is; |
222 | delete data_os; | |
223 | delete stream; | |
3adb47a9 GRG |
224 | client->Destroy(); |
225 | ||
0834112f | 226 | return NULL; |
f4ada568 GL |
227 | } |
228 | ||
229 | wxConnectionBase *wxTCPClient::OnMakeConnection() | |
230 | { | |
e5b502f3 | 231 | return new wxTCPConnection(); |
f4ada568 GL |
232 | } |
233 | ||
cdc59bb6 | 234 | // -------------------------------------------------------------------------- |
f4ada568 | 235 | // wxTCPServer |
cdc59bb6 | 236 | // -------------------------------------------------------------------------- |
f4ada568 | 237 | |
cdc59bb6 | 238 | wxTCPServer::wxTCPServer () : wxServerBase() |
f4ada568 | 239 | { |
f6bcfd97 | 240 | m_server = NULL; |
f4ada568 GL |
241 | } |
242 | ||
f6bcfd97 | 243 | bool wxTCPServer::Create(const wxString& serverName) |
f4ada568 | 244 | { |
f6bcfd97 BP |
245 | // Destroy previous server, if any |
246 | if (m_server) | |
247 | { | |
248 | m_server->SetClientData(NULL); | |
249 | m_server->Destroy(); | |
250 | m_server = NULL; | |
251 | } | |
f4ada568 | 252 | |
0dbfd66d VZ |
253 | wxSockAddress *addr = GetAddressFromName(serverName); |
254 | if ( !addr ) | |
255 | return FALSE; | |
256 | ||
257 | #ifdef __UNIX_LIKE__ | |
258 | mode_t umaskOld; | |
259 | if ( addr->Type() == wxSockAddress::UNIX ) | |
260 | { | |
261 | // ensure that the file doesn't exist as otherwise calling socket() would | |
262 | // fail | |
263 | int rc = remove(serverName); | |
264 | if ( rc < 0 && errno != ENOENT ) | |
265 | { | |
266 | delete addr; | |
267 | ||
268 | return FALSE; | |
269 | } | |
270 | ||
271 | // also set the umask to prevent the others from reading our file | |
272 | umaskOld = umask(077); | |
273 | } | |
274 | else | |
275 | { | |
276 | // unused anyhow but shut down the compiler warnings | |
277 | umaskOld = 0; | |
278 | } | |
279 | #endif // __UNIX_LIKE__ | |
f4ada568 | 280 | |
f6bcfd97 | 281 | // Create a socket listening on the specified port |
0dbfd66d VZ |
282 | m_server = new wxSocketServer(*addr, SCKIPC_FLAGS); |
283 | ||
284 | #ifdef __UNIX_LIKE__ | |
285 | if ( addr->Type() == wxSockAddress::UNIX ) | |
286 | { | |
287 | // restore the umask | |
288 | umask(umaskOld); | |
289 | ||
290 | // save the file name to remove it later | |
291 | m_filename = serverName; | |
292 | } | |
293 | #endif // __UNIX_LIKE__ | |
294 | ||
295 | delete addr; | |
f6bcfd97 BP |
296 | |
297 | if (!m_server->Ok()) | |
298 | { | |
299 | m_server->Destroy(); | |
300 | m_server = NULL; | |
301 | ||
302 | return FALSE; | |
303 | } | |
304 | ||
305 | m_server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID); | |
306 | m_server->SetClientData(this); | |
307 | m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); | |
308 | m_server->Notify(TRUE); | |
f4ada568 GL |
309 | |
310 | return TRUE; | |
311 | } | |
312 | ||
6e31e940 | 313 | wxTCPServer::~wxTCPServer() |
f4ada568 | 314 | { |
0dbfd66d VZ |
315 | if (m_server) |
316 | { | |
317 | m_server->SetClientData(NULL); | |
318 | m_server->Destroy(); | |
319 | } | |
320 | ||
321 | #ifdef __UNIX_LIKE__ | |
322 | if ( !m_filename.empty() ) | |
323 | { | |
324 | if ( !remove(m_filename) ) | |
325 | { | |
326 | wxLogDebug(_T("Stale AF_UNIX file '%s' left."), m_filename.c_str()); | |
327 | } | |
328 | } | |
329 | #endif // __UNIX_LIKE__ | |
f4ada568 GL |
330 | } |
331 | ||
e22036dc | 332 | wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) ) |
f4ada568 GL |
333 | { |
334 | return new wxTCPConnection(); | |
335 | } | |
336 | ||
cdc59bb6 | 337 | // -------------------------------------------------------------------------- |
f4ada568 | 338 | // wxTCPConnection |
cdc59bb6 | 339 | // -------------------------------------------------------------------------- |
f4ada568 | 340 | |
cdc59bb6 | 341 | wxTCPConnection::wxTCPConnection () : wxConnectionBase() |
f4ada568 | 342 | { |
cdc59bb6 GRG |
343 | m_sock = NULL; |
344 | m_sockstrm = NULL; | |
345 | m_codeci = NULL; | |
346 | m_codeco = NULL; | |
f4ada568 GL |
347 | } |
348 | ||
6e31e940 | 349 | wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer), int WXUNUSED(size)) |
7921cf2b JS |
350 | { |
351 | } | |
352 | ||
6e31e940 | 353 | wxTCPConnection::~wxTCPConnection () |
f4ada568 | 354 | { |
75ed1d15 GL |
355 | wxDELETE(m_codeci); |
356 | wxDELETE(m_codeco); | |
f4ada568 | 357 | wxDELETE(m_sockstrm); |
3adb47a9 | 358 | |
e5b502f3 GRG |
359 | if (m_sock) |
360 | { | |
361 | m_sock->SetClientData(NULL); | |
362 | m_sock->Destroy(); | |
363 | } | |
f4ada568 GL |
364 | } |
365 | ||
cb43b372 | 366 | void wxTCPConnection::Compress(bool WXUNUSED(on)) |
f4ada568 GL |
367 | { |
368 | // Use wxLZWStream | |
369 | } | |
370 | ||
371 | // Calls that CLIENT can make. | |
6e31e940 | 372 | bool wxTCPConnection::Disconnect () |
f4ada568 GL |
373 | { |
374 | // Send the the disconnect message to the peer. | |
75ed1d15 | 375 | m_codeco->Write8(IPC_DISCONNECT); |
cdc59bb6 | 376 | m_sock->Notify(FALSE); |
f4ada568 GL |
377 | m_sock->Close(); |
378 | ||
379 | return TRUE; | |
380 | } | |
381 | ||
0834112f | 382 | bool wxTCPConnection::Execute(const wxChar *data, int size, wxIPCFormat format) |
f4ada568 GL |
383 | { |
384 | if (!m_sock->IsConnected()) | |
385 | return FALSE; | |
386 | ||
387 | // Prepare EXECUTE message | |
75ed1d15 GL |
388 | m_codeco->Write8(IPC_EXECUTE); |
389 | m_codeco->Write8(format); | |
0834112f | 390 | |
f4ada568 | 391 | if (size < 0) |
f6bcfd97 | 392 | size = wxStrlen(data) + 1; // includes final NUL |
0834112f GRG |
393 | |
394 | m_codeco->Write32(size); | |
395 | m_sockstrm->Write(data, size); | |
f4ada568 GL |
396 | |
397 | return TRUE; | |
398 | } | |
399 | ||
0d2a2b60 | 400 | char *wxTCPConnection::Request (const wxString& item, int *size, wxIPCFormat format) |
f4ada568 GL |
401 | { |
402 | if (!m_sock->IsConnected()) | |
403 | return NULL; | |
404 | ||
75ed1d15 GL |
405 | m_codeco->Write8(IPC_REQUEST); |
406 | m_codeco->WriteString(item); | |
407 | m_codeco->Write8(format); | |
f4ada568 GL |
408 | |
409 | // If Unpack doesn't initialize it. | |
410 | int ret; | |
411 | ||
75ed1d15 | 412 | ret = m_codeci->Read8(); |
f4ada568 GL |
413 | if (ret == IPC_FAIL) |
414 | return NULL; | |
0834112f GRG |
415 | else |
416 | { | |
f4ada568 GL |
417 | size_t s; |
418 | char *data = NULL; | |
419 | ||
75ed1d15 | 420 | s = m_codeci->Read32(); |
f4ada568 | 421 | data = new char[s]; |
fae05df5 | 422 | m_sockstrm->Read(data, s); |
f4ada568 GL |
423 | |
424 | if (size) | |
425 | *size = s; | |
426 | return data; | |
427 | } | |
428 | } | |
429 | ||
783b6cfd | 430 | bool wxTCPConnection::Poke (const wxString& item, wxChar *data, int size, wxIPCFormat format) |
f4ada568 GL |
431 | { |
432 | if (!m_sock->IsConnected()) | |
433 | return FALSE; | |
434 | ||
75ed1d15 GL |
435 | m_codeco->Write8(IPC_POKE); |
436 | m_codeco->WriteString(item); | |
437 | m_codeco->Write8(format); | |
0834112f | 438 | |
f4ada568 | 439 | if (size < 0) |
f6bcfd97 | 440 | size = wxStrlen(data) + 1; // includes final NUL |
0834112f GRG |
441 | |
442 | m_codeco->Write32(size); | |
443 | m_sockstrm->Write(data, size); | |
f4ada568 GL |
444 | |
445 | return TRUE; | |
446 | } | |
447 | ||
448 | bool wxTCPConnection::StartAdvise (const wxString& item) | |
449 | { | |
450 | int ret; | |
451 | ||
452 | if (!m_sock->IsConnected()) | |
453 | return FALSE; | |
454 | ||
75ed1d15 GL |
455 | m_codeco->Write8(IPC_ADVISE_START); |
456 | m_codeco->WriteString(item); | |
f4ada568 | 457 | |
75ed1d15 | 458 | ret = m_codeci->Read8(); |
f4ada568 GL |
459 | |
460 | if (ret != IPC_FAIL) | |
461 | return TRUE; | |
462 | else | |
463 | return FALSE; | |
464 | } | |
465 | ||
466 | bool wxTCPConnection::StopAdvise (const wxString& item) | |
467 | { | |
468 | int msg; | |
469 | ||
470 | if (!m_sock->IsConnected()) | |
471 | return FALSE; | |
472 | ||
75ed1d15 GL |
473 | m_codeco->Write8(IPC_ADVISE_STOP); |
474 | m_codeco->WriteString(item); | |
f4ada568 | 475 | |
75ed1d15 | 476 | msg = m_codeci->Read8(); |
f4ada568 GL |
477 | |
478 | if (msg != IPC_FAIL) | |
479 | return TRUE; | |
480 | else | |
481 | return FALSE; | |
482 | } | |
483 | ||
484 | // Calls that SERVER can make | |
485 | bool wxTCPConnection::Advise (const wxString& item, | |
783b6cfd | 486 | wxChar *data, int size, wxIPCFormat format) |
f4ada568 GL |
487 | { |
488 | if (!m_sock->IsConnected()) | |
489 | return FALSE; | |
490 | ||
75ed1d15 GL |
491 | m_codeco->Write8(IPC_ADVISE); |
492 | m_codeco->WriteString(item); | |
493 | m_codeco->Write8(format); | |
0834112f | 494 | |
f4ada568 | 495 | if (size < 0) |
f6bcfd97 | 496 | size = wxStrlen(data) + 1; // includes final NUL |
0834112f GRG |
497 | |
498 | m_codeco->Write32(size); | |
499 | m_sockstrm->Write(data, size); | |
f4ada568 GL |
500 | |
501 | return TRUE; | |
502 | } | |
503 | ||
cdc59bb6 GRG |
504 | // -------------------------------------------------------------------------- |
505 | // wxTCPEventHandler (private class) | |
506 | // -------------------------------------------------------------------------- | |
507 | ||
508 | BEGIN_EVENT_TABLE(wxTCPEventHandler, wxEvtHandler) | |
509 | EVT_SOCKET(_CLIENT_ONREQUEST_ID, wxTCPEventHandler::Client_OnRequest) | |
510 | EVT_SOCKET(_SERVER_ONREQUEST_ID, wxTCPEventHandler::Server_OnRequest) | |
511 | END_EVENT_TABLE() | |
512 | ||
513 | void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event) | |
f4ada568 | 514 | { |
cdc59bb6 GRG |
515 | wxSocketBase *sock = event.GetSocket(); |
516 | wxSocketNotify evt = event.GetSocketEvent(); | |
1f0500b3 | 517 | wxTCPConnection *connection = (wxTCPConnection *)(sock->GetClientData()); |
cdc59bb6 | 518 | |
e5b502f3 GRG |
519 | // This socket is being deleted; skip this event |
520 | if (!connection) | |
521 | return; | |
522 | ||
f4ada568 | 523 | int msg = 0; |
75ed1d15 | 524 | wxDataInputStream *codeci; |
26a25f95 | 525 | wxDataOutputStream *codeco; |
fae05df5 | 526 | wxSocketStream *sockstrm; |
f4ada568 GL |
527 | wxString topic_name = connection->m_topic; |
528 | wxString item; | |
529 | ||
e5b502f3 | 530 | // We lost the connection: destroy everything |
0834112f GRG |
531 | if (evt == wxSOCKET_LOST) |
532 | { | |
cdc59bb6 GRG |
533 | sock->Notify(FALSE); |
534 | sock->Close(); | |
f4ada568 GL |
535 | connection->OnDisconnect(); |
536 | return; | |
537 | } | |
538 | ||
539 | // Receive message number. | |
75ed1d15 GL |
540 | codeci = connection->m_codeci; |
541 | codeco = connection->m_codeco; | |
fae05df5 | 542 | sockstrm = connection->m_sockstrm; |
75ed1d15 | 543 | msg = codeci->Read8(); |
f4ada568 | 544 | |
0834112f GRG |
545 | switch (msg) |
546 | { | |
547 | case IPC_EXECUTE: | |
548 | { | |
f4ada568 | 549 | char *data; |
26a25f95 | 550 | size_t size; |
0d2a2b60 | 551 | wxIPCFormat format; |
26a25f95 | 552 | |
0d2a2b60 | 553 | format = (wxIPCFormat)codeci->Read8(); |
75ed1d15 | 554 | size = codeci->Read32(); |
f4ada568 | 555 | data = new char[size]; |
fae05df5 | 556 | sockstrm->Read(data, size); |
f4ada568 | 557 | |
e0fbcda6 | 558 | connection->OnExecute (topic_name, data, size, format); |
f4ada568 GL |
559 | |
560 | delete [] data; | |
561 | break; | |
562 | } | |
0834112f GRG |
563 | case IPC_ADVISE: |
564 | { | |
f4ada568 GL |
565 | char *data; |
566 | size_t size; | |
0d2a2b60 | 567 | wxIPCFormat format; |
f4ada568 | 568 | |
75ed1d15 | 569 | item = codeci->ReadString(); |
0d2a2b60 | 570 | format = (wxIPCFormat)codeci->Read8(); |
75ed1d15 | 571 | size = codeci->Read32(); |
f4ada568 | 572 | data = new char[size]; |
fae05df5 | 573 | sockstrm->Read(data, size); |
26a25f95 | 574 | |
e0fbcda6 | 575 | connection->OnAdvise (topic_name, item, data, size, format); |
f4ada568 GL |
576 | |
577 | delete [] data; | |
578 | break; | |
579 | } | |
0834112f GRG |
580 | case IPC_ADVISE_START: |
581 | { | |
75ed1d15 | 582 | item = codeci->ReadString(); |
f4ada568 GL |
583 | |
584 | bool ok = connection->OnStartAdvise (topic_name, item); | |
585 | if (ok) | |
75ed1d15 | 586 | codeco->Write8(IPC_ADVISE_START); |
f4ada568 | 587 | else |
75ed1d15 | 588 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
589 | |
590 | break; | |
591 | } | |
0834112f GRG |
592 | case IPC_ADVISE_STOP: |
593 | { | |
75ed1d15 | 594 | item = codeci->ReadString(); |
f4ada568 GL |
595 | |
596 | bool ok = connection->OnStopAdvise (topic_name, item); | |
597 | if (ok) | |
75ed1d15 | 598 | codeco->Write8(IPC_ADVISE_STOP); |
f4ada568 | 599 | else |
75ed1d15 | 600 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
601 | |
602 | break; | |
603 | } | |
0834112f GRG |
604 | case IPC_POKE: |
605 | { | |
0d2a2b60 | 606 | wxIPCFormat format; |
f4ada568 | 607 | size_t size; |
783b6cfd | 608 | wxChar *data; |
f4ada568 | 609 | |
75ed1d15 | 610 | item = codeci->ReadString(); |
0d2a2b60 | 611 | format = (wxIPCFormat)codeci->Read8(); |
75ed1d15 | 612 | size = codeci->Read32(); |
783b6cfd | 613 | data = new wxChar[size]; |
fae05df5 | 614 | sockstrm->Read(data, size); |
26a25f95 | 615 | |
f4ada568 GL |
616 | connection->OnPoke (topic_name, item, data, size, format); |
617 | ||
618 | delete [] data; | |
619 | ||
620 | break; | |
621 | } | |
0834112f GRG |
622 | case IPC_REQUEST: |
623 | { | |
0d2a2b60 | 624 | wxIPCFormat format; |
f4ada568 | 625 | |
75ed1d15 | 626 | item = codeci->ReadString(); |
0d2a2b60 | 627 | format = (wxIPCFormat)codeci->Read8(); |
f4ada568 GL |
628 | |
629 | int user_size = -1; | |
630 | char *user_data = connection->OnRequest (topic_name, item, &user_size, format); | |
631 | ||
0834112f GRG |
632 | if (user_data) |
633 | { | |
75ed1d15 | 634 | codeco->Write8(IPC_REQUEST_REPLY); |
0834112f GRG |
635 | |
636 | if (user_size == -1) | |
637 | user_size = strlen(user_data) + 1; // includes final NUL | |
638 | ||
639 | codeco->Write32(user_size); | |
640 | sockstrm->Write(user_data, user_size); | |
641 | } | |
642 | else | |
75ed1d15 | 643 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
644 | |
645 | break; | |
646 | } | |
0834112f GRG |
647 | case IPC_DISCONNECT: |
648 | { | |
cdc59bb6 GRG |
649 | sock->Notify(FALSE); |
650 | sock->Close(); | |
f4ada568 GL |
651 | connection->OnDisconnect(); |
652 | break; | |
653 | } | |
654 | default: | |
75ed1d15 | 655 | codeco->Write8(IPC_FAIL); |
f4ada568 GL |
656 | break; |
657 | } | |
658 | } | |
659 | ||
cdc59bb6 | 660 | void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event) |
f4ada568 | 661 | { |
cdc59bb6 | 662 | wxSocketServer *server = (wxSocketServer *) event.GetSocket(); |
f6bcfd97 | 663 | wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData(); |
f4ada568 | 664 | |
e5b502f3 GRG |
665 | // This socket is being deleted; skip this event |
666 | if (!ipcserv) | |
667 | return; | |
668 | ||
cdc59bb6 | 669 | if (event.GetSocketEvent() != wxSOCKET_CONNECTION) |
f4ada568 GL |
670 | return; |
671 | ||
3adb47a9 | 672 | // Accept the connection, getting a new socket |
cdc59bb6 | 673 | wxSocketBase *sock = server->Accept(); |
0834112f | 674 | if (!sock->Ok()) |
3adb47a9 GRG |
675 | { |
676 | sock->Destroy(); | |
0834112f | 677 | return; |
3adb47a9 | 678 | } |
f4ada568 | 679 | |
cdc59bb6 GRG |
680 | wxSocketStream *stream = new wxSocketStream(*sock); |
681 | wxDataInputStream *codeci = new wxDataInputStream(*stream); | |
682 | wxDataOutputStream *codeco = new wxDataOutputStream(*stream); | |
f4ada568 | 683 | |
f4ada568 | 684 | int msg; |
75ed1d15 | 685 | msg = codeci->Read8(); |
f4ada568 | 686 | |
0834112f GRG |
687 | if (msg == IPC_CONNECT) |
688 | { | |
f4ada568 | 689 | wxString topic_name; |
75ed1d15 | 690 | topic_name = codeci->ReadString(); |
f4ada568 | 691 | |
f4ada568 GL |
692 | wxTCPConnection *new_connection = |
693 | (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name); | |
3adb47a9 | 694 | |
0834112f GRG |
695 | if (new_connection) |
696 | { | |
3adb47a9 | 697 | if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) |
0834112f | 698 | { |
3adb47a9 GRG |
699 | // Acknowledge success |
700 | codeco->Write8(IPC_CONNECT); | |
701 | new_connection->m_topic = topic_name; | |
26a25f95 | 702 | new_connection->m_sock = sock; |
3adb47a9 GRG |
703 | new_connection->m_sockstrm = stream; |
704 | new_connection->m_codeci = codeci; | |
705 | new_connection->m_codeco = codeco; | |
cdc59bb6 GRG |
706 | sock->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); |
707 | sock->SetClientData(new_connection); | |
3adb47a9 GRG |
708 | sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); |
709 | sock->Notify(TRUE); | |
f4ada568 GL |
710 | return; |
711 | } | |
3adb47a9 GRG |
712 | else |
713 | { | |
714 | delete new_connection; | |
715 | // and fall through to delete everything else | |
716 | } | |
f4ada568 GL |
717 | } |
718 | } | |
3adb47a9 GRG |
719 | |
720 | // Something went wrong, send failure message and delete everything | |
721 | codeco->Write8(IPC_FAIL); | |
722 | ||
723 | delete codeco; | |
724 | delete codeci; | |
725 | delete stream; | |
726 | sock->Destroy(); | |
f4ada568 | 727 | } |
35a4dab7 | 728 | |
cdc59bb6 GRG |
729 | // -------------------------------------------------------------------------- |
730 | // wxTCPEventHandlerModule (private class) | |
731 | // -------------------------------------------------------------------------- | |
732 | ||
733 | class WXDLLEXPORT wxTCPEventHandlerModule: public wxModule | |
734 | { | |
735 | DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule) | |
736 | ||
737 | public: | |
738 | bool OnInit() { gs_handler = new wxTCPEventHandler(); return TRUE; } | |
739 | void OnExit() { wxDELETE(gs_handler); } | |
740 | }; | |
741 | ||
742 | IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule, wxModule) | |
743 | ||
744 | ||
35a4dab7 | 745 | #endif |
3adb47a9 | 746 | // wxUSE_SOCKETS && wxUSE_IPC |