]>
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
,
63 wxSocketBase::wxRequestEvent evt
,
65 void Client_OnRequest(wxSocketBase
& sock
,
66 wxSocketBase::wxRequestEvent evt
,
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 wxTCPClient::wxTCPClient (void)
78 wxTCPClient::~wxTCPClient (void)
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 wxSocketHandler
*hsock
= &wxSocketHandler::Master();
95 wxSocketClient
*client
= hsock
->CreateClient();
96 wxSocketStream
*stream
= new wxSocketStream(*client
);
97 wxDataInputStream
data_is(*stream
);
98 wxDataOutputStream
data_os(*stream
);
100 client
->SetNotify(wxSocketBase::REQ_READ
| wxSocketBase::REQ_LOST
);
101 addr
.Service(server_name
);
104 if (!client
->Connect(addr
)) {
108 client
->Notify(FALSE
);
110 // Send topic name, and enquire whether this has succeeded
113 data_os
.Write8(IPC_CONNECT
);
114 data_os
.WriteString(topic
);
116 msg
= data_is
.Read8();
119 if (msg
== IPC_CONNECT
) {
120 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
122 if (!connection
->IsKindOf(CLASSINFO(wxTCPConnection
))) {
126 connection
->m_topic
= topic
;
127 client
->Callback(Client_OnRequest
);
128 client
->CallbackData((char *)connection
);
129 client
->Notify(TRUE
);
142 wxConnectionBase
*wxTCPClient::OnMakeConnection()
144 return new wxTCPConnection
;
147 // ---------------------------------------------------------------------------
149 // ---------------------------------------------------------------------------
151 wxTCPServer::wxTCPServer (void)
156 bool wxTCPServer::Create(const wxString
& server_name
)
159 wxSocketHandler
*hsock
= &wxSocketHandler::Master();
160 wxSocketServer
*server
;
162 addr
.Service(server_name
);
164 // Create a socket listening on specified port
165 server
= hsock
->CreateServer(addr
);
166 server
->Callback((wxSocketBase::wxSockCbk
)Server_OnRequest
);
167 server
->SetNotify(wxSocketBase::REQ_ACCEPT
);
169 server
->CallbackData((char *)this);
174 wxTCPServer::~wxTCPServer(void)
178 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
180 return new wxTCPConnection();
183 // ---------------------------------------------------------------------------
185 // ---------------------------------------------------------------------------
187 wxTCPConnection::wxTCPConnection (void)
188 : wxConnectionBase(),
189 m_sock(NULL
), m_sockstrm(NULL
), m_codeci(NULL
), m_codeco(NULL
)
193 wxTCPConnection::wxTCPConnection(char *buffer
, int size
)
197 wxTCPConnection::~wxTCPConnection (void)
202 wxDELETE(m_sockstrm
);
205 void wxTCPConnection::Compress(bool WXUNUSED(on
))
210 // Calls that CLIENT can make.
211 bool wxTCPConnection::Disconnect (void)
213 // Send the the disconnect message to the peer.
214 m_codeco
->Write8(IPC_DISCONNECT
);
220 bool wxTCPConnection::Execute (char *data
, int size
, wxIPCFormat format
)
222 if (!m_sock
->IsConnected())
225 // Prepare EXECUTE message
226 m_codeco
->Write8(IPC_EXECUTE
);
227 m_codeco
->Write8(format
);
229 m_codeco
->WriteString(data
);
231 m_codeco
->Write32(size
);
232 m_codeco
->Write(data
, size
);
238 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
240 if (!m_sock
->IsConnected())
243 m_codeco
->Write8(IPC_REQUEST
);
244 m_codeco
->WriteString(item
);
245 m_codeco
->Write8(format
);
247 // If Unpack doesn't initialize it.
250 ret
= m_codeci
->Read8();
257 s
= m_codeci
->Read32();
259 m_codeci
->Read(data
, s
);
267 bool wxTCPConnection::Poke (const wxString
& item
, char *data
, int size
, wxIPCFormat format
)
269 if (!m_sock
->IsConnected())
272 m_codeco
->Write8(IPC_POKE
);
273 m_codeco
->WriteString(item
);
274 m_codeco
->Write8(format
);
276 m_codeco
->WriteString(data
);
278 m_codeco
->Write32(size
);
279 m_codeco
->Write(data
, size
);
285 bool wxTCPConnection::StartAdvise (const wxString
& item
)
289 if (!m_sock
->IsConnected())
292 m_codeco
->Write8(IPC_ADVISE_START
);
293 m_codeco
->WriteString(item
);
295 ret
= m_codeci
->Read8();
303 bool wxTCPConnection::StopAdvise (const wxString
& item
)
307 if (!m_sock
->IsConnected())
310 m_codeco
->Write8(IPC_ADVISE_STOP
);
311 m_codeco
->WriteString(item
);
313 msg
= m_codeci
->Read8();
321 // Calls that SERVER can make
322 bool wxTCPConnection::Advise (const wxString
& item
,
323 char *data
, int size
, wxIPCFormat format
)
325 if (!m_sock
->IsConnected())
328 m_codeco
->Write8(IPC_ADVISE
);
329 m_codeco
->WriteString(item
);
330 m_codeco
->Write8(format
);
332 m_codeco
->WriteString(data
);
334 m_codeco
->Write32(size
);
335 m_codeco
->Write(data
, size
);
341 void Client_OnRequest(wxSocketBase
& sock
, wxSocketBase::wxRequestEvent evt
,
345 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
346 wxDataInputStream
*codeci
;
347 wxDataOutputStream
*codeco
;
348 wxString topic_name
= connection
->m_topic
;
351 // The socket handler signals us that we lost the connection: destroy all.
352 if (evt
== wxSocketBase::EVT_LOST
) {
354 connection
->OnDisconnect();
358 // Receive message number.
359 codeci
= connection
->m_codeci
;
360 codeco
= connection
->m_codeco
;
361 msg
= codeci
->Read8();
369 format
= (wxIPCFormat
)codeci
->Read8();
370 size
= codeci
->Read32();
371 data
= new char[size
];
372 codeci
->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 codeci
->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 char[size
];
426 codeci
->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 codeco
->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 wxSocketBase::wxRequestEvent evt
, char *cdata
)
469 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
470 wxSocketStream
*stream
;
471 wxDataInputStream
*codeci
;
472 wxDataOutputStream
*codeco
;
474 if (evt
!= wxSocketBase::EVT_ACCEPT
)
477 /* Accept the connection, getting a new socket */
478 wxSocketBase
*sock
= server
.Accept();
480 sock
->SetNotify(wxSocketBase::REQ_READ
| wxSocketBase::REQ_LOST
);
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
);