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
);
254 if (m_sock
) m_sock
->Destroy();
257 void wxTCPConnection::Compress(bool WXUNUSED(on
))
262 // Calls that CLIENT can make.
263 bool wxTCPConnection::Disconnect ()
265 // Send the the disconnect message to the peer.
266 m_codeco
->Write8(IPC_DISCONNECT
);
267 m_sock
->Notify(FALSE
);
273 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
275 if (!m_sock
->IsConnected())
278 // Prepare EXECUTE message
279 m_codeco
->Write8(IPC_EXECUTE
);
280 m_codeco
->Write8(format
);
283 size
= strlen(data
) + 1; // includes final NUL
285 m_codeco
->Write32(size
);
286 m_sockstrm
->Write(data
, size
);
291 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
293 if (!m_sock
->IsConnected())
296 m_codeco
->Write8(IPC_REQUEST
);
297 m_codeco
->WriteString(item
);
298 m_codeco
->Write8(format
);
300 // If Unpack doesn't initialize it.
303 ret
= m_codeci
->Read8();
311 s
= m_codeci
->Read32();
313 m_sockstrm
->Read(data
, s
);
321 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
323 if (!m_sock
->IsConnected())
326 m_codeco
->Write8(IPC_POKE
);
327 m_codeco
->WriteString(item
);
328 m_codeco
->Write8(format
);
331 size
= strlen(data
) + 1; // includes final NUL
333 m_codeco
->Write32(size
);
334 m_sockstrm
->Write(data
, size
);
339 bool wxTCPConnection::StartAdvise (const wxString
& item
)
343 if (!m_sock
->IsConnected())
346 m_codeco
->Write8(IPC_ADVISE_START
);
347 m_codeco
->WriteString(item
);
349 ret
= m_codeci
->Read8();
357 bool wxTCPConnection::StopAdvise (const wxString
& item
)
361 if (!m_sock
->IsConnected())
364 m_codeco
->Write8(IPC_ADVISE_STOP
);
365 m_codeco
->WriteString(item
);
367 msg
= m_codeci
->Read8();
375 // Calls that SERVER can make
376 bool wxTCPConnection::Advise (const wxString
& item
,
377 wxChar
*data
, int size
, wxIPCFormat format
)
379 if (!m_sock
->IsConnected())
382 m_codeco
->Write8(IPC_ADVISE
);
383 m_codeco
->WriteString(item
);
384 m_codeco
->Write8(format
);
387 size
= strlen(data
) + 1; // includes final NUL
389 m_codeco
->Write32(size
);
390 m_sockstrm
->Write(data
, size
);
395 // --------------------------------------------------------------------------
396 // wxTCPEventHandler (private class)
397 // --------------------------------------------------------------------------
399 BEGIN_EVENT_TABLE(wxTCPEventHandler
, wxEvtHandler
)
400 EVT_SOCKET(_CLIENT_ONREQUEST_ID
, wxTCPEventHandler::Client_OnRequest
)
401 EVT_SOCKET(_SERVER_ONREQUEST_ID
, wxTCPEventHandler::Server_OnRequest
)
404 void wxTCPEventHandler::Client_OnRequest(wxSocketEvent
&event
)
406 wxSocketBase
*sock
= event
.GetSocket();
407 wxSocketNotify evt
= event
.GetSocketEvent();
408 wxTCPConnection
*connection
= (wxTCPConnection
*)(event
.GetClientData());
411 wxDataInputStream
*codeci
;
412 wxDataOutputStream
*codeco
;
413 wxSocketStream
*sockstrm
;
414 wxString topic_name
= connection
->m_topic
;
417 // The socket handler signals us that we lost the connection: destroy all.
418 if (evt
== wxSOCKET_LOST
)
422 connection
->OnDisconnect();
426 // Receive message number.
427 codeci
= connection
->m_codeci
;
428 codeco
= connection
->m_codeco
;
429 sockstrm
= connection
->m_sockstrm
;
430 msg
= codeci
->Read8();
440 format
= (wxIPCFormat
)codeci
->Read8();
441 size
= codeci
->Read32();
442 data
= new char[size
];
443 sockstrm
->Read(data
, size
);
445 connection
->OnExecute (topic_name
, data
, size
, format
);
456 item
= codeci
->ReadString();
457 format
= (wxIPCFormat
)codeci
->Read8();
458 size
= codeci
->Read32();
459 data
= new char[size
];
460 sockstrm
->Read(data
, size
);
462 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
467 case IPC_ADVISE_START
:
469 item
= codeci
->ReadString();
471 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
473 codeco
->Write8(IPC_ADVISE_START
);
475 codeco
->Write8(IPC_FAIL
);
479 case IPC_ADVISE_STOP
:
481 item
= codeci
->ReadString();
483 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
485 codeco
->Write8(IPC_ADVISE_STOP
);
487 codeco
->Write8(IPC_FAIL
);
497 item
= codeci
->ReadString();
498 format
= (wxIPCFormat
)codeci
->Read8();
499 size
= codeci
->Read32();
500 data
= new wxChar
[size
];
501 sockstrm
->Read(data
, size
);
503 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
513 item
= codeci
->ReadString();
514 format
= (wxIPCFormat
)codeci
->Read8();
517 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
521 codeco
->Write8(IPC_REQUEST_REPLY
);
524 user_size
= strlen(user_data
) + 1; // includes final NUL
526 codeco
->Write32(user_size
);
527 sockstrm
->Write(user_data
, user_size
);
530 codeco
->Write8(IPC_FAIL
);
538 connection
->OnDisconnect();
542 codeco
->Write8(IPC_FAIL
);
547 void wxTCPEventHandler::Server_OnRequest(wxSocketEvent
&event
)
549 wxSocketServer
*server
= (wxSocketServer
*) event
.GetSocket();
550 wxTCPServer
*ipcserv
= (wxTCPServer
*) event
.GetClientData();
552 if (event
.GetSocketEvent() != wxSOCKET_CONNECTION
)
555 // Accept the connection, getting a new socket
556 wxSocketBase
*sock
= server
->Accept();
563 wxSocketStream
*stream
= new wxSocketStream(*sock
);
564 wxDataInputStream
*codeci
= new wxDataInputStream(*stream
);
565 wxDataOutputStream
*codeco
= new wxDataOutputStream(*stream
);
568 msg
= codeci
->Read8();
570 if (msg
== IPC_CONNECT
)
573 topic_name
= codeci
->ReadString();
575 wxTCPConnection
*new_connection
=
576 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
580 if (new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
582 // Acknowledge success
583 codeco
->Write8(IPC_CONNECT
);
584 new_connection
->m_topic
= topic_name
;
585 new_connection
->m_sock
= sock
;
586 new_connection
->m_sockstrm
= stream
;
587 new_connection
->m_codeci
= codeci
;
588 new_connection
->m_codeco
= codeco
;
589 sock
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
590 sock
->SetClientData(new_connection
);
591 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
597 delete new_connection
;
598 // and fall through to delete everything else
603 // Something went wrong, send failure message and delete everything
604 codeco
->Write8(IPC_FAIL
);
612 // --------------------------------------------------------------------------
613 // wxTCPEventHandlerModule (private class)
614 // --------------------------------------------------------------------------
616 class WXDLLEXPORT wxTCPEventHandlerModule
: public wxModule
618 DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule
)
621 bool OnInit() { gs_handler
= new wxTCPEventHandler(); return TRUE
; }
622 void OnExit() { wxDELETE(gs_handler
); }
625 IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule
, wxModule
)
629 // wxUSE_SOCKETS && wxUSE_IPC