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