]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/sckipc.cpp | |
3 | // Purpose: Interprocess communication implementation (wxSocket version) | |
4 | // Author: Julian Smart | |
5 | // Modified by: Guilhem Lavaux (big rewrite) May 1997, 1998 | |
6 | // Guillermo Rodriguez (updated for wxSocket v2) Jan 2000 | |
7 | // (callbacks deprecated) Mar 2000 | |
8 | // Vadim Zeitlin (added support for Unix sockets) Apr 2002 | |
9 | // Created: 1993 | |
10 | // RCS-ID: $Id$ | |
11 | // Copyright: (c) Julian Smart 1993 | |
12 | // (c) Guilhem Lavaux 1997, 1998 | |
13 | // (c) 2000 Guillermo Rodriguez <guille@iies.es> | |
14 | // Licence: wxWindows licence | |
15 | ///////////////////////////////////////////////////////////////////////////// | |
16 | ||
17 | // ========================================================================== | |
18 | // declarations | |
19 | // ========================================================================== | |
20 | ||
21 | // -------------------------------------------------------------------------- | |
22 | // headers | |
23 | // -------------------------------------------------------------------------- | |
24 | ||
25 | // For compilers that support precompilation, includes "wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | #if wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS | |
33 | ||
34 | #include "wx/sckipc.h" | |
35 | ||
36 | #ifndef WX_PRECOMP | |
37 | #include "wx/log.h" | |
38 | #include "wx/event.h" | |
39 | #endif | |
40 | ||
41 | #include <stdlib.h> | |
42 | #include <stdio.h> | |
43 | #include <errno.h> | |
44 | ||
45 | #include "wx/socket.h" | |
46 | #include "wx/module.h" | |
47 | ||
48 | // -------------------------------------------------------------------------- | |
49 | // macros and constants | |
50 | // -------------------------------------------------------------------------- | |
51 | ||
52 | // It seems to be already defined somewhere in the Xt includes. | |
53 | #ifndef __XT__ | |
54 | // Message codes | |
55 | enum | |
56 | { | |
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 | ||
71 | // All sockets will be created with the following flags | |
72 | #define SCKIPC_FLAGS (wxSOCKET_WAITALL) | |
73 | ||
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 * | |
86 | GetAddressFromName(const wxString& serverName, const wxString& host = wxEmptyString) | |
87 | { | |
88 | // we always use INET sockets under non-Unix systems | |
89 | #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__)) | |
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 | ||
112 | // -------------------------------------------------------------------------- | |
113 | // wxTCPEventHandler stuff (private class) | |
114 | // -------------------------------------------------------------------------- | |
115 | ||
116 | class wxTCPEventHandler : public wxEvtHandler | |
117 | { | |
118 | public: | |
119 | wxTCPEventHandler() : wxEvtHandler() {} | |
120 | ||
121 | void Client_OnRequest(wxSocketEvent& event); | |
122 | void Server_OnRequest(wxSocketEvent& event); | |
123 | ||
124 | DECLARE_EVENT_TABLE() | |
125 | DECLARE_NO_COPY_CLASS(wxTCPEventHandler) | |
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 | // -------------------------------------------------------------------------- | |
145 | // wxTCPClient | |
146 | // -------------------------------------------------------------------------- | |
147 | ||
148 | wxTCPClient::wxTCPClient () : wxClientBase() | |
149 | { | |
150 | } | |
151 | ||
152 | wxTCPClient::~wxTCPClient () | |
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, | |
164 | const wxString& serverName, | |
165 | const wxString& topic) | |
166 | { | |
167 | wxSockAddress *addr = GetAddressFromName(serverName, host); | |
168 | if ( !addr ) | |
169 | return NULL; | |
170 | ||
171 | wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS); | |
172 | wxSocketStream *stream = new wxSocketStream(*client); | |
173 | wxDataInputStream *data_is = new wxDataInputStream(*stream); | |
174 | wxDataOutputStream *data_os = new wxDataOutputStream(*stream); | |
175 | ||
176 | bool ok = client->Connect(*addr); | |
177 | delete addr; | |
178 | ||
179 | if ( ok ) | |
180 | { | |
181 | unsigned char msg; | |
182 | ||
183 | // Send topic name, and enquire whether this has succeeded | |
184 | data_os->Write8(IPC_CONNECT); | |
185 | data_os->WriteString(topic); | |
186 | ||
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 | { | |
196 | if (connection->IsKindOf(CLASSINFO(wxTCPConnection))) | |
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; | |
203 | client->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); | |
204 | client->SetClientData(connection); | |
205 | client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); | |
206 | client->Notify(true); | |
207 | return connection; | |
208 | } | |
209 | else | |
210 | { | |
211 | delete connection; | |
212 | // and fall through to delete everything else | |
213 | } | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
218 | // Something went wrong, delete everything | |
219 | delete data_is; | |
220 | delete data_os; | |
221 | delete stream; | |
222 | client->Destroy(); | |
223 | ||
224 | return NULL; | |
225 | } | |
226 | ||
227 | wxConnectionBase *wxTCPClient::OnMakeConnection() | |
228 | { | |
229 | return new wxTCPConnection(); | |
230 | } | |
231 | ||
232 | // -------------------------------------------------------------------------- | |
233 | // wxTCPServer | |
234 | // -------------------------------------------------------------------------- | |
235 | ||
236 | wxTCPServer::wxTCPServer () : wxServerBase() | |
237 | { | |
238 | m_server = NULL; | |
239 | } | |
240 | ||
241 | bool wxTCPServer::Create(const wxString& serverName) | |
242 | { | |
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 | } | |
250 | ||
251 | wxSockAddress *addr = GetAddressFromName(serverName); | |
252 | if ( !addr ) | |
253 | return false; | |
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 | |
261 | int rc = remove(serverName.fn_str()); | |
262 | if ( rc < 0 && errno != ENOENT ) | |
263 | { | |
264 | delete addr; | |
265 | ||
266 | return false; | |
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__ | |
278 | ||
279 | // Create a socket listening on the specified port | |
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; | |
294 | ||
295 | if (!m_server->Ok()) | |
296 | { | |
297 | m_server->Destroy(); | |
298 | m_server = NULL; | |
299 | ||
300 | return false; | |
301 | } | |
302 | ||
303 | m_server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID); | |
304 | m_server->SetClientData(this); | |
305 | m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); | |
306 | m_server->Notify(true); | |
307 | ||
308 | return true; | |
309 | } | |
310 | ||
311 | wxTCPServer::~wxTCPServer() | |
312 | { | |
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 | { | |
322 | if ( remove(m_filename.fn_str()) != 0 ) | |
323 | { | |
324 | wxLogDebug(_T("Stale AF_UNIX file '%s' left."), m_filename.c_str()); | |
325 | } | |
326 | } | |
327 | #endif // __UNIX_LIKE__ | |
328 | } | |
329 | ||
330 | wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) ) | |
331 | { | |
332 | return new wxTCPConnection(); | |
333 | } | |
334 | ||
335 | // -------------------------------------------------------------------------- | |
336 | // wxTCPConnection | |
337 | // -------------------------------------------------------------------------- | |
338 | ||
339 | wxTCPConnection::wxTCPConnection () : wxConnectionBase() | |
340 | { | |
341 | m_sock = NULL; | |
342 | m_sockstrm = NULL; | |
343 | m_codeci = NULL; | |
344 | m_codeco = NULL; | |
345 | } | |
346 | ||
347 | wxTCPConnection::wxTCPConnection(wxChar *buffer, int size) | |
348 | : wxConnectionBase(buffer, size) | |
349 | { | |
350 | m_sock = NULL; | |
351 | m_sockstrm = NULL; | |
352 | m_codeci = NULL; | |
353 | m_codeco = NULL; | |
354 | } | |
355 | ||
356 | wxTCPConnection::~wxTCPConnection () | |
357 | { | |
358 | Disconnect(); | |
359 | wxDELETE(m_codeci); | |
360 | wxDELETE(m_codeco); | |
361 | wxDELETE(m_sockstrm); | |
362 | ||
363 | if (m_sock) | |
364 | { | |
365 | m_sock->SetClientData(NULL); | |
366 | m_sock->Destroy(); | |
367 | } | |
368 | } | |
369 | ||
370 | void wxTCPConnection::Compress(bool WXUNUSED(on)) | |
371 | { | |
372 | // Use wxLZWStream | |
373 | } | |
374 | ||
375 | // Calls that CLIENT can make. | |
376 | bool wxTCPConnection::Disconnect () | |
377 | { | |
378 | if ( !GetConnected() ) | |
379 | return true; | |
380 | // Send the the disconnect message to the peer. | |
381 | m_codeco->Write8(IPC_DISCONNECT); | |
382 | m_sock->Notify(false); | |
383 | m_sock->Close(); | |
384 | SetConnected(false); | |
385 | ||
386 | return true; | |
387 | } | |
388 | ||
389 | bool wxTCPConnection::Execute(const wxChar *data, int size, wxIPCFormat format) | |
390 | { | |
391 | if (!m_sock->IsConnected()) | |
392 | return false; | |
393 | ||
394 | // Prepare EXECUTE message | |
395 | m_codeco->Write8(IPC_EXECUTE); | |
396 | m_codeco->Write8(format); | |
397 | ||
398 | if (size < 0) | |
399 | size = (wxStrlen(data) + 1) * sizeof(wxChar); // includes final NUL | |
400 | ||
401 | m_codeco->Write32(size); | |
402 | m_sockstrm->Write(data, size); | |
403 | ||
404 | return true; | |
405 | } | |
406 | ||
407 | wxChar *wxTCPConnection::Request (const wxString& item, int *size, wxIPCFormat format) | |
408 | { | |
409 | if (!m_sock->IsConnected()) | |
410 | return NULL; | |
411 | ||
412 | m_codeco->Write8(IPC_REQUEST); | |
413 | m_codeco->WriteString(item); | |
414 | m_codeco->Write8(format); | |
415 | ||
416 | // If Unpack doesn't initialize it. | |
417 | int ret; | |
418 | ||
419 | ret = m_codeci->Read8(); | |
420 | if (ret == IPC_FAIL) | |
421 | return NULL; | |
422 | else | |
423 | { | |
424 | size_t s; | |
425 | ||
426 | s = m_codeci->Read32(); | |
427 | wxChar *data = GetBufferAtLeast( s ); | |
428 | wxASSERT_MSG(data != NULL, | |
429 | _T("Buffer too small in wxTCPConnection::Request") ); | |
430 | m_sockstrm->Read(data, s); | |
431 | ||
432 | if (size) | |
433 | *size = s; | |
434 | return data; | |
435 | } | |
436 | } | |
437 | ||
438 | bool wxTCPConnection::Poke (const wxString& item, wxChar *data, int size, wxIPCFormat format) | |
439 | { | |
440 | if (!m_sock->IsConnected()) | |
441 | return false; | |
442 | ||
443 | m_codeco->Write8(IPC_POKE); | |
444 | m_codeco->WriteString(item); | |
445 | m_codeco->Write8(format); | |
446 | ||
447 | if (size < 0) | |
448 | size = (wxStrlen(data) + 1) * sizeof(wxChar); // includes final NUL | |
449 | ||
450 | m_codeco->Write32(size); | |
451 | m_sockstrm->Write(data, size); | |
452 | ||
453 | return true; | |
454 | } | |
455 | ||
456 | bool wxTCPConnection::StartAdvise (const wxString& item) | |
457 | { | |
458 | int ret; | |
459 | ||
460 | if (!m_sock->IsConnected()) | |
461 | return false; | |
462 | ||
463 | m_codeco->Write8(IPC_ADVISE_START); | |
464 | m_codeco->WriteString(item); | |
465 | ||
466 | ret = m_codeci->Read8(); | |
467 | ||
468 | if (ret != IPC_FAIL) | |
469 | return true; | |
470 | else | |
471 | return false; | |
472 | } | |
473 | ||
474 | bool wxTCPConnection::StopAdvise (const wxString& item) | |
475 | { | |
476 | int msg; | |
477 | ||
478 | if (!m_sock->IsConnected()) | |
479 | return false; | |
480 | ||
481 | m_codeco->Write8(IPC_ADVISE_STOP); | |
482 | m_codeco->WriteString(item); | |
483 | ||
484 | msg = m_codeci->Read8(); | |
485 | ||
486 | if (msg != IPC_FAIL) | |
487 | return true; | |
488 | else | |
489 | return false; | |
490 | } | |
491 | ||
492 | // Calls that SERVER can make | |
493 | bool wxTCPConnection::Advise (const wxString& item, | |
494 | wxChar *data, int size, wxIPCFormat format) | |
495 | { | |
496 | if (!m_sock->IsConnected()) | |
497 | return false; | |
498 | ||
499 | m_codeco->Write8(IPC_ADVISE); | |
500 | m_codeco->WriteString(item); | |
501 | m_codeco->Write8(format); | |
502 | ||
503 | if (size < 0) | |
504 | size = (wxStrlen(data) + 1) * sizeof(wxChar); // includes final NUL | |
505 | ||
506 | m_codeco->Write32(size); | |
507 | m_sockstrm->Write(data, size); | |
508 | ||
509 | return true; | |
510 | } | |
511 | ||
512 | // -------------------------------------------------------------------------- | |
513 | // wxTCPEventHandler (private class) | |
514 | // -------------------------------------------------------------------------- | |
515 | ||
516 | BEGIN_EVENT_TABLE(wxTCPEventHandler, wxEvtHandler) | |
517 | EVT_SOCKET(_CLIENT_ONREQUEST_ID, wxTCPEventHandler::Client_OnRequest) | |
518 | EVT_SOCKET(_SERVER_ONREQUEST_ID, wxTCPEventHandler::Server_OnRequest) | |
519 | END_EVENT_TABLE() | |
520 | ||
521 | void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event) | |
522 | { | |
523 | wxSocketBase *sock = event.GetSocket(); | |
524 | wxSocketNotify evt = event.GetSocketEvent(); | |
525 | wxTCPConnection *connection = (wxTCPConnection *)(sock->GetClientData()); | |
526 | ||
527 | // This socket is being deleted; skip this event | |
528 | if (!connection) | |
529 | return; | |
530 | ||
531 | wxDataInputStream *codeci; | |
532 | wxDataOutputStream *codeco; | |
533 | wxSocketStream *sockstrm; | |
534 | wxString topic_name = connection->m_topic; | |
535 | wxString item; | |
536 | ||
537 | // We lost the connection: destroy everything | |
538 | if (evt == wxSOCKET_LOST) | |
539 | { | |
540 | sock->Notify(false); | |
541 | sock->Close(); | |
542 | connection->OnDisconnect(); | |
543 | return; | |
544 | } | |
545 | ||
546 | // Receive message number. | |
547 | codeci = connection->m_codeci; | |
548 | codeco = connection->m_codeco; | |
549 | sockstrm = connection->m_sockstrm; | |
550 | int msg = codeci->Read8(); | |
551 | ||
552 | switch (msg) | |
553 | { | |
554 | case IPC_EXECUTE: | |
555 | { | |
556 | wxChar *data; | |
557 | size_t size; | |
558 | wxIPCFormat format; | |
559 | ||
560 | format = (wxIPCFormat)codeci->Read8(); | |
561 | size = codeci->Read32(); | |
562 | data = connection->GetBufferAtLeast( size ); | |
563 | wxASSERT_MSG(data != NULL, | |
564 | _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") ); | |
565 | sockstrm->Read(data, size); | |
566 | ||
567 | connection->OnExecute (topic_name, data, size, format); | |
568 | ||
569 | break; | |
570 | } | |
571 | case IPC_ADVISE: | |
572 | { | |
573 | wxChar *data; | |
574 | size_t size; | |
575 | wxIPCFormat format; | |
576 | ||
577 | item = codeci->ReadString(); | |
578 | format = (wxIPCFormat)codeci->Read8(); | |
579 | size = codeci->Read32(); | |
580 | data = connection->GetBufferAtLeast( size ); | |
581 | wxASSERT_MSG(data != NULL, | |
582 | _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") ); | |
583 | sockstrm->Read(data, size); | |
584 | ||
585 | connection->OnAdvise (topic_name, item, data, size, format); | |
586 | ||
587 | break; | |
588 | } | |
589 | case IPC_ADVISE_START: | |
590 | { | |
591 | item = codeci->ReadString(); | |
592 | ||
593 | bool ok = connection->OnStartAdvise (topic_name, item); | |
594 | if (ok) | |
595 | codeco->Write8(IPC_ADVISE_START); | |
596 | else | |
597 | codeco->Write8(IPC_FAIL); | |
598 | ||
599 | break; | |
600 | } | |
601 | case IPC_ADVISE_STOP: | |
602 | { | |
603 | item = codeci->ReadString(); | |
604 | ||
605 | bool ok = connection->OnStopAdvise (topic_name, item); | |
606 | if (ok) | |
607 | codeco->Write8(IPC_ADVISE_STOP); | |
608 | else | |
609 | codeco->Write8(IPC_FAIL); | |
610 | ||
611 | break; | |
612 | } | |
613 | case IPC_POKE: | |
614 | { | |
615 | wxIPCFormat format; | |
616 | size_t size; | |
617 | wxChar *data; | |
618 | ||
619 | item = codeci->ReadString(); | |
620 | format = (wxIPCFormat)codeci->Read8(); | |
621 | size = codeci->Read32(); | |
622 | data = connection->GetBufferAtLeast( size ); | |
623 | wxASSERT_MSG(data != NULL, | |
624 | _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") ); | |
625 | sockstrm->Read(data, size); | |
626 | ||
627 | connection->OnPoke (topic_name, item, data, size, format); | |
628 | ||
629 | break; | |
630 | } | |
631 | case IPC_REQUEST: | |
632 | { | |
633 | wxIPCFormat format; | |
634 | ||
635 | item = codeci->ReadString(); | |
636 | format = (wxIPCFormat)codeci->Read8(); | |
637 | ||
638 | int user_size = -1; | |
639 | wxChar *user_data = connection->OnRequest (topic_name, item, &user_size, format); | |
640 | ||
641 | if (user_data) | |
642 | { | |
643 | codeco->Write8(IPC_REQUEST_REPLY); | |
644 | ||
645 | if (user_size == -1) | |
646 | user_size = (wxStrlen(user_data) + 1) * sizeof(wxChar); // includes final NUL | |
647 | ||
648 | codeco->Write32(user_size); | |
649 | sockstrm->Write(user_data, user_size); | |
650 | } | |
651 | else | |
652 | codeco->Write8(IPC_FAIL); | |
653 | ||
654 | break; | |
655 | } | |
656 | case IPC_DISCONNECT: | |
657 | { | |
658 | sock->Notify(false); | |
659 | sock->Close(); | |
660 | connection->SetConnected(false); | |
661 | connection->OnDisconnect(); | |
662 | break; | |
663 | } | |
664 | default: | |
665 | codeco->Write8(IPC_FAIL); | |
666 | break; | |
667 | } | |
668 | } | |
669 | ||
670 | void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event) | |
671 | { | |
672 | wxSocketServer *server = (wxSocketServer *) event.GetSocket(); | |
673 | wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData(); | |
674 | ||
675 | // This socket is being deleted; skip this event | |
676 | if (!ipcserv) | |
677 | return; | |
678 | ||
679 | if (event.GetSocketEvent() != wxSOCKET_CONNECTION) | |
680 | return; | |
681 | ||
682 | // Accept the connection, getting a new socket | |
683 | wxSocketBase *sock = server->Accept(); | |
684 | if (!sock->Ok()) | |
685 | { | |
686 | sock->Destroy(); | |
687 | return; | |
688 | } | |
689 | ||
690 | wxSocketStream *stream = new wxSocketStream(*sock); | |
691 | wxDataInputStream *codeci = new wxDataInputStream(*stream); | |
692 | wxDataOutputStream *codeco = new wxDataOutputStream(*stream); | |
693 | ||
694 | int msg; | |
695 | msg = codeci->Read8(); | |
696 | ||
697 | if (msg == IPC_CONNECT) | |
698 | { | |
699 | wxString topic_name; | |
700 | topic_name = codeci->ReadString(); | |
701 | ||
702 | wxTCPConnection *new_connection = | |
703 | (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name); | |
704 | ||
705 | if (new_connection) | |
706 | { | |
707 | if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) | |
708 | { | |
709 | // Acknowledge success | |
710 | codeco->Write8(IPC_CONNECT); | |
711 | new_connection->m_topic = topic_name; | |
712 | new_connection->m_sock = sock; | |
713 | new_connection->m_sockstrm = stream; | |
714 | new_connection->m_codeci = codeci; | |
715 | new_connection->m_codeco = codeco; | |
716 | sock->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); | |
717 | sock->SetClientData(new_connection); | |
718 | sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); | |
719 | sock->Notify(true); | |
720 | return; | |
721 | } | |
722 | else | |
723 | { | |
724 | delete new_connection; | |
725 | // and fall through to delete everything else | |
726 | } | |
727 | } | |
728 | } | |
729 | ||
730 | // Something went wrong, send failure message and delete everything | |
731 | codeco->Write8(IPC_FAIL); | |
732 | ||
733 | delete codeco; | |
734 | delete codeci; | |
735 | delete stream; | |
736 | sock->Destroy(); | |
737 | } | |
738 | ||
739 | // -------------------------------------------------------------------------- | |
740 | // wxTCPEventHandlerModule (private class) | |
741 | // -------------------------------------------------------------------------- | |
742 | ||
743 | class wxTCPEventHandlerModule: public wxModule | |
744 | { | |
745 | DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule) | |
746 | ||
747 | public: | |
748 | bool OnInit() { gs_handler = new wxTCPEventHandler(); return true; } | |
749 | void OnExit() { wxDELETE(gs_handler); } | |
750 | }; | |
751 | ||
752 | IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule, wxModule) | |
753 | ||
754 | ||
755 | #endif | |
756 | // wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS |