]>
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 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
39 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
40 IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection
, wxConnectionBase
)
42 // It seems to be already defined somewhere in the Xt includes.
60 void Server_OnRequest(wxSocketServer
& server
,
63 void Client_OnRequest(wxSocketBase
& sock
,
67 // ---------------------------------------------------------------------------
69 // ---------------------------------------------------------------------------
71 wxTCPClient::wxTCPClient ()
76 wxTCPClient::~wxTCPClient ()
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 wxSocketClient
*client
= new wxSocketClient();
93 wxSocketStream
*stream
= new wxSocketStream(*client
);
94 wxDataInputStream
data_is(*stream
);
95 wxDataOutputStream
data_os(*stream
);
97 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
98 addr
.Service(server_name
);
101 if (!client
->Connect(addr
)) {
105 client
->Notify(FALSE
);
107 // Send topic name, and enquire whether this has succeeded
110 data_os
.Write8(IPC_CONNECT
);
111 data_os
.WriteString(topic
);
113 msg
= data_is
.Read8();
116 if (msg
== IPC_CONNECT
) {
117 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
119 if (!connection
->IsKindOf(CLASSINFO(wxTCPConnection
))) {
123 connection
->m_topic
= topic
;
124 client
->Callback(Client_OnRequest
);
125 client
->CallbackData((char *)connection
);
126 client
->Notify(TRUE
);
138 wxConnectionBase
*wxTCPClient::OnMakeConnection()
140 return new wxTCPConnection
;
143 // ---------------------------------------------------------------------------
145 // ---------------------------------------------------------------------------
147 wxTCPServer::wxTCPServer ()
152 bool wxTCPServer::Create(const wxString
& server_name
)
155 wxSocketServer
*server
;
157 addr
.Service(server_name
);
159 // Create a socket listening on specified port
160 server
= new wxSocketServer(addr
);
161 server
->Callback((wxSocketBase::wxSockCbk
)Server_OnRequest
);
162 server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
164 server
->CallbackData((char *)this);
169 wxTCPServer::~wxTCPServer()
173 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
175 return new wxTCPConnection();
178 // ---------------------------------------------------------------------------
180 // ---------------------------------------------------------------------------
182 wxTCPConnection::wxTCPConnection ()
183 : wxConnectionBase(),
184 m_sock(NULL
), m_sockstrm(NULL
), m_codeci(NULL
), m_codeco(NULL
)
188 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
192 wxTCPConnection::~wxTCPConnection ()
197 wxDELETE(m_sockstrm
);
200 void wxTCPConnection::Compress(bool WXUNUSED(on
))
205 // Calls that CLIENT can make.
206 bool wxTCPConnection::Disconnect ()
208 // Send the the disconnect message to the peer.
209 m_codeco
->Write8(IPC_DISCONNECT
);
215 bool wxTCPConnection::Execute (const wxChar
*data
, int size
, wxIPCFormat format
)
217 if (!m_sock
->IsConnected())
220 // Prepare EXECUTE message
221 m_codeco
->Write8(IPC_EXECUTE
);
222 m_codeco
->Write8(format
);
224 m_codeco
->WriteString(data
);
226 m_codeco
->Write32(size
);
227 m_sockstrm
->Write(data
, size
);
233 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
235 if (!m_sock
->IsConnected())
238 m_codeco
->Write8(IPC_REQUEST
);
239 m_codeco
->WriteString(item
);
240 m_codeco
->Write8(format
);
242 // If Unpack doesn't initialize it.
245 ret
= m_codeci
->Read8();
252 s
= m_codeci
->Read32();
254 m_sockstrm
->Read(data
, s
);
262 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
264 if (!m_sock
->IsConnected())
267 m_codeco
->Write8(IPC_POKE
);
268 m_codeco
->WriteString(item
);
269 m_codeco
->Write8(format
);
271 m_codeco
->WriteString(data
);
273 m_codeco
->Write32(size
);
274 m_sockstrm
->Write(data
, size
);
280 bool wxTCPConnection::StartAdvise (const wxString
& item
)
284 if (!m_sock
->IsConnected())
287 m_codeco
->Write8(IPC_ADVISE_START
);
288 m_codeco
->WriteString(item
);
290 ret
= m_codeci
->Read8();
298 bool wxTCPConnection::StopAdvise (const wxString
& item
)
302 if (!m_sock
->IsConnected())
305 m_codeco
->Write8(IPC_ADVISE_STOP
);
306 m_codeco
->WriteString(item
);
308 msg
= m_codeci
->Read8();
316 // Calls that SERVER can make
317 bool wxTCPConnection::Advise (const wxString
& item
,
318 wxChar
*data
, int size
, wxIPCFormat format
)
320 if (!m_sock
->IsConnected())
323 m_codeco
->Write8(IPC_ADVISE
);
324 m_codeco
->WriteString(item
);
325 m_codeco
->Write8(format
);
327 m_codeco
->WriteString(data
);
329 m_codeco
->Write32(size
);
330 m_sockstrm
->Write(data
, size
);
336 void Client_OnRequest(wxSocketBase
& sock
, wxSocketNotify evt
,
340 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
341 wxDataInputStream
*codeci
;
342 wxDataOutputStream
*codeco
;
343 wxSocketStream
*sockstrm
;
344 wxString topic_name
= connection
->m_topic
;
347 // The socket handler signals us that we lost the connection: destroy all.
348 if (evt
== wxSOCKET_LOST
) {
350 connection
->OnDisconnect();
354 // Receive message number.
355 codeci
= connection
->m_codeci
;
356 codeco
= connection
->m_codeco
;
357 sockstrm
= connection
->m_sockstrm
;
358 msg
= codeci
->Read8();
366 format
= (wxIPCFormat
)codeci
->Read8();
367 size
= codeci
->Read32();
368 data
= new char[size
];
369 sockstrm
->Read(data
, size
);
371 connection
->OnExecute (topic_name
, data
, size
, format
);
381 item
= codeci
->ReadString();
382 format
= (wxIPCFormat
)codeci
->Read8();
383 size
= codeci
->Read32();
384 data
= new char[size
];
385 sockstrm
->Read(data
, size
);
387 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
392 case IPC_ADVISE_START
: {
393 item
= codeci
->ReadString();
395 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
397 codeco
->Write8(IPC_ADVISE_START
);
399 codeco
->Write8(IPC_FAIL
);
403 case IPC_ADVISE_STOP
: {
404 item
= codeci
->ReadString();
406 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
408 codeco
->Write8(IPC_ADVISE_STOP
);
410 codeco
->Write8(IPC_FAIL
);
419 item
= codeci
->ReadString();
420 format
= (wxIPCFormat
)codeci
->Read8();
421 size
= codeci
->Read32();
422 data
= new wxChar
[size
];
423 sockstrm
->Read(data
, size
);
425 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
434 item
= codeci
->ReadString();
435 format
= (wxIPCFormat
)codeci
->Read8();
438 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
441 codeco
->Write8(IPC_REQUEST_REPLY
);
442 if (user_size
!= -1) {
443 codeco
->Write32(user_size
);
444 sockstrm
->Write(user_data
, user_size
);
446 codeco
->WriteString(user_data
);
448 codeco
->Write8(IPC_FAIL
);
452 case IPC_DISCONNECT
: {
454 connection
->OnDisconnect();
458 codeco
->Write8(IPC_FAIL
);
463 void Server_OnRequest(wxSocketServer
& server
,
464 wxSocketNotify evt
, char *cdata
)
466 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
467 wxSocketStream
*stream
;
468 wxDataInputStream
*codeci
;
469 wxDataOutputStream
*codeco
;
471 if (evt
!= wxSOCKET_CONNECTION
)
474 /* Accept the connection, getting a new socket */
475 wxSocketBase
*sock
= server
.Accept();
477 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
479 stream
= new wxSocketStream(*sock
);
480 codeci
= new wxDataInputStream(*stream
);
481 codeco
= new wxDataOutputStream(*stream
);
487 msg
= codeci
->Read8();
489 if (msg
== IPC_CONNECT
) {
491 topic_name
= codeci
->ReadString();
493 /* Register new socket with the notifier */
494 wxTCPConnection
*new_connection
=
495 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
496 if (new_connection
) {
497 if (!new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
))) {
498 delete new_connection
;
499 codeco
->Write8(IPC_FAIL
);
502 // Acknowledge success
503 codeco
->Write8(IPC_CONNECT
);
505 new_connection
->m_topic
= topic_name
;
506 new_connection
->m_sockstrm
= stream
;
507 new_connection
->m_codeci
= codeci
;
508 new_connection
->m_codeco
= codeco
;
509 sock
->Callback(Client_OnRequest
);
510 sock
->CallbackData((char *)new_connection
);
513 // Send failure message
514 codeco
->Write8(IPC_FAIL
);