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
10 // Copyright: (c) Julian Smart 1993
11 // (c) Guilhem Lavaux 1997, 1998
12 // (c) 2000 Guillermo Rodriguez <guille@iies.es>
13 // Licence: wxWindows license
14 /////////////////////////////////////////////////////////////////////////////
16 // ==========================================================================
18 // ==========================================================================
20 // --------------------------------------------------------------------------
22 // --------------------------------------------------------------------------
25 #pragma implementation "sckipc.h"
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
39 #if wxUSE_SOCKETS && wxUSE_IPC
44 #include "wx/socket.h"
45 #include "wx/sckipc.h"
46 #include "wx/module.h"
54 // --------------------------------------------------------------------------
55 // macros and constants
56 // --------------------------------------------------------------------------
58 // It seems to be already defined somewhere in the Xt includes.
78 // All sockets will be created with the following flags
79 #define SCKIPC_FLAGS (wxSOCKET_WAITALL)
81 // --------------------------------------------------------------------------
82 // wxTCPEventHandler stuff (private class)
83 // --------------------------------------------------------------------------
85 class wxTCPEventHandler
: public wxEvtHandler
88 wxTCPEventHandler() : wxEvtHandler() {};
90 void Client_OnRequest(wxSocketEvent
& event
);
91 void Server_OnRequest(wxSocketEvent
& event
);
98 _CLIENT_ONREQUEST_ID
= 1000,
102 static wxTCPEventHandler
*gs_handler
= NULL
;
104 // ==========================================================================
106 // ==========================================================================
108 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
109 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
110 IMPLEMENT_CLASS(wxTCPConnection
, wxConnectionBase
)
112 // --------------------------------------------------------------------------
114 // --------------------------------------------------------------------------
116 wxTCPClient::wxTCPClient () : wxClientBase()
120 wxTCPClient::~wxTCPClient ()
124 bool wxTCPClient::ValidHost(const wxString
& host
)
128 return addr
.Hostname(host
);
131 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
132 const wxString
& server_name
,
133 const wxString
& topic
)
135 wxSocketClient
*client
= new wxSocketClient(SCKIPC_FLAGS
);
136 wxSocketStream
*stream
= new wxSocketStream(*client
);
137 wxDataInputStream
*data_is
= new wxDataInputStream(*stream
);
138 wxDataOutputStream
*data_os
= new wxDataOutputStream(*stream
);
141 addr
.Service(server_name
);
144 if (client
->Connect(addr
))
148 // Send topic name, and enquire whether this has succeeded
149 data_os
->Write8(IPC_CONNECT
);
150 data_os
->WriteString(topic
);
152 msg
= data_is
->Read8();
155 if (msg
== IPC_CONNECT
)
157 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
161 if (connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
163 connection
->m_topic
= topic
;
164 connection
->m_sock
= client
;
165 connection
->m_sockstrm
= stream
;
166 connection
->m_codeci
= data_is
;
167 connection
->m_codeco
= data_os
;
168 client
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
169 client
->SetClientData(connection
);
170 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
171 client
->Notify(TRUE
);
177 // and fall through to delete everything else
183 // Something went wrong, delete everything
192 wxConnectionBase
*wxTCPClient::OnMakeConnection()
194 return new wxTCPConnection();
197 // --------------------------------------------------------------------------
199 // --------------------------------------------------------------------------
201 wxTCPServer::wxTCPServer () : wxServerBase()
205 bool wxTCPServer::Create(const wxString
& server_name
)
207 wxSocketServer
*server
;
209 // wxIPV4address defaults to INADDR_ANY:0
211 addr
.Service(server_name
);
213 // Create a socket listening on specified port
214 server
= new wxSocketServer(addr
, SCKIPC_FLAGS
);
215 server
->SetEventHandler(*gs_handler
, _SERVER_ONREQUEST_ID
);
216 server
->SetClientData(this);
217 server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
218 server
->Notify(TRUE
);
223 wxTCPServer::~wxTCPServer()
227 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
229 return new wxTCPConnection();
232 // --------------------------------------------------------------------------
234 // --------------------------------------------------------------------------
236 wxTCPConnection::wxTCPConnection () : wxConnectionBase()
244 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
248 wxTCPConnection::~wxTCPConnection ()
252 wxDELETE(m_sockstrm
);
256 m_sock
->SetClientData(NULL
);
261 void wxTCPConnection::Compress(bool WXUNUSED(on
))
266 // Calls that CLIENT can make.
267 bool wxTCPConnection::Disconnect ()
269 // Send the the disconnect message to the peer.
270 m_codeco
->Write8(IPC_DISCONNECT
);
271 m_sock
->Notify(FALSE
);
277 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
279 if (!m_sock
->IsConnected())
282 // Prepare EXECUTE message
283 m_codeco
->Write8(IPC_EXECUTE
);
284 m_codeco
->Write8(format
);
287 size
= strlen(data
) + 1; // includes final NUL
289 m_codeco
->Write32(size
);
290 m_sockstrm
->Write(data
, size
);
295 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
297 if (!m_sock
->IsConnected())
300 m_codeco
->Write8(IPC_REQUEST
);
301 m_codeco
->WriteString(item
);
302 m_codeco
->Write8(format
);
304 // If Unpack doesn't initialize it.
307 ret
= m_codeci
->Read8();
315 s
= m_codeci
->Read32();
317 m_sockstrm
->Read(data
, s
);
325 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
327 if (!m_sock
->IsConnected())
330 m_codeco
->Write8(IPC_POKE
);
331 m_codeco
->WriteString(item
);
332 m_codeco
->Write8(format
);
335 size
= strlen(data
) + 1; // includes final NUL
337 m_codeco
->Write32(size
);
338 m_sockstrm
->Write(data
, size
);
343 bool wxTCPConnection::StartAdvise (const wxString
& item
)
347 if (!m_sock
->IsConnected())
350 m_codeco
->Write8(IPC_ADVISE_START
);
351 m_codeco
->WriteString(item
);
353 ret
= m_codeci
->Read8();
361 bool wxTCPConnection::StopAdvise (const wxString
& item
)
365 if (!m_sock
->IsConnected())
368 m_codeco
->Write8(IPC_ADVISE_STOP
);
369 m_codeco
->WriteString(item
);
371 msg
= m_codeci
->Read8();
379 // Calls that SERVER can make
380 bool wxTCPConnection::Advise (const wxString
& item
,
381 wxChar
*data
, int size
, wxIPCFormat format
)
383 if (!m_sock
->IsConnected())
386 m_codeco
->Write8(IPC_ADVISE
);
387 m_codeco
->WriteString(item
);
388 m_codeco
->Write8(format
);
391 size
= strlen(data
) + 1; // includes final NUL
393 m_codeco
->Write32(size
);
394 m_sockstrm
->Write(data
, size
);
399 // --------------------------------------------------------------------------
400 // wxTCPEventHandler (private class)
401 // --------------------------------------------------------------------------
403 BEGIN_EVENT_TABLE(wxTCPEventHandler
, wxEvtHandler
)
404 EVT_SOCKET(_CLIENT_ONREQUEST_ID
, wxTCPEventHandler::Client_OnRequest
)
405 EVT_SOCKET(_SERVER_ONREQUEST_ID
, wxTCPEventHandler::Server_OnRequest
)
408 void wxTCPEventHandler::Client_OnRequest(wxSocketEvent
&event
)
410 wxSocketBase
*sock
= event
.GetSocket();
411 wxSocketNotify evt
= event
.GetSocketEvent();
412 wxTCPConnection
*connection
= (wxTCPConnection
*)(sock
->GetClientData());
414 // This socket is being deleted; skip this event
419 wxDataInputStream
*codeci
;
420 wxDataOutputStream
*codeco
;
421 wxSocketStream
*sockstrm
;
422 wxString topic_name
= connection
->m_topic
;
425 // We lost the connection: destroy everything
426 if (evt
== wxSOCKET_LOST
)
430 connection
->OnDisconnect();
434 // Receive message number.
435 codeci
= connection
->m_codeci
;
436 codeco
= connection
->m_codeco
;
437 sockstrm
= connection
->m_sockstrm
;
438 msg
= codeci
->Read8();
448 format
= (wxIPCFormat
)codeci
->Read8();
449 size
= codeci
->Read32();
450 data
= new char[size
];
451 sockstrm
->Read(data
, size
);
453 connection
->OnExecute (topic_name
, data
, size
, format
);
464 item
= codeci
->ReadString();
465 format
= (wxIPCFormat
)codeci
->Read8();
466 size
= codeci
->Read32();
467 data
= new char[size
];
468 sockstrm
->Read(data
, size
);
470 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
475 case IPC_ADVISE_START
:
477 item
= codeci
->ReadString();
479 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
481 codeco
->Write8(IPC_ADVISE_START
);
483 codeco
->Write8(IPC_FAIL
);
487 case IPC_ADVISE_STOP
:
489 item
= codeci
->ReadString();
491 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
493 codeco
->Write8(IPC_ADVISE_STOP
);
495 codeco
->Write8(IPC_FAIL
);
505 item
= codeci
->ReadString();
506 format
= (wxIPCFormat
)codeci
->Read8();
507 size
= codeci
->Read32();
508 data
= new wxChar
[size
];
509 sockstrm
->Read(data
, size
);
511 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
521 item
= codeci
->ReadString();
522 format
= (wxIPCFormat
)codeci
->Read8();
525 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
529 codeco
->Write8(IPC_REQUEST_REPLY
);
532 user_size
= strlen(user_data
) + 1; // includes final NUL
534 codeco
->Write32(user_size
);
535 sockstrm
->Write(user_data
, user_size
);
538 codeco
->Write8(IPC_FAIL
);
546 connection
->OnDisconnect();
550 codeco
->Write8(IPC_FAIL
);
555 void wxTCPEventHandler::Server_OnRequest(wxSocketEvent
&event
)
557 wxSocketServer
*server
= (wxSocketServer
*) event
.GetSocket();
558 wxTCPServer
*ipcserv
= (wxTCPServer
*) event
.GetClientData();
560 // This socket is being deleted; skip this event
564 if (event
.GetSocketEvent() != wxSOCKET_CONNECTION
)
567 // Accept the connection, getting a new socket
568 wxSocketBase
*sock
= server
->Accept();
575 wxSocketStream
*stream
= new wxSocketStream(*sock
);
576 wxDataInputStream
*codeci
= new wxDataInputStream(*stream
);
577 wxDataOutputStream
*codeco
= new wxDataOutputStream(*stream
);
580 msg
= codeci
->Read8();
582 if (msg
== IPC_CONNECT
)
585 topic_name
= codeci
->ReadString();
587 wxTCPConnection
*new_connection
=
588 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
592 if (new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
594 // Acknowledge success
595 codeco
->Write8(IPC_CONNECT
);
596 new_connection
->m_topic
= topic_name
;
597 new_connection
->m_sock
= sock
;
598 new_connection
->m_sockstrm
= stream
;
599 new_connection
->m_codeci
= codeci
;
600 new_connection
->m_codeco
= codeco
;
601 sock
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
602 sock
->SetClientData(new_connection
);
603 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
609 delete new_connection
;
610 // and fall through to delete everything else
615 // Something went wrong, send failure message and delete everything
616 codeco
->Write8(IPC_FAIL
);
624 // --------------------------------------------------------------------------
625 // wxTCPEventHandlerModule (private class)
626 // --------------------------------------------------------------------------
628 class WXDLLEXPORT wxTCPEventHandlerModule
: public wxModule
630 DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule
)
633 bool OnInit() { gs_handler
= new wxTCPEventHandler(); return TRUE
; }
634 void OnExit() { wxDELETE(gs_handler
); }
637 IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule
, wxModule
)
641 // wxUSE_SOCKETS && wxUSE_IPC