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 && wxUSE_STREAMS
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()
206 bool wxTCPServer::Create(const wxString
& serverName
)
208 // Destroy previous server, if any
211 m_server
->SetClientData(NULL
);
216 // wxIPV4address defaults to INADDR_ANY:0
218 addr
.Service(serverName
);
220 // Create a socket listening on the specified port
221 m_server
= new wxSocketServer(addr
, SCKIPC_FLAGS
);
231 m_server
->SetEventHandler(*gs_handler
, _SERVER_ONREQUEST_ID
);
232 m_server
->SetClientData(this);
233 m_server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
234 m_server
->Notify(TRUE
);
239 wxTCPServer::~wxTCPServer()
243 m_server
->SetClientData(NULL
);
248 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
250 return new wxTCPConnection();
253 // --------------------------------------------------------------------------
255 // --------------------------------------------------------------------------
257 wxTCPConnection::wxTCPConnection () : wxConnectionBase()
265 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
269 wxTCPConnection::~wxTCPConnection ()
273 wxDELETE(m_sockstrm
);
277 m_sock
->SetClientData(NULL
);
282 void wxTCPConnection::Compress(bool WXUNUSED(on
))
287 // Calls that CLIENT can make.
288 bool wxTCPConnection::Disconnect ()
290 // Send the the disconnect message to the peer.
291 m_codeco
->Write8(IPC_DISCONNECT
);
292 m_sock
->Notify(FALSE
);
298 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
300 if (!m_sock
->IsConnected())
303 // Prepare EXECUTE message
304 m_codeco
->Write8(IPC_EXECUTE
);
305 m_codeco
->Write8(format
);
308 size
= wxStrlen(data
) + 1; // includes final NUL
310 m_codeco
->Write32(size
);
311 m_sockstrm
->Write(data
, size
);
316 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
318 if (!m_sock
->IsConnected())
321 m_codeco
->Write8(IPC_REQUEST
);
322 m_codeco
->WriteString(item
);
323 m_codeco
->Write8(format
);
325 // If Unpack doesn't initialize it.
328 ret
= m_codeci
->Read8();
336 s
= m_codeci
->Read32();
338 m_sockstrm
->Read(data
, s
);
346 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
348 if (!m_sock
->IsConnected())
351 m_codeco
->Write8(IPC_POKE
);
352 m_codeco
->WriteString(item
);
353 m_codeco
->Write8(format
);
356 size
= wxStrlen(data
) + 1; // includes final NUL
358 m_codeco
->Write32(size
);
359 m_sockstrm
->Write(data
, size
);
364 bool wxTCPConnection::StartAdvise (const wxString
& item
)
368 if (!m_sock
->IsConnected())
371 m_codeco
->Write8(IPC_ADVISE_START
);
372 m_codeco
->WriteString(item
);
374 ret
= m_codeci
->Read8();
382 bool wxTCPConnection::StopAdvise (const wxString
& item
)
386 if (!m_sock
->IsConnected())
389 m_codeco
->Write8(IPC_ADVISE_STOP
);
390 m_codeco
->WriteString(item
);
392 msg
= m_codeci
->Read8();
400 // Calls that SERVER can make
401 bool wxTCPConnection::Advise (const wxString
& item
,
402 wxChar
*data
, int size
, wxIPCFormat format
)
404 if (!m_sock
->IsConnected())
407 m_codeco
->Write8(IPC_ADVISE
);
408 m_codeco
->WriteString(item
);
409 m_codeco
->Write8(format
);
412 size
= wxStrlen(data
) + 1; // includes final NUL
414 m_codeco
->Write32(size
);
415 m_sockstrm
->Write(data
, size
);
420 // --------------------------------------------------------------------------
421 // wxTCPEventHandler (private class)
422 // --------------------------------------------------------------------------
424 BEGIN_EVENT_TABLE(wxTCPEventHandler
, wxEvtHandler
)
425 EVT_SOCKET(_CLIENT_ONREQUEST_ID
, wxTCPEventHandler::Client_OnRequest
)
426 EVT_SOCKET(_SERVER_ONREQUEST_ID
, wxTCPEventHandler::Server_OnRequest
)
429 void wxTCPEventHandler::Client_OnRequest(wxSocketEvent
&event
)
431 wxSocketBase
*sock
= event
.GetSocket();
432 wxSocketNotify evt
= event
.GetSocketEvent();
433 wxTCPConnection
*connection
= (wxTCPConnection
*)(sock
->GetClientData());
435 // This socket is being deleted; skip this event
440 wxDataInputStream
*codeci
;
441 wxDataOutputStream
*codeco
;
442 wxSocketStream
*sockstrm
;
443 wxString topic_name
= connection
->m_topic
;
446 // We lost the connection: destroy everything
447 if (evt
== wxSOCKET_LOST
)
451 connection
->OnDisconnect();
455 // Receive message number.
456 codeci
= connection
->m_codeci
;
457 codeco
= connection
->m_codeco
;
458 sockstrm
= connection
->m_sockstrm
;
459 msg
= codeci
->Read8();
469 format
= (wxIPCFormat
)codeci
->Read8();
470 size
= codeci
->Read32();
471 data
= new char[size
];
472 sockstrm
->Read(data
, size
);
474 connection
->OnExecute (topic_name
, data
, size
, format
);
485 item
= codeci
->ReadString();
486 format
= (wxIPCFormat
)codeci
->Read8();
487 size
= codeci
->Read32();
488 data
= new char[size
];
489 sockstrm
->Read(data
, size
);
491 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
496 case IPC_ADVISE_START
:
498 item
= codeci
->ReadString();
500 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
502 codeco
->Write8(IPC_ADVISE_START
);
504 codeco
->Write8(IPC_FAIL
);
508 case IPC_ADVISE_STOP
:
510 item
= codeci
->ReadString();
512 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
514 codeco
->Write8(IPC_ADVISE_STOP
);
516 codeco
->Write8(IPC_FAIL
);
526 item
= codeci
->ReadString();
527 format
= (wxIPCFormat
)codeci
->Read8();
528 size
= codeci
->Read32();
529 data
= new wxChar
[size
];
530 sockstrm
->Read(data
, size
);
532 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
542 item
= codeci
->ReadString();
543 format
= (wxIPCFormat
)codeci
->Read8();
546 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
550 codeco
->Write8(IPC_REQUEST_REPLY
);
553 user_size
= strlen(user_data
) + 1; // includes final NUL
555 codeco
->Write32(user_size
);
556 sockstrm
->Write(user_data
, user_size
);
559 codeco
->Write8(IPC_FAIL
);
567 connection
->OnDisconnect();
571 codeco
->Write8(IPC_FAIL
);
576 void wxTCPEventHandler::Server_OnRequest(wxSocketEvent
&event
)
578 wxSocketServer
*server
= (wxSocketServer
*) event
.GetSocket();
579 wxTCPServer
*ipcserv
= (wxTCPServer
*) server
->GetClientData();
581 // This socket is being deleted; skip this event
585 if (event
.GetSocketEvent() != wxSOCKET_CONNECTION
)
588 // Accept the connection, getting a new socket
589 wxSocketBase
*sock
= server
->Accept();
596 wxSocketStream
*stream
= new wxSocketStream(*sock
);
597 wxDataInputStream
*codeci
= new wxDataInputStream(*stream
);
598 wxDataOutputStream
*codeco
= new wxDataOutputStream(*stream
);
601 msg
= codeci
->Read8();
603 if (msg
== IPC_CONNECT
)
606 topic_name
= codeci
->ReadString();
608 wxTCPConnection
*new_connection
=
609 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
613 if (new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
615 // Acknowledge success
616 codeco
->Write8(IPC_CONNECT
);
617 new_connection
->m_topic
= topic_name
;
618 new_connection
->m_sock
= sock
;
619 new_connection
->m_sockstrm
= stream
;
620 new_connection
->m_codeci
= codeci
;
621 new_connection
->m_codeco
= codeco
;
622 sock
->SetEventHandler(*gs_handler
, _CLIENT_ONREQUEST_ID
);
623 sock
->SetClientData(new_connection
);
624 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
630 delete new_connection
;
631 // and fall through to delete everything else
636 // Something went wrong, send failure message and delete everything
637 codeco
->Write8(IPC_FAIL
);
645 // --------------------------------------------------------------------------
646 // wxTCPEventHandlerModule (private class)
647 // --------------------------------------------------------------------------
649 class WXDLLEXPORT wxTCPEventHandlerModule
: public wxModule
651 DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule
)
654 bool OnInit() { gs_handler
= new wxTCPEventHandler(); return TRUE
; }
655 void OnExit() { wxDELETE(gs_handler
); }
658 IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule
, wxModule
)
662 // wxUSE_SOCKETS && wxUSE_IPC