]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckipc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/sckipc.cpp
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 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #if wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS
34 #include "wx/sckipc.h"
39 #include "wx/module.h"
46 #include "wx/socket.h"
48 // --------------------------------------------------------------------------
49 // macros and constants
50 // --------------------------------------------------------------------------
52 // It seems to be already defined somewhere in the Xt includes.
71 // All sockets will be created with the following flags
72 #define SCKIPC_FLAGS (wxSOCKET_WAITALL)
74 // headers needed for umask()
76 #include <sys/types.h>
78 #endif // __UNIX_LIKE__
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // get the address object for the given server name, the caller must delete it
85 static wxSockAddress
*
86 GetAddressFromName(const wxString
& serverName
, const wxString
& host
= wxEmptyString
)
88 // we always use INET sockets under non-Unix systems
89 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
90 // under Unix, if the server name looks like a path, create a AF_UNIX
91 // socket instead of AF_INET one
92 if ( serverName
.Find(_T('/')) != wxNOT_FOUND
)
94 wxUNIXaddress
*addr
= new wxUNIXaddress
;
95 addr
->Filename(serverName
);
101 wxIPV4address
*addr
= new wxIPV4address
;
102 addr
->Service(serverName
);
105 addr
->Hostname(host
);
112 // --------------------------------------------------------------------------
113 // wxTCPEventHandler stuff (private class)
114 // --------------------------------------------------------------------------
116 class wxTCPEventHandler
: public wxEvtHandler
119 wxTCPEventHandler() : wxEvtHandler() {}
121 void Client_OnRequest(wxSocketEvent
& event
);
122 void Server_OnRequest(wxSocketEvent
& event
);
124 DECLARE_EVENT_TABLE()
125 DECLARE_NO_COPY_CLASS(wxTCPEventHandler
)
130 _CLIENT_ONREQUEST_ID
= 1000,
134 static wxTCPEventHandler
*gs_handler
= NULL
;
136 // ==========================================================================
138 // ==========================================================================
140 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
141 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
142 IMPLEMENT_CLASS(wxTCPConnection
, wxConnectionBase
)
144 // --------------------------------------------------------------------------
146 // --------------------------------------------------------------------------
148 wxTCPClient::wxTCPClient () : wxClientBase()
152 wxTCPClient::~wxTCPClient ()
156 bool wxTCPClient::ValidHost(const wxString
& host
)
160 return addr
.Hostname(host
);
163 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
164 const wxString
& serverName
,
165 const wxString
& topic
)
167 wxSockAddress
*addr
= GetAddressFromName(serverName
, host
);
171 wxSocketClient
*client
= new wxSocketClient(SCKIPC_FLAGS
);
172 wxSocketStream
*stream
= new wxSocketStream(*client
);
173 wxDataInputStream
*data_is
= new wxDataInputStream(*stream
);
174 wxDataOutputStream
*data_os
= new wxDataOutputStream(*stream
);
176 bool ok
= client
->Connect(*addr
);
183 // Send topic name, and enquire whether this has succeeded
184 data_os
->Write8(IPC_CONNECT
);
185 data_os
->WriteString(topic
);
187 msg
= data_is
->Read8();
190 if (msg
== IPC_CONNECT
)
192 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
196 if (connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
198 connection
->m_topic
= topic
;
199 connection
->m_sock
= client
;
200 connection
->m_sockstrm
= stream
;
201 connection
->m_codeci
= data_is
;
202 connection
->m_codeco
= data_os
;
203 client
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
204 client
->SetClientData(connection
);
205 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
206 client
->Notify(true);
212 // and fall through to delete everything else
218 // Something went wrong, delete everything
227 wxConnectionBase
*wxTCPClient::OnMakeConnection()
229 return new wxTCPConnection();
232 // --------------------------------------------------------------------------
234 // --------------------------------------------------------------------------
236 wxTCPServer::wxTCPServer () : wxServerBase()
241 bool wxTCPServer::Create(const wxString
& serverName
)
243 // Destroy previous server, if any
246 m_server
->SetClientData(NULL
);
251 wxSockAddress
*addr
= GetAddressFromName(serverName
);
257 if ( addr
->Type() == wxSockAddress::UNIX
)
259 // ensure that the file doesn't exist as otherwise calling socket() would
261 int rc
= remove(serverName
.fn_str());
262 if ( rc
< 0 && errno
!= ENOENT
)
269 // also set the umask to prevent the others from reading our file
270 umaskOld
= umask(077);
274 // unused anyhow but shut down the compiler warnings
277 #endif // __UNIX_LIKE__
279 // Create a socket listening on the specified port
280 m_server
= new wxSocketServer(*addr
, SCKIPC_FLAGS
);
283 if ( addr
->Type() == wxSockAddress::UNIX
)
288 // save the file name to remove it later
289 m_filename
= serverName
;
291 #endif // __UNIX_LIKE__
303 m_server
->SetEventHandler(*gs_handler
, _SERVER_ONREQUEST_ID
);
304 m_server
->SetClientData(this);
305 m_server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
306 m_server
->Notify(true);
311 wxTCPServer::~wxTCPServer()
315 m_server
->SetClientData(NULL
);
320 if ( !m_filename
.empty() )
322 if ( remove(m_filename
.fn_str()) != 0 )
324 wxLogDebug(_T("Stale AF_UNIX file '%s' left."), m_filename
.c_str());
327 #endif // __UNIX_LIKE__
330 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
332 return new wxTCPConnection();
335 // --------------------------------------------------------------------------
337 // --------------------------------------------------------------------------
339 wxTCPConnection::wxTCPConnection () : wxConnectionBase()
347 wxTCPConnection::wxTCPConnection(wxChar
*buffer
, int size
)
348 : wxConnectionBase(buffer
, size
)
356 wxTCPConnection::~wxTCPConnection ()
362 m_sock
->SetClientData(NULL
);
366 /* Delete after destroy */
369 wxDELETE(m_sockstrm
);
372 void wxTCPConnection::Compress(bool WXUNUSED(on
))
377 // Calls that CLIENT can make.
378 bool wxTCPConnection::Disconnect ()
380 if ( !GetConnected() )
382 // Send the the disconnect message to the peer.
383 m_codeco
->Write8(IPC_DISCONNECT
);
384 m_sock
->Notify(false);
391 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
393 if (!m_sock
->IsConnected())
396 // Prepare EXECUTE message
397 m_codeco
->Write8(IPC_EXECUTE
);
398 m_codeco
->Write8(format
);
401 size
= (wxStrlen(data
) + 1) * sizeof(wxChar
); // includes final NUL
403 m_codeco
->Write32(size
);
404 m_sockstrm
->Write(data
, size
);
409 wxChar
*wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
411 if (!m_sock
->IsConnected())
414 m_codeco
->Write8(IPC_REQUEST
);
415 m_codeco
->WriteString(item
);
416 m_codeco
->Write8(format
);
418 // If Unpack doesn't initialize it.
421 ret
= m_codeci
->Read8();
428 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) * sizeof(wxChar
); // 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) * sizeof(wxChar
); // 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 if (!sock
) { /* No socket, no glory */
530 wxSocketNotify evt
= event
.GetSocketEvent();
531 wxTCPConnection
*connection
= (wxTCPConnection
*)(sock
->GetClientData());
533 // This socket is being deleted; skip this event
537 wxDataInputStream
*codeci
;
538 wxDataOutputStream
*codeco
;
539 wxSocketStream
*sockstrm
;
540 wxString topic_name
= connection
->m_topic
;
543 // We lost the connection: destroy everything
544 if (evt
== wxSOCKET_LOST
)
548 connection
->OnDisconnect();
552 // Receive message number.
553 codeci
= connection
->m_codeci
;
554 codeco
= connection
->m_codeco
;
555 sockstrm
= connection
->m_sockstrm
;
556 int msg
= codeci
->Read8();
566 format
= (wxIPCFormat
)codeci
->Read8();
567 size
= codeci
->Read32();
569 data
= connection
->GetBufferAtLeast( size
);
570 wxASSERT_MSG(data
!= NULL
,
571 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
572 sockstrm
->Read(data
, size
);
574 connection
->OnExecute (topic_name
, data
, size
, format
);
584 item
= codeci
->ReadString();
585 format
= (wxIPCFormat
)codeci
->Read8();
586 size
= codeci
->Read32();
587 data
= connection
->GetBufferAtLeast( size
);
588 wxASSERT_MSG(data
!= NULL
,
589 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
590 sockstrm
->Read(data
, size
);
592 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
596 case IPC_ADVISE_START
:
598 item
= codeci
->ReadString();
600 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
602 codeco
->Write8(IPC_ADVISE_START
);
604 codeco
->Write8(IPC_FAIL
);
608 case IPC_ADVISE_STOP
:
610 item
= codeci
->ReadString();
612 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
614 codeco
->Write8(IPC_ADVISE_STOP
);
616 codeco
->Write8(IPC_FAIL
);
626 item
= codeci
->ReadString();
627 format
= (wxIPCFormat
)codeci
->Read8();
628 size
= codeci
->Read32();
629 data
= connection
->GetBufferAtLeast( size
);
630 wxASSERT_MSG(data
!= NULL
,
631 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
632 sockstrm
->Read(data
, size
);
634 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
642 item
= codeci
->ReadString();
643 format
= (wxIPCFormat
)codeci
->Read8();
646 wxChar
*user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
650 codeco
->Write8(IPC_REQUEST_REPLY
);
653 user_size
= (wxStrlen(user_data
) + 1) * sizeof(wxChar
); // includes final NUL
655 codeco
->Write32(user_size
);
656 sockstrm
->Write(user_data
, user_size
);
659 codeco
->Write8(IPC_FAIL
);
667 connection
->SetConnected(false);
668 connection
->OnDisconnect();
672 codeco
->Write8(IPC_FAIL
);
677 void wxTCPEventHandler::Server_OnRequest(wxSocketEvent
&event
)
679 wxSocketServer
*server
= (wxSocketServer
*) event
.GetSocket();
680 if (!server
) { /* No server, Then exit */
683 wxTCPServer
*ipcserv
= (wxTCPServer
*) server
->GetClientData();
685 // This socket is being deleted; skip this event
689 if (event
.GetSocketEvent() != wxSOCKET_CONNECTION
)
692 // Accept the connection, getting a new socket
693 wxSocketBase
*sock
= server
->Accept();
694 if (!sock
) { /* No socket, no glory */
703 wxSocketStream
*stream
= new wxSocketStream(*sock
);
704 wxDataInputStream
*codeci
= new wxDataInputStream(*stream
);
705 wxDataOutputStream
*codeco
= new wxDataOutputStream(*stream
);
708 msg
= codeci
->Read8();
710 if (msg
== IPC_CONNECT
)
713 topic_name
= codeci
->ReadString();
715 wxTCPConnection
*new_connection
=
716 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
720 if (new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
722 // Acknowledge success
723 codeco
->Write8(IPC_CONNECT
);
724 new_connection
->m_topic
= topic_name
;
725 new_connection
->m_sock
= sock
;
726 new_connection
->m_sockstrm
= stream
;
727 new_connection
->m_codeci
= codeci
;
728 new_connection
->m_codeco
= codeco
;
729 sock
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
730 sock
->SetClientData(new_connection
);
731 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
737 delete new_connection
;
738 // and fall through to delete everything else
743 // Something went wrong, send failure message and delete everything
744 codeco
->Write8(IPC_FAIL
);
752 // --------------------------------------------------------------------------
753 // wxTCPEventHandlerModule (private class)
754 // --------------------------------------------------------------------------
756 class wxTCPEventHandlerModule
: public wxModule
758 DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule
)
761 bool OnInit() { gs_handler
= new wxTCPEventHandler(); return true; }
762 void OnExit() { wxDELETE(gs_handler
); }
765 IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule
, wxModule
)
769 // wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS