]>
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"
29 #include "wx/socket.h"
30 #include "wx/sckipc.h"
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
38 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
39 IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection
, wxConnectionBase
)
42 // It seems to be already defined somewhere in the Xt includes.
60 void Server_OnRequest(wxSocketServer
& server
,
61 wxSocketBase::wxRequestEvent evt
,
63 void Client_OnRequest(wxSocketBase
& sock
,
64 wxSocketBase::wxRequestEvent evt
,
67 // ---------------------------------------------------------------------------
69 // ---------------------------------------------------------------------------
71 wxTCPClient::wxTCPClient (void)
76 wxTCPClient::~wxTCPClient (void)
80 bool wxTCPClient::ValidHost(const wxString
& host
)
84 return addr
.Hostname(host
);
87 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
88 const wxString
& server_name
,
89 const wxString
& topic
)
92 wxSocketHandler
*hsock
= &wxSocketHandler::Master();
93 wxSocketClient
*client
= hsock
->CreateClient();
94 wxSocketStream
*stream
= new wxSocketStream(*client
);
95 wxDataInputStream
data_is(*stream
);
96 wxDataOutputStream
data_os(*stream
);
98 client
->SetNotify(wxSocketBase::REQ_READ
| wxSocketBase::REQ_LOST
);
99 addr
.Service(server_name
);
102 if (!client
->Connect(addr
)) {
106 client
->Notify(FALSE
);
108 // Send topic name, and enquire whether this has succeeded
111 data_os
.Write8(IPC_CONNECT
);
112 data_os
.WriteString(topic
);
114 msg
= data_is
.Read8();
117 if (msg
== IPC_CONNECT
) {
118 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
120 if (!connection
->IsKindOf(CLASSINFO(wxTCPConnection
))) {
124 connection
->m_topic
= topic
;
125 client
->Callback(Client_OnRequest
);
126 client
->CallbackData((char *)connection
);
127 client
->Notify(TRUE
);
140 wxConnectionBase
*wxTCPClient::OnMakeConnection()
142 return new wxTCPConnection
;
145 // ---------------------------------------------------------------------------
147 // ---------------------------------------------------------------------------
149 wxTCPServer::wxTCPServer (void)
154 bool wxTCPServer::Create(const wxString
& server_name
)
157 wxSocketHandler
*hsock
= &wxSocketHandler::Master();
158 wxSocketServer
*server
;
160 addr
.Service(server_name
);
162 // Create a socket listening on specified port
163 server
= hsock
->CreateServer(addr
);
164 server
->Callback((wxSocketBase::wxSockCbk
)Server_OnRequest
);
165 server
->SetNotify(wxSocketBase::REQ_ACCEPT
);
167 server
->CallbackData((char *)this);
172 wxTCPServer::~wxTCPServer(void)
176 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
178 return new wxTCPConnection();
181 // ---------------------------------------------------------------------------
183 // ---------------------------------------------------------------------------
185 wxTCPConnection::wxTCPConnection (void)
186 : wxConnectionBase(),
187 m_sock(NULL
), m_sockstrm(NULL
), m_codeci(NULL
), m_codeco(NULL
)
191 wxTCPConnection::wxTCPConnection(char *buffer
, int size
)
195 wxTCPConnection::~wxTCPConnection (void)
200 wxDELETE(m_sockstrm
);
203 void wxTCPConnection::Compress(bool WXUNUSED(on
))
208 // Calls that CLIENT can make.
209 bool wxTCPConnection::Disconnect (void)
211 // Send the the disconnect message to the peer.
212 m_codeco
->Write8(IPC_DISCONNECT
);
218 bool wxTCPConnection::Execute (char *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_codeco
->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_codeci
->Read(data
, s
);
265 bool wxTCPConnection::Poke (const wxString
& item
, char *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_codeco
->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 char *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_codeco
->Write(data
, size
);
339 void Client_OnRequest(wxSocketBase
& sock
, wxSocketBase::wxRequestEvent evt
,
343 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
344 wxDataInputStream
*codeci
;
345 wxDataOutputStream
*codeco
;
346 wxString topic_name
= connection
->m_topic
;
349 // The socket handler signals us that we lost the connection: destroy all.
350 if (evt
== wxSocketBase::EVT_LOST
) {
352 connection
->OnDisconnect();
356 // Receive message number.
357 codeci
= connection
->m_codeci
;
358 codeco
= connection
->m_codeco
;
359 msg
= codeci
->Read8();
367 format
= (wxIPCFormat
)codeci
->Read8();
368 size
= codeci
->Read32();
369 data
= new char[size
];
370 codeci
->Read(data
, size
);
372 connection
->OnExecute (topic_name
, data
, size
, format
);
382 item
= codeci
->ReadString();
383 format
= (wxIPCFormat
)codeci
->Read8();
384 size
= codeci
->Read32();
385 data
= new char[size
];
386 codeci
->Read(data
, size
);
388 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
393 case IPC_ADVISE_START
: {
394 item
= codeci
->ReadString();
396 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
398 codeco
->Write8(IPC_ADVISE_START
);
400 codeco
->Write8(IPC_FAIL
);
404 case IPC_ADVISE_STOP
: {
405 item
= codeci
->ReadString();
407 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
409 codeco
->Write8(IPC_ADVISE_STOP
);
411 codeco
->Write8(IPC_FAIL
);
420 item
= codeci
->ReadString();
421 format
= (wxIPCFormat
)codeci
->Read8();
422 size
= codeci
->Read32();
423 data
= new char[size
];
424 codeci
->Read(data
, size
);
426 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
435 item
= codeci
->ReadString();
436 format
= (wxIPCFormat
)codeci
->Read8();
439 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
442 codeco
->Write8(IPC_REQUEST_REPLY
);
443 if (user_size
!= -1) {
444 codeco
->Write32(user_size
);
445 codeco
->Write(user_data
, user_size
);
447 codeco
->WriteString(user_data
);
449 codeco
->Write8(IPC_FAIL
);
453 case IPC_DISCONNECT
: {
455 connection
->OnDisconnect();
459 codeco
->Write8(IPC_FAIL
);
464 void Server_OnRequest(wxSocketServer
& server
,
465 wxSocketBase::wxRequestEvent evt
, char *cdata
)
467 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
468 wxSocketStream
*stream
;
469 wxDataInputStream
*codeci
;
470 wxDataOutputStream
*codeco
;
472 if (evt
!= wxSocketBase::EVT_ACCEPT
)
475 /* Accept the connection, getting a new socket */
476 wxSocketBase
*sock
= server
.Accept();
478 sock
->SetNotify(wxSocketBase::REQ_READ
| wxSocketBase::REQ_LOST
);
480 stream
= new wxSocketStream(*sock
);
481 codeci
= new wxDataInputStream(*stream
);
482 codeco
= new wxDataOutputStream(*stream
);
488 msg
= codeci
->Read8();
490 if (msg
== IPC_CONNECT
) {
492 topic_name
= codeci
->ReadString();
494 /* Register new socket with the notifier */
495 wxTCPConnection
*new_connection
=
496 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
497 if (new_connection
) {
498 if (!new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
))) {
499 delete new_connection
;
500 codeco
->Write8(IPC_FAIL
);
503 // Acknowledge success
504 codeco
->Write8(IPC_CONNECT
);
506 new_connection
->m_topic
= topic_name
;
507 new_connection
->m_sockstrm
= stream
;
508 new_connection
->m_codeci
= codeci
;
509 new_connection
->m_codeco
= codeco
;
510 sock
->Callback(Client_OnRequest
);
511 sock
->CallbackData((char *)new_connection
);
514 // Send failure message
515 codeco
->Write8(IPC_FAIL
);