]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckipc.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Interprocess communication implementation (wxSocket version)
4 // Author: Julian Smart, Guilhem Lavaux
5 // Modified by: Guilhem Lavaux (big rewrite) May 1997, 1998
8 // Copyright: (c) Julian Smart 1993, Guilhem Lavaux 1997, 1998
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "sckipc.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
31 #include "wx/socket.h"
32 #include "wx/sckipc.h"
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
40 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
41 IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection
, wxConnectionBase
)
44 // It seems to be already defined somewhere in the Xt includes.
62 void Server_OnRequest(wxSocketServer
& server
,
65 void Client_OnRequest(wxSocketBase
& sock
,
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 wxTCPClient::wxTCPClient ()
78 wxTCPClient::~wxTCPClient ()
82 bool wxTCPClient::ValidHost(const wxString
& host
)
86 return addr
.Hostname(host
);
89 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
90 const wxString
& server_name
,
91 const wxString
& topic
)
94 wxSocketClient
*client
= new wxSocketClient();
95 wxSocketStream
*stream
= new wxSocketStream(*client
);
96 wxDataInputStream
data_is(*stream
);
97 wxDataOutputStream
data_os(*stream
);
99 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
100 addr
.Service(server_name
);
103 if (!client
->Connect(addr
)) {
107 client
->Notify(FALSE
);
109 // Send topic name, and enquire whether this has succeeded
112 data_os
.Write8(IPC_CONNECT
);
113 data_os
.WriteString(topic
);
115 msg
= data_is
.Read8();
118 if (msg
== IPC_CONNECT
) {
119 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
121 if (!connection
->IsKindOf(CLASSINFO(wxTCPConnection
))) {
125 connection
->m_topic
= topic
;
126 client
->Callback(Client_OnRequest
);
127 client
->CallbackData((char *)connection
);
128 client
->Notify(TRUE
);
141 wxConnectionBase
*wxTCPClient::OnMakeConnection()
143 return new wxTCPConnection
;
146 // ---------------------------------------------------------------------------
148 // ---------------------------------------------------------------------------
150 wxTCPServer::wxTCPServer ()
155 bool wxTCPServer::Create(const wxString
& server_name
)
158 wxSocketServer
*server
;
160 addr
.Service(server_name
);
162 // Create a socket listening on specified port
163 server
= new wxSocketServer(addr
);
164 server
->Callback((wxSocketBase::wxSockCbk
)Server_OnRequest
);
165 server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
167 server
->CallbackData((char *)this);
172 wxTCPServer::~wxTCPServer()
176 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
178 return new wxTCPConnection();
181 // ---------------------------------------------------------------------------
183 // ---------------------------------------------------------------------------
185 wxTCPConnection::wxTCPConnection ()
186 : wxConnectionBase(),
187 m_sock(NULL
), m_sockstrm(NULL
), m_codeci(NULL
), m_codeco(NULL
)
191 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
195 wxTCPConnection::~wxTCPConnection ()
200 wxDELETE(m_sockstrm
);
203 void wxTCPConnection::Compress(bool WXUNUSED(on
))
208 // Calls that CLIENT can make.
209 bool wxTCPConnection::Disconnect ()
211 // Send the the disconnect message to the peer.
212 m_codeco
->Write8(IPC_DISCONNECT
);
218 bool wxTCPConnection::Execute (const wxChar
*data
, int size
, wxIPCFormat format
)
220 if (!m_sock
->IsConnected())
223 // Prepare EXECUTE message
224 m_codeco
->Write8(IPC_EXECUTE
);
225 m_codeco
->Write8(format
);
227 m_codeco
->WriteString(data
);
229 m_codeco
->Write32(size
);
230 m_sockstrm
->Write(data
, size
);
236 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
238 if (!m_sock
->IsConnected())
241 m_codeco
->Write8(IPC_REQUEST
);
242 m_codeco
->WriteString(item
);
243 m_codeco
->Write8(format
);
245 // If Unpack doesn't initialize it.
248 ret
= m_codeci
->Read8();
255 s
= m_codeci
->Read32();
257 m_sockstrm
->Read(data
, s
);
265 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
267 if (!m_sock
->IsConnected())
270 m_codeco
->Write8(IPC_POKE
);
271 m_codeco
->WriteString(item
);
272 m_codeco
->Write8(format
);
274 m_codeco
->WriteString(data
);
276 m_codeco
->Write32(size
);
277 m_sockstrm
->Write(data
, size
);
283 bool wxTCPConnection::StartAdvise (const wxString
& item
)
287 if (!m_sock
->IsConnected())
290 m_codeco
->Write8(IPC_ADVISE_START
);
291 m_codeco
->WriteString(item
);
293 ret
= m_codeci
->Read8();
301 bool wxTCPConnection::StopAdvise (const wxString
& item
)
305 if (!m_sock
->IsConnected())
308 m_codeco
->Write8(IPC_ADVISE_STOP
);
309 m_codeco
->WriteString(item
);
311 msg
= m_codeci
->Read8();
319 // Calls that SERVER can make
320 bool wxTCPConnection::Advise (const wxString
& item
,
321 wxChar
*data
, int size
, wxIPCFormat format
)
323 if (!m_sock
->IsConnected())
326 m_codeco
->Write8(IPC_ADVISE
);
327 m_codeco
->WriteString(item
);
328 m_codeco
->Write8(format
);
330 m_codeco
->WriteString(data
);
332 m_codeco
->Write32(size
);
333 m_sockstrm
->Write(data
, size
);
339 void Client_OnRequest(wxSocketBase
& sock
, wxSocketNotify evt
,
343 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
344 wxDataInputStream
*codeci
;
345 wxDataOutputStream
*codeco
;
346 wxSocketStream
*sockstrm
;
347 wxString topic_name
= connection
->m_topic
;
350 // The socket handler signals us that we lost the connection: destroy all.
351 if (evt
== wxSOCKET_LOST
) {
353 connection
->OnDisconnect();
357 // Receive message number.
358 codeci
= connection
->m_codeci
;
359 codeco
= connection
->m_codeco
;
360 sockstrm
= connection
->m_sockstrm
;
361 msg
= codeci
->Read8();
369 format
= (wxIPCFormat
)codeci
->Read8();
370 size
= codeci
->Read32();
371 data
= new char[size
];
372 sockstrm
->Read(data
, size
);
374 connection
->OnExecute (topic_name
, data
, size
, format
);
384 item
= codeci
->ReadString();
385 format
= (wxIPCFormat
)codeci
->Read8();
386 size
= codeci
->Read32();
387 data
= new char[size
];
388 sockstrm
->Read(data
, size
);
390 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
395 case IPC_ADVISE_START
: {
396 item
= codeci
->ReadString();
398 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
400 codeco
->Write8(IPC_ADVISE_START
);
402 codeco
->Write8(IPC_FAIL
);
406 case IPC_ADVISE_STOP
: {
407 item
= codeci
->ReadString();
409 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
411 codeco
->Write8(IPC_ADVISE_STOP
);
413 codeco
->Write8(IPC_FAIL
);
422 item
= codeci
->ReadString();
423 format
= (wxIPCFormat
)codeci
->Read8();
424 size
= codeci
->Read32();
425 data
= new wxChar
[size
];
426 sockstrm
->Read(data
, size
);
428 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
437 item
= codeci
->ReadString();
438 format
= (wxIPCFormat
)codeci
->Read8();
441 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
444 codeco
->Write8(IPC_REQUEST_REPLY
);
445 if (user_size
!= -1) {
446 codeco
->Write32(user_size
);
447 sockstrm
->Write(user_data
, user_size
);
449 codeco
->WriteString(user_data
);
451 codeco
->Write8(IPC_FAIL
);
455 case IPC_DISCONNECT
: {
457 connection
->OnDisconnect();
461 codeco
->Write8(IPC_FAIL
);
466 void Server_OnRequest(wxSocketServer
& server
,
467 wxSocketNotify evt
, char *cdata
)
469 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
470 wxSocketStream
*stream
;
471 wxDataInputStream
*codeci
;
472 wxDataOutputStream
*codeco
;
474 if (evt
!= wxSOCKET_CONNECTION
)
477 /* Accept the connection, getting a new socket */
478 wxSocketBase
*sock
= server
.Accept();
480 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
482 stream
= new wxSocketStream(*sock
);
483 codeci
= new wxDataInputStream(*stream
);
484 codeco
= new wxDataOutputStream(*stream
);
490 msg
= codeci
->Read8();
492 if (msg
== IPC_CONNECT
) {
494 topic_name
= codeci
->ReadString();
496 /* Register new socket with the notifier */
497 wxTCPConnection
*new_connection
=
498 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
499 if (new_connection
) {
500 if (!new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
))) {
501 delete new_connection
;
502 codeco
->Write8(IPC_FAIL
);
505 // Acknowledge success
506 codeco
->Write8(IPC_CONNECT
);
508 new_connection
->m_topic
= topic_name
;
509 new_connection
->m_sockstrm
= stream
;
510 new_connection
->m_codeci
= codeci
;
511 new_connection
->m_codeco
= codeco
;
512 sock
->Callback(Client_OnRequest
);
513 sock
->CallbackData((char *)new_connection
);
516 // Send failure message
517 codeco
->Write8(IPC_FAIL
);