]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckipc.cpp
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 licence
15 /////////////////////////////////////////////////////////////////////////////
17 // ==========================================================================
19 // ==========================================================================
21 // --------------------------------------------------------------------------
23 // --------------------------------------------------------------------------
25 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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__) && !defined(__WINE__)
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()
128 DECLARE_NO_COPY_CLASS(wxTCPEventHandler
)
133 _CLIENT_ONREQUEST_ID
= 1000,
137 static wxTCPEventHandler
*gs_handler
= NULL
;
139 // ==========================================================================
141 // ==========================================================================
143 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
144 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
145 IMPLEMENT_CLASS(wxTCPConnection
, wxConnectionBase
)
147 // --------------------------------------------------------------------------
149 // --------------------------------------------------------------------------
151 wxTCPClient::wxTCPClient () : wxClientBase()
155 wxTCPClient::~wxTCPClient ()
159 bool wxTCPClient::ValidHost(const wxString
& host
)
163 return addr
.Hostname(host
);
166 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
167 const wxString
& serverName
,
168 const wxString
& topic
)
170 wxSocketClient
*client
= new wxSocketClient(SCKIPC_FLAGS
);
171 wxSocketStream
*stream
= new wxSocketStream(*client
);
172 wxDataInputStream
*data_is
= new wxDataInputStream(*stream
);
173 wxDataOutputStream
*data_os
= new wxDataOutputStream(*stream
);
175 wxSockAddress
*addr
= GetAddressFromName(serverName
, host
);
179 bool ok
= client
->Connect(*addr
);
186 // Send topic name, and enquire whether this has succeeded
187 data_os
->Write8(IPC_CONNECT
);
188 data_os
->WriteString(topic
);
190 msg
= data_is
->Read8();
193 if (msg
== IPC_CONNECT
)
195 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
199 if (connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
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
;
206 client
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
207 client
->SetClientData(connection
);
208 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
209 client
->Notify(true);
215 // and fall through to delete everything else
221 // Something went wrong, delete everything
230 wxConnectionBase
*wxTCPClient::OnMakeConnection()
232 return new wxTCPConnection();
235 // --------------------------------------------------------------------------
237 // --------------------------------------------------------------------------
239 wxTCPServer::wxTCPServer () : wxServerBase()
244 bool wxTCPServer::Create(const wxString
& serverName
)
246 // Destroy previous server, if any
249 m_server
->SetClientData(NULL
);
254 wxSockAddress
*addr
= GetAddressFromName(serverName
);
260 if ( addr
->Type() == wxSockAddress::UNIX
)
262 // ensure that the file doesn't exist as otherwise calling socket() would
264 int rc
= remove(serverName
.fn_str());
265 if ( rc
< 0 && errno
!= ENOENT
)
272 // also set the umask to prevent the others from reading our file
273 umaskOld
= umask(077);
277 // unused anyhow but shut down the compiler warnings
280 #endif // __UNIX_LIKE__
282 // Create a socket listening on the specified port
283 m_server
= new wxSocketServer(*addr
, SCKIPC_FLAGS
);
286 if ( addr
->Type() == wxSockAddress::UNIX
)
291 // save the file name to remove it later
292 m_filename
= serverName
;
294 #endif // __UNIX_LIKE__
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);
314 wxTCPServer::~wxTCPServer()
318 m_server
->SetClientData(NULL
);
323 if ( !m_filename
.empty() )
325 if ( remove(m_filename
.fn_str()) != 0 )
327 wxLogDebug(_T("Stale AF_UNIX file '%s' left."), m_filename
.c_str());
330 #endif // __UNIX_LIKE__
333 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
335 return new wxTCPConnection();
338 // --------------------------------------------------------------------------
340 // --------------------------------------------------------------------------
342 wxTCPConnection::wxTCPConnection () : wxConnectionBase()
350 wxTCPConnection::wxTCPConnection(wxChar
*buffer
, int size
)
351 : wxConnectionBase(buffer
, size
)
359 wxTCPConnection::~wxTCPConnection ()
364 wxDELETE(m_sockstrm
);
368 m_sock
->SetClientData(NULL
);
373 void wxTCPConnection::Compress(bool WXUNUSED(on
))
378 // Calls that CLIENT can make.
379 bool wxTCPConnection::Disconnect ()
381 if ( !GetConnected() )
383 // Send the the disconnect message to the peer.
384 m_codeco
->Write8(IPC_DISCONNECT
);
385 m_sock
->Notify(false);
392 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
394 if (!m_sock
->IsConnected())
397 // Prepare EXECUTE message
398 m_codeco
->Write8(IPC_EXECUTE
);
399 m_codeco
->Write8(format
);
402 size
= wxStrlen(data
) + 1; // includes final NUL
404 m_codeco
->Write32(size
);
405 m_sockstrm
->Write(data
, size
);
410 wxChar
*wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
412 if (!m_sock
->IsConnected())
415 m_codeco
->Write8(IPC_REQUEST
);
416 m_codeco
->WriteString(item
);
417 m_codeco
->Write8(format
);
419 // If Unpack doesn't initialize it.
422 ret
= m_codeci
->Read8();
429 s
= m_codeci
->Read32();
430 wxChar
*data
= GetBufferAtLeast( s
);
431 wxASSERT_MSG(data
!= NULL
,
432 _T("Buffer too small in wxTCPConnection::Request") );
433 m_sockstrm
->Read(data
, s
);
441 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
443 if (!m_sock
->IsConnected())
446 m_codeco
->Write8(IPC_POKE
);
447 m_codeco
->WriteString(item
);
448 m_codeco
->Write8(format
);
451 size
= wxStrlen(data
) + 1; // includes final NUL
453 m_codeco
->Write32(size
);
454 m_sockstrm
->Write(data
, size
);
459 bool wxTCPConnection::StartAdvise (const wxString
& item
)
463 if (!m_sock
->IsConnected())
466 m_codeco
->Write8(IPC_ADVISE_START
);
467 m_codeco
->WriteString(item
);
469 ret
= m_codeci
->Read8();
477 bool wxTCPConnection::StopAdvise (const wxString
& item
)
481 if (!m_sock
->IsConnected())
484 m_codeco
->Write8(IPC_ADVISE_STOP
);
485 m_codeco
->WriteString(item
);
487 msg
= m_codeci
->Read8();
495 // Calls that SERVER can make
496 bool wxTCPConnection::Advise (const wxString
& item
,
497 wxChar
*data
, int size
, wxIPCFormat format
)
499 if (!m_sock
->IsConnected())
502 m_codeco
->Write8(IPC_ADVISE
);
503 m_codeco
->WriteString(item
);
504 m_codeco
->Write8(format
);
507 size
= wxStrlen(data
) + 1; // includes final NUL
509 m_codeco
->Write32(size
);
510 m_sockstrm
->Write(data
, size
);
515 // --------------------------------------------------------------------------
516 // wxTCPEventHandler (private class)
517 // --------------------------------------------------------------------------
519 BEGIN_EVENT_TABLE(wxTCPEventHandler
, wxEvtHandler
)
520 EVT_SOCKET(_CLIENT_ONREQUEST_ID
, wxTCPEventHandler::Client_OnRequest
)
521 EVT_SOCKET(_SERVER_ONREQUEST_ID
, wxTCPEventHandler::Server_OnRequest
)
524 void wxTCPEventHandler::Client_OnRequest(wxSocketEvent
&event
)
526 wxSocketBase
*sock
= event
.GetSocket();
527 wxSocketNotify evt
= event
.GetSocketEvent();
528 wxTCPConnection
*connection
= (wxTCPConnection
*)(sock
->GetClientData());
530 // This socket is being deleted; skip this event
534 wxDataInputStream
*codeci
;
535 wxDataOutputStream
*codeco
;
536 wxSocketStream
*sockstrm
;
537 wxString topic_name
= connection
->m_topic
;
540 // We lost the connection: destroy everything
541 if (evt
== wxSOCKET_LOST
)
545 connection
->OnDisconnect();
549 // Receive message number.
550 codeci
= connection
->m_codeci
;
551 codeco
= connection
->m_codeco
;
552 sockstrm
= connection
->m_sockstrm
;
553 int msg
= codeci
->Read8();
563 format
= (wxIPCFormat
)codeci
->Read8();
564 size
= codeci
->Read32();
565 data
= connection
->GetBufferAtLeast( size
);
566 wxASSERT_MSG(data
!= NULL
,
567 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
568 sockstrm
->Read(data
, size
);
570 connection
->OnExecute (topic_name
, data
, size
, format
);
580 item
= codeci
->ReadString();
581 format
= (wxIPCFormat
)codeci
->Read8();
582 size
= codeci
->Read32();
583 data
= connection
->GetBufferAtLeast( size
);
584 wxASSERT_MSG(data
!= NULL
,
585 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
586 sockstrm
->Read(data
, size
);
588 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
592 case IPC_ADVISE_START
:
594 item
= codeci
->ReadString();
596 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
598 codeco
->Write8(IPC_ADVISE_START
);
600 codeco
->Write8(IPC_FAIL
);
604 case IPC_ADVISE_STOP
:
606 item
= codeci
->ReadString();
608 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
610 codeco
->Write8(IPC_ADVISE_STOP
);
612 codeco
->Write8(IPC_FAIL
);
622 item
= codeci
->ReadString();
623 format
= (wxIPCFormat
)codeci
->Read8();
624 size
= codeci
->Read32();
625 data
= connection
->GetBufferAtLeast( size
);
626 wxASSERT_MSG(data
!= NULL
,
627 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
628 sockstrm
->Read(data
, size
);
630 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
638 item
= codeci
->ReadString();
639 format
= (wxIPCFormat
)codeci
->Read8();
642 wxChar
*user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
646 codeco
->Write8(IPC_REQUEST_REPLY
);
649 user_size
= wxStrlen(user_data
) + 1; // includes final NUL
651 codeco
->Write32(user_size
);
652 sockstrm
->Write(user_data
, user_size
);
655 codeco
->Write8(IPC_FAIL
);
663 connection
->SetConnected(false);
664 connection
->OnDisconnect();
668 codeco
->Write8(IPC_FAIL
);
673 void wxTCPEventHandler::Server_OnRequest(wxSocketEvent
&event
)
675 wxSocketServer
*server
= (wxSocketServer
*) event
.GetSocket();
676 wxTCPServer
*ipcserv
= (wxTCPServer
*) server
->GetClientData();
678 // This socket is being deleted; skip this event
682 if (event
.GetSocketEvent() != wxSOCKET_CONNECTION
)
685 // Accept the connection, getting a new socket
686 wxSocketBase
*sock
= server
->Accept();
693 wxSocketStream
*stream
= new wxSocketStream(*sock
);
694 wxDataInputStream
*codeci
= new wxDataInputStream(*stream
);
695 wxDataOutputStream
*codeco
= new wxDataOutputStream(*stream
);
698 msg
= codeci
->Read8();
700 if (msg
== IPC_CONNECT
)
703 topic_name
= codeci
->ReadString();
705 wxTCPConnection
*new_connection
=
706 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
710 if (new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
712 // Acknowledge success
713 codeco
->Write8(IPC_CONNECT
);
714 new_connection
->m_topic
= topic_name
;
715 new_connection
->m_sock
= sock
;
716 new_connection
->m_sockstrm
= stream
;
717 new_connection
->m_codeci
= codeci
;
718 new_connection
->m_codeco
= codeco
;
719 sock
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
720 sock
->SetClientData(new_connection
);
721 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
727 delete new_connection
;
728 // and fall through to delete everything else
733 // Something went wrong, send failure message and delete everything
734 codeco
->Write8(IPC_FAIL
);
742 // --------------------------------------------------------------------------
743 // wxTCPEventHandlerModule (private class)
744 // --------------------------------------------------------------------------
746 class wxTCPEventHandlerModule
: public wxModule
748 DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule
)
751 bool OnInit() { gs_handler
= new wxTCPEventHandler(); return true; }
752 void OnExit() { wxDELETE(gs_handler
); }
755 IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule
, wxModule
)
759 // wxUSE_SOCKETS && wxUSE_IPC