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