]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckipc.cpp
fix for removal of dosyacc.c
[wxWidgets.git] / src / common / sckipc.cpp
CommitLineData
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>
55d99c7a 14// Licence: wxWindows licence
f4ada568
GL
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
58enum
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
88static wxSockAddress *
89GetAddressFromName(const wxString& serverName, const wxString& host = _T(""))
90{
91 // we always use INET sockets under non-Unix systems
5283098e 92#if defined(__UNIX__) && !defined(__WXMAC__) && !defined(__WINE__)
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
119class wxTCPEventHandler : public wxEvtHandler
120{
121public:
122 wxTCPEventHandler() : wxEvtHandler() {};
123
124 void Client_OnRequest(wxSocketEvent& event);
125 void Server_OnRequest(wxSocketEvent& event);
126
127 DECLARE_EVENT_TABLE()
128};
129
130enum
131{
132 _CLIENT_ONREQUEST_ID = 1000,
133 _SERVER_ONREQUEST_ID
134};
135
136static wxTCPEventHandler *gs_handler = NULL;
137
138// ==========================================================================
139// implementation
140// ==========================================================================
141
142IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase)
143IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase)
144IMPLEMENT_CLASS(wxTCPConnection, wxConnectionBase)
145
146// --------------------------------------------------------------------------
f4ada568 147// wxTCPClient
cdc59bb6 148// --------------------------------------------------------------------------
f4ada568 149
cdc59bb6 150wxTCPClient::wxTCPClient () : wxClientBase()
f4ada568
GL
151{
152}
153
6e31e940 154wxTCPClient::~wxTCPClient ()
f4ada568
GL
155{
156}
157
158bool wxTCPClient::ValidHost(const wxString& host)
159{
160 wxIPV4address addr;
161
162 return addr.Hostname(host);
163}
164
165wxConnectionBase *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
229wxConnectionBase *wxTCPClient::OnMakeConnection()
230{
e5b502f3 231 return new wxTCPConnection();
f4ada568
GL
232}
233
cdc59bb6 234// --------------------------------------------------------------------------
f4ada568 235// wxTCPServer
cdc59bb6 236// --------------------------------------------------------------------------
f4ada568 237
cdc59bb6 238wxTCPServer::wxTCPServer () : wxServerBase()
f4ada568 239{
f6bcfd97 240 m_server = NULL;
f4ada568
GL
241}
242
f6bcfd97 243bool 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
401eb3de 263 int rc = remove(serverName.fn_str());
0dbfd66d
VZ
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 313wxTCPServer::~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 {
401eb3de 324 if ( remove(m_filename.fn_str()) != 0 )
0dbfd66d
VZ
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 332wxConnectionBase *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 341wxTCPConnection::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
b814b812
JS
349wxTCPConnection::wxTCPConnection(wxChar *buffer, int size)
350 : wxConnectionBase(buffer, size)
7921cf2b 351{
b814b812
JS
352 m_sock = NULL;
353 m_sockstrm = NULL;
354 m_codeci = NULL;
355 m_codeco = NULL;
7921cf2b
JS
356}
357
6e31e940 358wxTCPConnection::~wxTCPConnection ()
f4ada568 359{
b814b812 360 Disconnect();
75ed1d15
GL
361 wxDELETE(m_codeci);
362 wxDELETE(m_codeco);
f4ada568 363 wxDELETE(m_sockstrm);
3adb47a9 364
e5b502f3
GRG
365 if (m_sock)
366 {
367 m_sock->SetClientData(NULL);
368 m_sock->Destroy();
369 }
f4ada568
GL
370}
371
cb43b372 372void wxTCPConnection::Compress(bool WXUNUSED(on))
f4ada568
GL
373{
374 // Use wxLZWStream
375}
376
377// Calls that CLIENT can make.
6e31e940 378bool wxTCPConnection::Disconnect ()
f4ada568 379{
b814b812
JS
380 if ( !GetConnected() )
381 return TRUE;
f4ada568 382 // Send the the disconnect message to the peer.
75ed1d15 383 m_codeco->Write8(IPC_DISCONNECT);
cdc59bb6 384 m_sock->Notify(FALSE);
f4ada568 385 m_sock->Close();
b7282ad2 386 SetConnected(FALSE);
f4ada568
GL
387
388 return TRUE;
389}
390
0834112f 391bool wxTCPConnection::Execute(const wxChar *data, int size, wxIPCFormat format)
f4ada568
GL
392{
393 if (!m_sock->IsConnected())
394 return FALSE;
395
396 // Prepare EXECUTE message
75ed1d15
GL
397 m_codeco->Write8(IPC_EXECUTE);
398 m_codeco->Write8(format);
0834112f 399
f4ada568 400 if (size < 0)
f6bcfd97 401 size = wxStrlen(data) + 1; // includes final NUL
0834112f
GRG
402
403 m_codeco->Write32(size);
404 m_sockstrm->Write(data, size);
f4ada568
GL
405
406 return TRUE;
407}
408
d38e8d5f 409wxChar *wxTCPConnection::Request (const wxString& item, int *size, wxIPCFormat format)
f4ada568
GL
410{
411 if (!m_sock->IsConnected())
412 return NULL;
413
75ed1d15
GL
414 m_codeco->Write8(IPC_REQUEST);
415 m_codeco->WriteString(item);
416 m_codeco->Write8(format);
f4ada568
GL
417
418 // If Unpack doesn't initialize it.
419 int ret;
420
75ed1d15 421 ret = m_codeci->Read8();
f4ada568
GL
422 if (ret == IPC_FAIL)
423 return NULL;
0834112f
GRG
424 else
425 {
f4ada568 426 size_t s;
f4ada568 427
75ed1d15 428 s = m_codeci->Read32();
b814b812
JS
429 wxChar *data = GetBufferAtLeast( s );
430 wxASSERT_MSG(data != NULL,
431 _T("Buffer too small in wxTCPConnection::Request") );
fae05df5 432 m_sockstrm->Read(data, s);
f4ada568
GL
433
434 if (size)
435 *size = s;
436 return data;
437 }
438}
439
783b6cfd 440bool wxTCPConnection::Poke (const wxString& item, wxChar *data, int size, wxIPCFormat format)
f4ada568
GL
441{
442 if (!m_sock->IsConnected())
443 return FALSE;
444
75ed1d15
GL
445 m_codeco->Write8(IPC_POKE);
446 m_codeco->WriteString(item);
447 m_codeco->Write8(format);
0834112f 448
f4ada568 449 if (size < 0)
f6bcfd97 450 size = wxStrlen(data) + 1; // includes final NUL
0834112f
GRG
451
452 m_codeco->Write32(size);
453 m_sockstrm->Write(data, size);
f4ada568
GL
454
455 return TRUE;
456}
457
458bool wxTCPConnection::StartAdvise (const wxString& item)
459{
460 int ret;
461
462 if (!m_sock->IsConnected())
463 return FALSE;
464
75ed1d15
GL
465 m_codeco->Write8(IPC_ADVISE_START);
466 m_codeco->WriteString(item);
f4ada568 467
75ed1d15 468 ret = m_codeci->Read8();
f4ada568
GL
469
470 if (ret != IPC_FAIL)
471 return TRUE;
472 else
473 return FALSE;
474}
475
476bool wxTCPConnection::StopAdvise (const wxString& item)
477{
478 int msg;
479
480 if (!m_sock->IsConnected())
481 return FALSE;
482
75ed1d15
GL
483 m_codeco->Write8(IPC_ADVISE_STOP);
484 m_codeco->WriteString(item);
f4ada568 485
75ed1d15 486 msg = m_codeci->Read8();
f4ada568
GL
487
488 if (msg != IPC_FAIL)
489 return TRUE;
490 else
491 return FALSE;
492}
493
494// Calls that SERVER can make
495bool wxTCPConnection::Advise (const wxString& item,
783b6cfd 496 wxChar *data, int size, wxIPCFormat format)
f4ada568
GL
497{
498 if (!m_sock->IsConnected())
499 return FALSE;
500
75ed1d15
GL
501 m_codeco->Write8(IPC_ADVISE);
502 m_codeco->WriteString(item);
503 m_codeco->Write8(format);
0834112f 504
f4ada568 505 if (size < 0)
f6bcfd97 506 size = wxStrlen(data) + 1; // includes final NUL
0834112f
GRG
507
508 m_codeco->Write32(size);
509 m_sockstrm->Write(data, size);
f4ada568
GL
510
511 return TRUE;
512}
513
cdc59bb6
GRG
514// --------------------------------------------------------------------------
515// wxTCPEventHandler (private class)
516// --------------------------------------------------------------------------
517
518BEGIN_EVENT_TABLE(wxTCPEventHandler, wxEvtHandler)
519 EVT_SOCKET(_CLIENT_ONREQUEST_ID, wxTCPEventHandler::Client_OnRequest)
520 EVT_SOCKET(_SERVER_ONREQUEST_ID, wxTCPEventHandler::Server_OnRequest)
521END_EVENT_TABLE()
522
523void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event)
f4ada568 524{
cdc59bb6
GRG
525 wxSocketBase *sock = event.GetSocket();
526 wxSocketNotify evt = event.GetSocketEvent();
1f0500b3 527 wxTCPConnection *connection = (wxTCPConnection *)(sock->GetClientData());
cdc59bb6 528
e5b502f3
GRG
529 // This socket is being deleted; skip this event
530 if (!connection)
531 return;
532
f4ada568 533 int msg = 0;
75ed1d15 534 wxDataInputStream *codeci;
26a25f95 535 wxDataOutputStream *codeco;
fae05df5 536 wxSocketStream *sockstrm;
f4ada568
GL
537 wxString topic_name = connection->m_topic;
538 wxString item;
539
e5b502f3 540 // We lost the connection: destroy everything
0834112f
GRG
541 if (evt == wxSOCKET_LOST)
542 {
cdc59bb6
GRG
543 sock->Notify(FALSE);
544 sock->Close();
f4ada568
GL
545 connection->OnDisconnect();
546 return;
547 }
548
549 // Receive message number.
75ed1d15
GL
550 codeci = connection->m_codeci;
551 codeco = connection->m_codeco;
fae05df5 552 sockstrm = connection->m_sockstrm;
75ed1d15 553 msg = codeci->Read8();
f4ada568 554
0834112f
GRG
555 switch (msg)
556 {
557 case IPC_EXECUTE:
558 {
d38e8d5f 559 wxChar *data;
26a25f95 560 size_t size;
0d2a2b60 561 wxIPCFormat format;
26a25f95 562
0d2a2b60 563 format = (wxIPCFormat)codeci->Read8();
75ed1d15 564 size = codeci->Read32();
b814b812
JS
565 data = connection->GetBufferAtLeast( size );
566 wxASSERT_MSG(data != NULL,
567 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
fae05df5 568 sockstrm->Read(data, size);
f4ada568 569
e0fbcda6 570 connection->OnExecute (topic_name, data, size, format);
f4ada568 571
f4ada568
GL
572 break;
573 }
0834112f
GRG
574 case IPC_ADVISE:
575 {
d38e8d5f 576 wxChar *data;
f4ada568 577 size_t size;
0d2a2b60 578 wxIPCFormat format;
f4ada568 579
75ed1d15 580 item = codeci->ReadString();
0d2a2b60 581 format = (wxIPCFormat)codeci->Read8();
75ed1d15 582 size = codeci->Read32();
b814b812
JS
583 data = connection->GetBufferAtLeast( size );
584 wxASSERT_MSG(data != NULL,
585 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
fae05df5 586 sockstrm->Read(data, size);
26a25f95 587
e0fbcda6 588 connection->OnAdvise (topic_name, item, data, size, format);
f4ada568 589
f4ada568
GL
590 break;
591 }
0834112f
GRG
592 case IPC_ADVISE_START:
593 {
75ed1d15 594 item = codeci->ReadString();
f4ada568
GL
595
596 bool ok = connection->OnStartAdvise (topic_name, item);
597 if (ok)
75ed1d15 598 codeco->Write8(IPC_ADVISE_START);
f4ada568 599 else
75ed1d15 600 codeco->Write8(IPC_FAIL);
f4ada568
GL
601
602 break;
603 }
0834112f
GRG
604 case IPC_ADVISE_STOP:
605 {
75ed1d15 606 item = codeci->ReadString();
f4ada568
GL
607
608 bool ok = connection->OnStopAdvise (topic_name, item);
609 if (ok)
75ed1d15 610 codeco->Write8(IPC_ADVISE_STOP);
f4ada568 611 else
75ed1d15 612 codeco->Write8(IPC_FAIL);
f4ada568
GL
613
614 break;
615 }
0834112f
GRG
616 case IPC_POKE:
617 {
0d2a2b60 618 wxIPCFormat format;
f4ada568 619 size_t size;
783b6cfd 620 wxChar *data;
f4ada568 621
75ed1d15 622 item = codeci->ReadString();
0d2a2b60 623 format = (wxIPCFormat)codeci->Read8();
75ed1d15 624 size = codeci->Read32();
b814b812
JS
625 data = connection->GetBufferAtLeast( size );
626 wxASSERT_MSG(data != NULL,
627 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
fae05df5 628 sockstrm->Read(data, size);
26a25f95 629
f4ada568
GL
630 connection->OnPoke (topic_name, item, data, size, format);
631
f4ada568
GL
632 break;
633 }
0834112f
GRG
634 case IPC_REQUEST:
635 {
0d2a2b60 636 wxIPCFormat format;
f4ada568 637
75ed1d15 638 item = codeci->ReadString();
0d2a2b60 639 format = (wxIPCFormat)codeci->Read8();
f4ada568
GL
640
641 int user_size = -1;
d38e8d5f 642 wxChar *user_data = connection->OnRequest (topic_name, item, &user_size, format);
f4ada568 643
0834112f
GRG
644 if (user_data)
645 {
75ed1d15 646 codeco->Write8(IPC_REQUEST_REPLY);
0834112f
GRG
647
648 if (user_size == -1)
d38e8d5f 649 user_size = wxStrlen(user_data) + 1; // includes final NUL
0834112f
GRG
650
651 codeco->Write32(user_size);
652 sockstrm->Write(user_data, user_size);
653 }
654 else
75ed1d15 655 codeco->Write8(IPC_FAIL);
f4ada568
GL
656
657 break;
658 }
0834112f
GRG
659 case IPC_DISCONNECT:
660 {
cdc59bb6
GRG
661 sock->Notify(FALSE);
662 sock->Close();
b7282ad2 663 connection->SetConnected(FALSE);
f4ada568
GL
664 connection->OnDisconnect();
665 break;
666 }
667 default:
75ed1d15 668 codeco->Write8(IPC_FAIL);
f4ada568
GL
669 break;
670 }
671}
672
cdc59bb6 673void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event)
f4ada568 674{
cdc59bb6 675 wxSocketServer *server = (wxSocketServer *) event.GetSocket();
f6bcfd97 676 wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData();
f4ada568 677
e5b502f3
GRG
678 // This socket is being deleted; skip this event
679 if (!ipcserv)
680 return;
681
cdc59bb6 682 if (event.GetSocketEvent() != wxSOCKET_CONNECTION)
f4ada568
GL
683 return;
684
3adb47a9 685 // Accept the connection, getting a new socket
cdc59bb6 686 wxSocketBase *sock = server->Accept();
0834112f 687 if (!sock->Ok())
3adb47a9
GRG
688 {
689 sock->Destroy();
0834112f 690 return;
3adb47a9 691 }
f4ada568 692
cdc59bb6
GRG
693 wxSocketStream *stream = new wxSocketStream(*sock);
694 wxDataInputStream *codeci = new wxDataInputStream(*stream);
695 wxDataOutputStream *codeco = new wxDataOutputStream(*stream);
f4ada568 696
f4ada568 697 int msg;
75ed1d15 698 msg = codeci->Read8();
f4ada568 699
0834112f
GRG
700 if (msg == IPC_CONNECT)
701 {
f4ada568 702 wxString topic_name;
75ed1d15 703 topic_name = codeci->ReadString();
f4ada568 704
f4ada568
GL
705 wxTCPConnection *new_connection =
706 (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name);
3adb47a9 707
0834112f
GRG
708 if (new_connection)
709 {
3adb47a9 710 if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection)))
0834112f 711 {
3adb47a9
GRG
712 // Acknowledge success
713 codeco->Write8(IPC_CONNECT);
714 new_connection->m_topic = topic_name;
26a25f95 715 new_connection->m_sock = sock;
3adb47a9
GRG
716 new_connection->m_sockstrm = stream;
717 new_connection->m_codeci = codeci;
718 new_connection->m_codeco = codeco;
cdc59bb6
GRG
719 sock->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID);
720 sock->SetClientData(new_connection);
3adb47a9
GRG
721 sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
722 sock->Notify(TRUE);
f4ada568
GL
723 return;
724 }
3adb47a9
GRG
725 else
726 {
727 delete new_connection;
728 // and fall through to delete everything else
729 }
f4ada568
GL
730 }
731 }
3adb47a9
GRG
732
733 // Something went wrong, send failure message and delete everything
734 codeco->Write8(IPC_FAIL);
735
736 delete codeco;
737 delete codeci;
738 delete stream;
739 sock->Destroy();
f4ada568 740}
35a4dab7 741
cdc59bb6
GRG
742// --------------------------------------------------------------------------
743// wxTCPEventHandlerModule (private class)
744// --------------------------------------------------------------------------
745
746class WXDLLEXPORT wxTCPEventHandlerModule: public wxModule
747{
748 DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule)
749
750public:
751 bool OnInit() { gs_handler = new wxTCPEventHandler(); return TRUE; }
752 void OnExit() { wxDELETE(gs_handler); }
753};
754
755IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule, wxModule)
756
757
35a4dab7 758#endif
3adb47a9 759 // wxUSE_SOCKETS && wxUSE_IPC