1 /////////////////////////////////////////////////////////////////////////////
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
11 // Copyright: (c) Julian Smart 1993
12 // (c) Guilhem Lavaux 1997, 1998
13 // (c) 2000 Guillermo Rodriguez <guille@iies.es>
14 // Licence: wxWindows license
15 /////////////////////////////////////////////////////////////////////////////
17 // ==========================================================================
19 // ==========================================================================
21 // --------------------------------------------------------------------------
23 // --------------------------------------------------------------------------
26 #pragma implementation "sckipc.h"
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
40 #if wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS
46 #include "wx/socket.h"
47 #include "wx/sckipc.h"
48 #include "wx/module.h"
51 // --------------------------------------------------------------------------
52 // macros and constants
53 // --------------------------------------------------------------------------
55 // It seems to be already defined somewhere in the Xt includes.
74 // All sockets will be created with the following flags
75 #define SCKIPC_FLAGS (wxSOCKET_WAITALL)
77 // headers needed for umask()
79 #include <sys/types.h>
81 #endif // __UNIX_LIKE__
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 // get the address object for the given server name, the caller must delete it
88 static wxSockAddress
*
89 GetAddressFromName(const wxString
& serverName
, const wxString
& host
= _T(""))
91 // we always use INET sockets under non-Unix systems
92 #if defined(__UNIX__) && !defined(__WXMAC__)
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
)
97 wxUNIXaddress
*addr
= new wxUNIXaddress
;
98 addr
->Filename(serverName
);
104 wxIPV4address
*addr
= new wxIPV4address
;
105 addr
->Service(serverName
);
108 addr
->Hostname(host
);
115 // --------------------------------------------------------------------------
116 // wxTCPEventHandler stuff (private class)
117 // --------------------------------------------------------------------------
119 class wxTCPEventHandler
: public wxEvtHandler
122 wxTCPEventHandler() : wxEvtHandler() {};
124 void Client_OnRequest(wxSocketEvent
& event
);
125 void Server_OnRequest(wxSocketEvent
& event
);
127 DECLARE_EVENT_TABLE()
132 _CLIENT_ONREQUEST_ID
= 1000,
136 static wxTCPEventHandler
*gs_handler
= NULL
;
138 // ==========================================================================
140 // ==========================================================================
142 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
143 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
144 IMPLEMENT_CLASS(wxTCPConnection
, wxConnectionBase
)
146 // --------------------------------------------------------------------------
148 // --------------------------------------------------------------------------
150 wxTCPClient::wxTCPClient () : wxClientBase()
154 wxTCPClient::~wxTCPClient ()
158 bool wxTCPClient::ValidHost(const wxString
& host
)
162 return addr
.Hostname(host
);
165 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
166 const wxString
& serverName
,
167 const wxString
& topic
)
169 wxSocketClient
*client
= new wxSocketClient(SCKIPC_FLAGS
);
170 wxSocketStream
*stream
= new wxSocketStream(*client
);
171 wxDataInputStream
*data_is
= new wxDataInputStream(*stream
);
172 wxDataOutputStream
*data_os
= new wxDataOutputStream(*stream
);
174 wxSockAddress
*addr
= GetAddressFromName(serverName
, host
);
178 bool ok
= client
->Connect(*addr
);
185 // Send topic name, and enquire whether this has succeeded
186 data_os
->Write8(IPC_CONNECT
);
187 data_os
->WriteString(topic
);
189 msg
= data_is
->Read8();
192 if (msg
== IPC_CONNECT
)
194 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
198 if (connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
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
;
205 client
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
206 client
->SetClientData(connection
);
207 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
208 client
->Notify(TRUE
);
214 // and fall through to delete everything else
220 // Something went wrong, delete everything
229 wxConnectionBase
*wxTCPClient::OnMakeConnection()
231 return new wxTCPConnection();
234 // --------------------------------------------------------------------------
236 // --------------------------------------------------------------------------
238 wxTCPServer::wxTCPServer () : wxServerBase()
243 bool wxTCPServer::Create(const wxString
& serverName
)
245 // Destroy previous server, if any
248 m_server
->SetClientData(NULL
);
253 wxSockAddress
*addr
= GetAddressFromName(serverName
);
259 if ( addr
->Type() == wxSockAddress::UNIX
)
261 // ensure that the file doesn't exist as otherwise calling socket() would
263 int rc
= remove(serverName
.fn_str());
264 if ( rc
< 0 && errno
!= ENOENT
)
271 // also set the umask to prevent the others from reading our file
272 umaskOld
= umask(077);
276 // unused anyhow but shut down the compiler warnings
279 #endif // __UNIX_LIKE__
281 // Create a socket listening on the specified port
282 m_server
= new wxSocketServer(*addr
, SCKIPC_FLAGS
);
285 if ( addr
->Type() == wxSockAddress::UNIX
)
290 // save the file name to remove it later
291 m_filename
= serverName
;
293 #endif // __UNIX_LIKE__
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
);
313 wxTCPServer::~wxTCPServer()
317 m_server
->SetClientData(NULL
);
322 if ( !m_filename
.empty() )
324 if ( remove(m_filename
.fn_str()) != 0 )
326 wxLogDebug(_T("Stale AF_UNIX file '%s' left."), m_filename
.c_str());
329 #endif // __UNIX_LIKE__
332 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
334 return new wxTCPConnection();
337 // --------------------------------------------------------------------------
339 // --------------------------------------------------------------------------
341 wxTCPConnection::wxTCPConnection () : wxConnectionBase()
349 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
353 wxTCPConnection::~wxTCPConnection ()
357 wxDELETE(m_sockstrm
);
361 m_sock
->SetClientData(NULL
);
366 void wxTCPConnection::Compress(bool WXUNUSED(on
))
371 // Calls that CLIENT can make.
372 bool wxTCPConnection::Disconnect ()
374 // Send the the disconnect message to the peer.
375 m_codeco
->Write8(IPC_DISCONNECT
);
376 m_sock
->Notify(FALSE
);
382 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
384 if (!m_sock
->IsConnected())
387 // Prepare EXECUTE message
388 m_codeco
->Write8(IPC_EXECUTE
);
389 m_codeco
->Write8(format
);
392 size
= wxStrlen(data
) + 1; // includes final NUL
394 m_codeco
->Write32(size
);
395 m_sockstrm
->Write(data
, size
);
400 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
402 if (!m_sock
->IsConnected())
405 m_codeco
->Write8(IPC_REQUEST
);
406 m_codeco
->WriteString(item
);
407 m_codeco
->Write8(format
);
409 // If Unpack doesn't initialize it.
412 ret
= m_codeci
->Read8();
420 s
= m_codeci
->Read32();
422 m_sockstrm
->Read(data
, s
);
430 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
432 if (!m_sock
->IsConnected())
435 m_codeco
->Write8(IPC_POKE
);
436 m_codeco
->WriteString(item
);
437 m_codeco
->Write8(format
);
440 size
= wxStrlen(data
) + 1; // includes final NUL
442 m_codeco
->Write32(size
);
443 m_sockstrm
->Write(data
, size
);
448 bool wxTCPConnection::StartAdvise (const wxString
& item
)
452 if (!m_sock
->IsConnected())
455 m_codeco
->Write8(IPC_ADVISE_START
);
456 m_codeco
->WriteString(item
);
458 ret
= m_codeci
->Read8();
466 bool wxTCPConnection::StopAdvise (const wxString
& item
)
470 if (!m_sock
->IsConnected())
473 m_codeco
->Write8(IPC_ADVISE_STOP
);
474 m_codeco
->WriteString(item
);
476 msg
= m_codeci
->Read8();
484 // Calls that SERVER can make
485 bool wxTCPConnection::Advise (const wxString
& item
,
486 wxChar
*data
, int size
, wxIPCFormat format
)
488 if (!m_sock
->IsConnected())
491 m_codeco
->Write8(IPC_ADVISE
);
492 m_codeco
->WriteString(item
);
493 m_codeco
->Write8(format
);
496 size
= wxStrlen(data
) + 1; // includes final NUL
498 m_codeco
->Write32(size
);
499 m_sockstrm
->Write(data
, size
);
504 // --------------------------------------------------------------------------
505 // wxTCPEventHandler (private class)
506 // --------------------------------------------------------------------------
508 BEGIN_EVENT_TABLE(wxTCPEventHandler
, wxEvtHandler
)
509 EVT_SOCKET(_CLIENT_ONREQUEST_ID
, wxTCPEventHandler::Client_OnRequest
)
510 EVT_SOCKET(_SERVER_ONREQUEST_ID
, wxTCPEventHandler::Server_OnRequest
)
513 void wxTCPEventHandler::Client_OnRequest(wxSocketEvent
&event
)
515 wxSocketBase
*sock
= event
.GetSocket();
516 wxSocketNotify evt
= event
.GetSocketEvent();
517 wxTCPConnection
*connection
= (wxTCPConnection
*)(sock
->GetClientData());
519 // This socket is being deleted; skip this event
524 wxDataInputStream
*codeci
;
525 wxDataOutputStream
*codeco
;
526 wxSocketStream
*sockstrm
;
527 wxString topic_name
= connection
->m_topic
;
530 // We lost the connection: destroy everything
531 if (evt
== wxSOCKET_LOST
)
535 connection
->OnDisconnect();
539 // Receive message number.
540 codeci
= connection
->m_codeci
;
541 codeco
= connection
->m_codeco
;
542 sockstrm
= connection
->m_sockstrm
;
543 msg
= codeci
->Read8();
553 format
= (wxIPCFormat
)codeci
->Read8();
554 size
= codeci
->Read32();
555 data
= new char[size
];
556 sockstrm
->Read(data
, size
);
558 connection
->OnExecute (topic_name
, data
, size
, format
);
569 item
= codeci
->ReadString();
570 format
= (wxIPCFormat
)codeci
->Read8();
571 size
= codeci
->Read32();
572 data
= new char[size
];
573 sockstrm
->Read(data
, size
);
575 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
580 case IPC_ADVISE_START
:
582 item
= codeci
->ReadString();
584 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
586 codeco
->Write8(IPC_ADVISE_START
);
588 codeco
->Write8(IPC_FAIL
);
592 case IPC_ADVISE_STOP
:
594 item
= codeci
->ReadString();
596 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
598 codeco
->Write8(IPC_ADVISE_STOP
);
600 codeco
->Write8(IPC_FAIL
);
610 item
= codeci
->ReadString();
611 format
= (wxIPCFormat
)codeci
->Read8();
612 size
= codeci
->Read32();
613 data
= new wxChar
[size
];
614 sockstrm
->Read(data
, size
);
616 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
626 item
= codeci
->ReadString();
627 format
= (wxIPCFormat
)codeci
->Read8();
630 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
634 codeco
->Write8(IPC_REQUEST_REPLY
);
637 user_size
= strlen(user_data
) + 1; // includes final NUL
639 codeco
->Write32(user_size
);
640 sockstrm
->Write(user_data
, user_size
);
643 codeco
->Write8(IPC_FAIL
);
651 connection
->OnDisconnect();
655 codeco
->Write8(IPC_FAIL
);
660 void wxTCPEventHandler::Server_OnRequest(wxSocketEvent
&event
)
662 wxSocketServer
*server
= (wxSocketServer
*) event
.GetSocket();
663 wxTCPServer
*ipcserv
= (wxTCPServer
*) server
->GetClientData();
665 // This socket is being deleted; skip this event
669 if (event
.GetSocketEvent() != wxSOCKET_CONNECTION
)
672 // Accept the connection, getting a new socket
673 wxSocketBase
*sock
= server
->Accept();
680 wxSocketStream
*stream
= new wxSocketStream(*sock
);
681 wxDataInputStream
*codeci
= new wxDataInputStream(*stream
);
682 wxDataOutputStream
*codeco
= new wxDataOutputStream(*stream
);
685 msg
= codeci
->Read8();
687 if (msg
== IPC_CONNECT
)
690 topic_name
= codeci
->ReadString();
692 wxTCPConnection
*new_connection
=
693 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
697 if (new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
699 // Acknowledge success
700 codeco
->Write8(IPC_CONNECT
);
701 new_connection
->m_topic
= topic_name
;
702 new_connection
->m_sock
= sock
;
703 new_connection
->m_sockstrm
= stream
;
704 new_connection
->m_codeci
= codeci
;
705 new_connection
->m_codeco
= codeco
;
706 sock
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
707 sock
->SetClientData(new_connection
);
708 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
714 delete new_connection
;
715 // and fall through to delete everything else
720 // Something went wrong, send failure message and delete everything
721 codeco
->Write8(IPC_FAIL
);
729 // --------------------------------------------------------------------------
730 // wxTCPEventHandlerModule (private class)
731 // --------------------------------------------------------------------------
733 class WXDLLEXPORT wxTCPEventHandlerModule
: public wxModule
735 DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule
)
738 bool OnInit() { gs_handler
= new wxTCPEventHandler(); return TRUE
; }
739 void OnExit() { wxDELETE(gs_handler
); }
742 IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule
, wxModule
)
746 // wxUSE_SOCKETS && wxUSE_IPC