]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckipc.cpp
return the index of the inserted/appended item
[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
14f355c2 25#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f4ada568
GL
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()
fc7a2a60 128 DECLARE_NO_COPY_CLASS(wxTCPEventHandler)
cdc59bb6
GRG
129};
130
131enum
132{
133 _CLIENT_ONREQUEST_ID = 1000,
134 _SERVER_ONREQUEST_ID
135};
136
137static wxTCPEventHandler *gs_handler = NULL;
138
139// ==========================================================================
140// implementation
141// ==========================================================================
142
143IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase)
144IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase)
145IMPLEMENT_CLASS(wxTCPConnection, wxConnectionBase)
146
147// --------------------------------------------------------------------------
f4ada568 148// wxTCPClient
cdc59bb6 149// --------------------------------------------------------------------------
f4ada568 150
cdc59bb6 151wxTCPClient::wxTCPClient () : wxClientBase()
f4ada568
GL
152{
153}
154
6e31e940 155wxTCPClient::~wxTCPClient ()
f4ada568
GL
156{
157}
158
159bool wxTCPClient::ValidHost(const wxString& host)
160{
161 wxIPV4address addr;
162
163 return addr.Hostname(host);
164}
165
166wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
0dbfd66d 167 const wxString& serverName,
f4ada568
GL
168 const wxString& topic)
169{
d3ea6527 170 wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS);
f4ada568 171 wxSocketStream *stream = new wxSocketStream(*client);
0834112f
GRG
172 wxDataInputStream *data_is = new wxDataInputStream(*stream);
173 wxDataOutputStream *data_os = new wxDataOutputStream(*stream);
174
0dbfd66d
VZ
175 wxSockAddress *addr = GetAddressFromName(serverName, host);
176 if ( !addr )
177 return NULL;
178
179 bool ok = client->Connect(*addr);
180 delete addr;
f4ada568 181
0dbfd66d 182 if ( ok )
0834112f
GRG
183 {
184 unsigned char msg;
26a25f95 185
0834112f
GRG
186 // Send topic name, and enquire whether this has succeeded
187 data_os->Write8(IPC_CONNECT);
188 data_os->WriteString(topic);
26a25f95 189
0834112f
GRG
190 msg = data_is->Read8();
191
192 // OK! Confirmation.
193 if (msg == IPC_CONNECT)
194 {
195 wxTCPConnection *connection = (wxTCPConnection *)OnMakeConnection ();
196
197 if (connection)
198 {
3adb47a9 199 if (connection->IsKindOf(CLASSINFO(wxTCPConnection)))
0834112f
GRG
200 {
201 connection->m_topic = topic;
202 connection->m_sock = client;
203 connection->m_sockstrm = stream;
204 connection->m_codeci = data_is;
205 connection->m_codeco = data_os;
cdc59bb6
GRG
206 client->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID);
207 client->SetClientData(connection);
0834112f
GRG
208 client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
209 client->Notify(TRUE);
210 return connection;
211 }
3adb47a9
GRG
212 else
213 {
214 delete connection;
215 // and fall through to delete everything else
216 }
f4ada568 217 }
f4ada568 218 }
f4ada568 219 }
0834112f 220
3adb47a9 221 // Something went wrong, delete everything
0834112f
GRG
222 delete data_is;
223 delete data_os;
224 delete stream;
3adb47a9
GRG
225 client->Destroy();
226
0834112f 227 return NULL;
f4ada568
GL
228}
229
230wxConnectionBase *wxTCPClient::OnMakeConnection()
231{
e5b502f3 232 return new wxTCPConnection();
f4ada568
GL
233}
234
cdc59bb6 235// --------------------------------------------------------------------------
f4ada568 236// wxTCPServer
cdc59bb6 237// --------------------------------------------------------------------------
f4ada568 238
cdc59bb6 239wxTCPServer::wxTCPServer () : wxServerBase()
f4ada568 240{
f6bcfd97 241 m_server = NULL;
f4ada568
GL
242}
243
f6bcfd97 244bool wxTCPServer::Create(const wxString& serverName)
f4ada568 245{
f6bcfd97
BP
246 // Destroy previous server, if any
247 if (m_server)
248 {
249 m_server->SetClientData(NULL);
250 m_server->Destroy();
251 m_server = NULL;
252 }
f4ada568 253
0dbfd66d
VZ
254 wxSockAddress *addr = GetAddressFromName(serverName);
255 if ( !addr )
256 return FALSE;
257
258#ifdef __UNIX_LIKE__
259 mode_t umaskOld;
260 if ( addr->Type() == wxSockAddress::UNIX )
261 {
262 // ensure that the file doesn't exist as otherwise calling socket() would
263 // fail
401eb3de 264 int rc = remove(serverName.fn_str());
0dbfd66d
VZ
265 if ( rc < 0 && errno != ENOENT )
266 {
267 delete addr;
268
269 return FALSE;
270 }
271
272 // also set the umask to prevent the others from reading our file
273 umaskOld = umask(077);
274 }
275 else
276 {
277 // unused anyhow but shut down the compiler warnings
278 umaskOld = 0;
279 }
280#endif // __UNIX_LIKE__
f4ada568 281
f6bcfd97 282 // Create a socket listening on the specified port
0dbfd66d
VZ
283 m_server = new wxSocketServer(*addr, SCKIPC_FLAGS);
284
285#ifdef __UNIX_LIKE__
286 if ( addr->Type() == wxSockAddress::UNIX )
287 {
288 // restore the umask
289 umask(umaskOld);
290
291 // save the file name to remove it later
292 m_filename = serverName;
293 }
294#endif // __UNIX_LIKE__
295
296 delete addr;
f6bcfd97
BP
297
298 if (!m_server->Ok())
299 {
300 m_server->Destroy();
301 m_server = NULL;
302
303 return FALSE;
304 }
305
306 m_server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID);
307 m_server->SetClientData(this);
308 m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
309 m_server->Notify(TRUE);
f4ada568
GL
310
311 return TRUE;
312}
313
6e31e940 314wxTCPServer::~wxTCPServer()
f4ada568 315{
0dbfd66d
VZ
316 if (m_server)
317 {
318 m_server->SetClientData(NULL);
319 m_server->Destroy();
320 }
321
322#ifdef __UNIX_LIKE__
323 if ( !m_filename.empty() )
324 {
401eb3de 325 if ( remove(m_filename.fn_str()) != 0 )
0dbfd66d
VZ
326 {
327 wxLogDebug(_T("Stale AF_UNIX file '%s' left."), m_filename.c_str());
328 }
329 }
330#endif // __UNIX_LIKE__
f4ada568
GL
331}
332
e22036dc 333wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) )
f4ada568
GL
334{
335 return new wxTCPConnection();
336}
337
cdc59bb6 338// --------------------------------------------------------------------------
f4ada568 339// wxTCPConnection
cdc59bb6 340// --------------------------------------------------------------------------
f4ada568 341
cdc59bb6 342wxTCPConnection::wxTCPConnection () : wxConnectionBase()
f4ada568 343{
cdc59bb6
GRG
344 m_sock = NULL;
345 m_sockstrm = NULL;
346 m_codeci = NULL;
347 m_codeco = NULL;
f4ada568
GL
348}
349
b814b812
JS
350wxTCPConnection::wxTCPConnection(wxChar *buffer, int size)
351 : wxConnectionBase(buffer, size)
7921cf2b 352{
b814b812
JS
353 m_sock = NULL;
354 m_sockstrm = NULL;
355 m_codeci = NULL;
356 m_codeco = NULL;
7921cf2b
JS
357}
358
6e31e940 359wxTCPConnection::~wxTCPConnection ()
f4ada568 360{
b814b812 361 Disconnect();
75ed1d15
GL
362 wxDELETE(m_codeci);
363 wxDELETE(m_codeco);
f4ada568 364 wxDELETE(m_sockstrm);
3adb47a9 365
e5b502f3
GRG
366 if (m_sock)
367 {
368 m_sock->SetClientData(NULL);
369 m_sock->Destroy();
370 }
f4ada568
GL
371}
372
cb43b372 373void wxTCPConnection::Compress(bool WXUNUSED(on))
f4ada568
GL
374{
375 // Use wxLZWStream
376}
377
378// Calls that CLIENT can make.
6e31e940 379bool wxTCPConnection::Disconnect ()
f4ada568 380{
b814b812
JS
381 if ( !GetConnected() )
382 return TRUE;
f4ada568 383 // Send the the disconnect message to the peer.
75ed1d15 384 m_codeco->Write8(IPC_DISCONNECT);
cdc59bb6 385 m_sock->Notify(FALSE);
f4ada568 386 m_sock->Close();
b7282ad2 387 SetConnected(FALSE);
f4ada568
GL
388
389 return TRUE;
390}
391
0834112f 392bool wxTCPConnection::Execute(const wxChar *data, int size, wxIPCFormat format)
f4ada568
GL
393{
394 if (!m_sock->IsConnected())
395 return FALSE;
396
397 // Prepare EXECUTE message
75ed1d15
GL
398 m_codeco->Write8(IPC_EXECUTE);
399 m_codeco->Write8(format);
0834112f 400
f4ada568 401 if (size < 0)
f6bcfd97 402 size = wxStrlen(data) + 1; // includes final NUL
0834112f
GRG
403
404 m_codeco->Write32(size);
405 m_sockstrm->Write(data, size);
f4ada568
GL
406
407 return TRUE;
408}
409
d38e8d5f 410wxChar *wxTCPConnection::Request (const wxString& item, int *size, wxIPCFormat format)
f4ada568
GL
411{
412 if (!m_sock->IsConnected())
413 return NULL;
414
75ed1d15
GL
415 m_codeco->Write8(IPC_REQUEST);
416 m_codeco->WriteString(item);
417 m_codeco->Write8(format);
f4ada568
GL
418
419 // If Unpack doesn't initialize it.
420 int ret;
421
75ed1d15 422 ret = m_codeci->Read8();
f4ada568
GL
423 if (ret == IPC_FAIL)
424 return NULL;
0834112f
GRG
425 else
426 {
f4ada568 427 size_t s;
f4ada568 428
75ed1d15 429 s = m_codeci->Read32();
b814b812
JS
430 wxChar *data = GetBufferAtLeast( s );
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
783b6cfd 441bool wxTCPConnection::Poke (const wxString& item, wxChar *data, int size, wxIPCFormat format)
f4ada568
GL
442{
443 if (!m_sock->IsConnected())
444 return FALSE;
445
75ed1d15
GL
446 m_codeco->Write8(IPC_POKE);
447 m_codeco->WriteString(item);
448 m_codeco->Write8(format);
0834112f 449
f4ada568 450 if (size < 0)
f6bcfd97 451 size = wxStrlen(data) + 1; // includes final NUL
0834112f
GRG
452
453 m_codeco->Write32(size);
454 m_sockstrm->Write(data, size);
f4ada568
GL
455
456 return TRUE;
457}
458
459bool wxTCPConnection::StartAdvise (const wxString& item)
460{
461 int ret;
462
463 if (!m_sock->IsConnected())
464 return FALSE;
465
75ed1d15
GL
466 m_codeco->Write8(IPC_ADVISE_START);
467 m_codeco->WriteString(item);
f4ada568 468
75ed1d15 469 ret = m_codeci->Read8();
f4ada568
GL
470
471 if (ret != IPC_FAIL)
472 return TRUE;
473 else
474 return FALSE;
475}
476
477bool wxTCPConnection::StopAdvise (const wxString& item)
478{
479 int msg;
480
481 if (!m_sock->IsConnected())
482 return FALSE;
483
75ed1d15
GL
484 m_codeco->Write8(IPC_ADVISE_STOP);
485 m_codeco->WriteString(item);
f4ada568 486
75ed1d15 487 msg = m_codeci->Read8();
f4ada568
GL
488
489 if (msg != IPC_FAIL)
490 return TRUE;
491 else
492 return FALSE;
493}
494
495// Calls that SERVER can make
496bool wxTCPConnection::Advise (const wxString& item,
783b6cfd 497 wxChar *data, int size, wxIPCFormat format)
f4ada568
GL
498{
499 if (!m_sock->IsConnected())
500 return FALSE;
501
75ed1d15
GL
502 m_codeco->Write8(IPC_ADVISE);
503 m_codeco->WriteString(item);
504 m_codeco->Write8(format);
0834112f 505
f4ada568 506 if (size < 0)
f6bcfd97 507 size = wxStrlen(data) + 1; // includes final NUL
0834112f
GRG
508
509 m_codeco->Write32(size);
510 m_sockstrm->Write(data, size);
f4ada568
GL
511
512 return TRUE;
513}
514
cdc59bb6
GRG
515// --------------------------------------------------------------------------
516// wxTCPEventHandler (private class)
517// --------------------------------------------------------------------------
518
519BEGIN_EVENT_TABLE(wxTCPEventHandler, wxEvtHandler)
520 EVT_SOCKET(_CLIENT_ONREQUEST_ID, wxTCPEventHandler::Client_OnRequest)
521 EVT_SOCKET(_SERVER_ONREQUEST_ID, wxTCPEventHandler::Server_OnRequest)
522END_EVENT_TABLE()
523
524void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event)
f4ada568 525{
cdc59bb6
GRG
526 wxSocketBase *sock = event.GetSocket();
527 wxSocketNotify evt = event.GetSocketEvent();
1f0500b3 528 wxTCPConnection *connection = (wxTCPConnection *)(sock->GetClientData());
cdc59bb6 529
e5b502f3
GRG
530 // This socket is being deleted; skip this event
531 if (!connection)
532 return;
533
f4ada568 534 int msg = 0;
75ed1d15 535 wxDataInputStream *codeci;
26a25f95 536 wxDataOutputStream *codeco;
fae05df5 537 wxSocketStream *sockstrm;
f4ada568
GL
538 wxString topic_name = connection->m_topic;
539 wxString item;
540
e5b502f3 541 // We lost the connection: destroy everything
0834112f
GRG
542 if (evt == wxSOCKET_LOST)
543 {
cdc59bb6
GRG
544 sock->Notify(FALSE);
545 sock->Close();
f4ada568
GL
546 connection->OnDisconnect();
547 return;
548 }
549
550 // Receive message number.
75ed1d15
GL
551 codeci = connection->m_codeci;
552 codeco = connection->m_codeco;
fae05df5 553 sockstrm = connection->m_sockstrm;
75ed1d15 554 msg = codeci->Read8();
f4ada568 555
0834112f
GRG
556 switch (msg)
557 {
558 case IPC_EXECUTE:
559 {
d38e8d5f 560 wxChar *data;
26a25f95 561 size_t size;
0d2a2b60 562 wxIPCFormat format;
26a25f95 563
0d2a2b60 564 format = (wxIPCFormat)codeci->Read8();
75ed1d15 565 size = codeci->Read32();
b814b812
JS
566 data = connection->GetBufferAtLeast( size );
567 wxASSERT_MSG(data != NULL,
568 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
fae05df5 569 sockstrm->Read(data, size);
f4ada568 570
e0fbcda6 571 connection->OnExecute (topic_name, data, size, format);
f4ada568 572
f4ada568
GL
573 break;
574 }
0834112f
GRG
575 case IPC_ADVISE:
576 {
d38e8d5f 577 wxChar *data;
f4ada568 578 size_t size;
0d2a2b60 579 wxIPCFormat format;
f4ada568 580
75ed1d15 581 item = codeci->ReadString();
0d2a2b60 582 format = (wxIPCFormat)codeci->Read8();
75ed1d15 583 size = codeci->Read32();
b814b812
JS
584 data = connection->GetBufferAtLeast( size );
585 wxASSERT_MSG(data != NULL,
586 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
fae05df5 587 sockstrm->Read(data, size);
26a25f95 588
e0fbcda6 589 connection->OnAdvise (topic_name, item, data, size, format);
f4ada568 590
f4ada568
GL
591 break;
592 }
0834112f
GRG
593 case IPC_ADVISE_START:
594 {
75ed1d15 595 item = codeci->ReadString();
f4ada568
GL
596
597 bool ok = connection->OnStartAdvise (topic_name, item);
598 if (ok)
75ed1d15 599 codeco->Write8(IPC_ADVISE_START);
f4ada568 600 else
75ed1d15 601 codeco->Write8(IPC_FAIL);
f4ada568
GL
602
603 break;
604 }
0834112f
GRG
605 case IPC_ADVISE_STOP:
606 {
75ed1d15 607 item = codeci->ReadString();
f4ada568
GL
608
609 bool ok = connection->OnStopAdvise (topic_name, item);
610 if (ok)
75ed1d15 611 codeco->Write8(IPC_ADVISE_STOP);
f4ada568 612 else
75ed1d15 613 codeco->Write8(IPC_FAIL);
f4ada568
GL
614
615 break;
616 }
0834112f
GRG
617 case IPC_POKE:
618 {
0d2a2b60 619 wxIPCFormat format;
f4ada568 620 size_t size;
783b6cfd 621 wxChar *data;
f4ada568 622
75ed1d15 623 item = codeci->ReadString();
0d2a2b60 624 format = (wxIPCFormat)codeci->Read8();
75ed1d15 625 size = codeci->Read32();
b814b812
JS
626 data = connection->GetBufferAtLeast( size );
627 wxASSERT_MSG(data != NULL,
628 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
fae05df5 629 sockstrm->Read(data, size);
26a25f95 630
f4ada568
GL
631 connection->OnPoke (topic_name, item, data, size, format);
632
f4ada568
GL
633 break;
634 }
0834112f
GRG
635 case IPC_REQUEST:
636 {
0d2a2b60 637 wxIPCFormat format;
f4ada568 638
75ed1d15 639 item = codeci->ReadString();
0d2a2b60 640 format = (wxIPCFormat)codeci->Read8();
f4ada568
GL
641
642 int user_size = -1;
d38e8d5f 643 wxChar *user_data = connection->OnRequest (topic_name, item, &user_size, format);
f4ada568 644
0834112f
GRG
645 if (user_data)
646 {
75ed1d15 647 codeco->Write8(IPC_REQUEST_REPLY);
0834112f
GRG
648
649 if (user_size == -1)
d38e8d5f 650 user_size = wxStrlen(user_data) + 1; // includes final NUL
0834112f
GRG
651
652 codeco->Write32(user_size);
653 sockstrm->Write(user_data, user_size);
654 }
655 else
75ed1d15 656 codeco->Write8(IPC_FAIL);
f4ada568
GL
657
658 break;
659 }
0834112f
GRG
660 case IPC_DISCONNECT:
661 {
cdc59bb6
GRG
662 sock->Notify(FALSE);
663 sock->Close();
b7282ad2 664 connection->SetConnected(FALSE);
f4ada568
GL
665 connection->OnDisconnect();
666 break;
667 }
668 default:
75ed1d15 669 codeco->Write8(IPC_FAIL);
f4ada568
GL
670 break;
671 }
672}
673
cdc59bb6 674void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event)
f4ada568 675{
cdc59bb6 676 wxSocketServer *server = (wxSocketServer *) event.GetSocket();
f6bcfd97 677 wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData();
f4ada568 678
e5b502f3
GRG
679 // This socket is being deleted; skip this event
680 if (!ipcserv)
681 return;
682
cdc59bb6 683 if (event.GetSocketEvent() != wxSOCKET_CONNECTION)
f4ada568
GL
684 return;
685
3adb47a9 686 // Accept the connection, getting a new socket
cdc59bb6 687 wxSocketBase *sock = server->Accept();
0834112f 688 if (!sock->Ok())
3adb47a9
GRG
689 {
690 sock->Destroy();
0834112f 691 return;
3adb47a9 692 }
f4ada568 693
cdc59bb6
GRG
694 wxSocketStream *stream = new wxSocketStream(*sock);
695 wxDataInputStream *codeci = new wxDataInputStream(*stream);
696 wxDataOutputStream *codeco = new wxDataOutputStream(*stream);
f4ada568 697
f4ada568 698 int msg;
75ed1d15 699 msg = codeci->Read8();
f4ada568 700
0834112f
GRG
701 if (msg == IPC_CONNECT)
702 {
f4ada568 703 wxString topic_name;
75ed1d15 704 topic_name = codeci->ReadString();
f4ada568 705
f4ada568
GL
706 wxTCPConnection *new_connection =
707 (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name);
3adb47a9 708
0834112f
GRG
709 if (new_connection)
710 {
3adb47a9 711 if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection)))
0834112f 712 {
3adb47a9
GRG
713 // Acknowledge success
714 codeco->Write8(IPC_CONNECT);
715 new_connection->m_topic = topic_name;
26a25f95 716 new_connection->m_sock = sock;
3adb47a9
GRG
717 new_connection->m_sockstrm = stream;
718 new_connection->m_codeci = codeci;
719 new_connection->m_codeco = codeco;
cdc59bb6
GRG
720 sock->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID);
721 sock->SetClientData(new_connection);
3adb47a9
GRG
722 sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
723 sock->Notify(TRUE);
f4ada568
GL
724 return;
725 }
3adb47a9
GRG
726 else
727 {
728 delete new_connection;
729 // and fall through to delete everything else
730 }
f4ada568
GL
731 }
732 }
3adb47a9
GRG
733
734 // Something went wrong, send failure message and delete everything
735 codeco->Write8(IPC_FAIL);
736
737 delete codeco;
738 delete codeci;
739 delete stream;
740 sock->Destroy();
f4ada568 741}
35a4dab7 742
cdc59bb6
GRG
743// --------------------------------------------------------------------------
744// wxTCPEventHandlerModule (private class)
745// --------------------------------------------------------------------------
746
ed4c6c69 747class wxTCPEventHandlerModule: public wxModule
cdc59bb6
GRG
748{
749 DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule)
750
751public:
752 bool OnInit() { gs_handler = new wxTCPEventHandler(); return TRUE; }
753 void OnExit() { wxDELETE(gs_handler); }
754};
755
756IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule, wxModule)
757
758
35a4dab7 759#endif
3adb47a9 760 // wxUSE_SOCKETS && wxUSE_IPC