]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckipc.cpp
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
9 // Copyright: (c) Julian Smart 1993
10 // (c) Guilhem Lavaux 1997, 1998
11 // (c) 2000 Guillermo Rodriguez <guille@iies.es>
12 // Licence: wxWindows license
13 /////////////////////////////////////////////////////////////////////////////
16 #pragma implementation "sckipc.h"
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
34 #include "wx/socket.h"
35 #include "wx/sckipc.h"
42 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
43 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
44 IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection
, wxConnectionBase
)
46 // It seems to be already defined somewhere in the Xt includes.
64 void Server_OnRequest(wxSocketServer
& server
,
67 void Client_OnRequest(wxSocketBase
& sock
,
71 // ---------------------------------------------------------------------------
73 // ---------------------------------------------------------------------------
75 wxTCPClient::wxTCPClient ()
80 wxTCPClient::~wxTCPClient ()
84 bool wxTCPClient::ValidHost(const wxString
& host
)
88 return addr
.Hostname(host
);
91 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
92 const wxString
& server_name
,
93 const wxString
& topic
)
95 wxSocketClient
*client
= new wxSocketClient();
96 wxSocketStream
*stream
= new wxSocketStream(*client
);
97 wxDataInputStream
*data_is
= new wxDataInputStream(*stream
);
98 wxDataOutputStream
*data_os
= new wxDataOutputStream(*stream
);
101 addr
.Service(server_name
);
104 if (client
->Connect(addr
))
108 // Send topic name, and enquire whether this has succeeded
109 data_os
->Write8(IPC_CONNECT
);
110 data_os
->WriteString(topic
);
112 msg
= data_is
->Read8();
115 if (msg
== IPC_CONNECT
)
117 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
121 if (!connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
124 // and fall through to delete everything else
128 connection
->m_topic
= topic
;
129 connection
->m_sock
= client
;
130 connection
->m_sockstrm
= stream
;
131 connection
->m_codeci
= data_is
;
132 connection
->m_codeco
= data_os
;
133 client
->Callback(Client_OnRequest
);
134 client
->CallbackData((char *)connection
);
135 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
136 client
->Notify(TRUE
);
143 // something went wrong
151 wxConnectionBase
*wxTCPClient::OnMakeConnection()
153 return new wxTCPConnection
;
156 // ---------------------------------------------------------------------------
158 // ---------------------------------------------------------------------------
160 wxTCPServer::wxTCPServer ()
165 bool wxTCPServer::Create(const wxString
& server_name
)
168 wxSocketServer
*server
;
170 addr
.LocalHost(); // GRG
171 addr
.Service(server_name
);
173 // Create a socket listening on specified port
174 server
= new wxSocketServer(addr
);
175 server
->Callback((wxSocketBase::wxSockCbk
)Server_OnRequest
);
176 server
->CallbackData((char *)this);
177 server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
178 server
->Notify(TRUE
); // GRG
183 wxTCPServer::~wxTCPServer()
187 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
189 return new wxTCPConnection();
192 // ---------------------------------------------------------------------------
194 // ---------------------------------------------------------------------------
196 wxTCPConnection::wxTCPConnection ()
197 : wxConnectionBase(),
198 m_sock(NULL
), m_sockstrm(NULL
), m_codeci(NULL
), m_codeco(NULL
)
202 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
206 wxTCPConnection::~wxTCPConnection ()
211 wxDELETE(m_sockstrm
);
214 void wxTCPConnection::Compress(bool WXUNUSED(on
))
219 // Calls that CLIENT can make.
220 bool wxTCPConnection::Disconnect ()
222 // Send the the disconnect message to the peer.
223 m_codeco
->Write8(IPC_DISCONNECT
);
229 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
231 if (!m_sock
->IsConnected())
234 // Prepare EXECUTE message
235 m_codeco
->Write8(IPC_EXECUTE
);
236 m_codeco
->Write8(format
);
239 size
= strlen(data
) + 1; // includes final NUL
241 m_codeco
->Write32(size
);
242 m_sockstrm
->Write(data
, size
);
247 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
249 if (!m_sock
->IsConnected())
252 m_codeco
->Write8(IPC_REQUEST
);
253 m_codeco
->WriteString(item
);
254 m_codeco
->Write8(format
);
256 // If Unpack doesn't initialize it.
259 ret
= m_codeci
->Read8();
267 s
= m_codeci
->Read32();
269 m_sockstrm
->Read(data
, s
);
277 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
279 if (!m_sock
->IsConnected())
282 m_codeco
->Write8(IPC_POKE
);
283 m_codeco
->WriteString(item
);
284 m_codeco
->Write8(format
);
287 size
= strlen(data
) + 1; // includes final NUL
289 m_codeco
->Write32(size
);
290 m_sockstrm
->Write(data
, size
);
295 bool wxTCPConnection::StartAdvise (const wxString
& item
)
299 if (!m_sock
->IsConnected())
302 m_codeco
->Write8(IPC_ADVISE_START
);
303 m_codeco
->WriteString(item
);
305 ret
= m_codeci
->Read8();
313 bool wxTCPConnection::StopAdvise (const wxString
& item
)
317 if (!m_sock
->IsConnected())
320 m_codeco
->Write8(IPC_ADVISE_STOP
);
321 m_codeco
->WriteString(item
);
323 msg
= m_codeci
->Read8();
331 // Calls that SERVER can make
332 bool wxTCPConnection::Advise (const wxString
& item
,
333 wxChar
*data
, int size
, wxIPCFormat format
)
335 if (!m_sock
->IsConnected())
338 m_codeco
->Write8(IPC_ADVISE
);
339 m_codeco
->WriteString(item
);
340 m_codeco
->Write8(format
);
343 size
= strlen(data
) + 1; // includes final NUL
345 m_codeco
->Write32(size
);
346 m_sockstrm
->Write(data
, size
);
351 void Client_OnRequest(wxSocketBase
& sock
, wxSocketNotify evt
,
355 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
356 wxDataInputStream
*codeci
;
357 wxDataOutputStream
*codeco
;
358 wxSocketStream
*sockstrm
;
359 wxString topic_name
= connection
->m_topic
;
362 // The socket handler signals us that we lost the connection: destroy all.
363 if (evt
== wxSOCKET_LOST
)
366 connection
->OnDisconnect();
370 // Receive message number.
371 codeci
= connection
->m_codeci
;
372 codeco
= connection
->m_codeco
;
373 sockstrm
= connection
->m_sockstrm
;
374 msg
= codeci
->Read8();
384 format
= (wxIPCFormat
)codeci
->Read8();
385 size
= codeci
->Read32();
386 data
= new char[size
];
387 sockstrm
->Read(data
, size
);
389 connection
->OnExecute (topic_name
, data
, size
, format
);
400 item
= codeci
->ReadString();
401 format
= (wxIPCFormat
)codeci
->Read8();
402 size
= codeci
->Read32();
403 data
= new char[size
];
404 sockstrm
->Read(data
, size
);
406 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
411 case IPC_ADVISE_START
:
413 item
= codeci
->ReadString();
415 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
417 codeco
->Write8(IPC_ADVISE_START
);
419 codeco
->Write8(IPC_FAIL
);
423 case IPC_ADVISE_STOP
:
425 item
= codeci
->ReadString();
427 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
429 codeco
->Write8(IPC_ADVISE_STOP
);
431 codeco
->Write8(IPC_FAIL
);
441 item
= codeci
->ReadString();
442 format
= (wxIPCFormat
)codeci
->Read8();
443 size
= codeci
->Read32();
444 data
= new wxChar
[size
];
445 sockstrm
->Read(data
, size
);
447 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
457 item
= codeci
->ReadString();
458 format
= (wxIPCFormat
)codeci
->Read8();
461 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
465 codeco
->Write8(IPC_REQUEST_REPLY
);
468 user_size
= strlen(user_data
) + 1; // includes final NUL
470 codeco
->Write32(user_size
);
471 sockstrm
->Write(user_data
, user_size
);
474 codeco
->Write8(IPC_FAIL
);
481 connection
->OnDisconnect();
485 codeco
->Write8(IPC_FAIL
);
490 void Server_OnRequest(wxSocketServer
& server
,
491 wxSocketNotify evt
, char *cdata
)
493 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
494 wxSocketStream
*stream
;
495 wxDataInputStream
*codeci
;
496 wxDataOutputStream
*codeco
;
498 if (evt
!= wxSOCKET_CONNECTION
)
501 /* Accept the connection, getting a new socket */
502 wxSocketBase
*sock
= server
.Accept();
506 stream
= new wxSocketStream(*sock
);
507 codeci
= new wxDataInputStream(*stream
);
508 codeco
= new wxDataOutputStream(*stream
);
511 msg
= codeci
->Read8();
513 if (msg
== IPC_CONNECT
)
516 topic_name
= codeci
->ReadString();
518 /* Register new socket with the notifier */
519 wxTCPConnection
*new_connection
=
520 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
523 if (!new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
525 delete new_connection
;
526 codeco
->Write8(IPC_FAIL
);
529 // Acknowledge success
530 codeco
->Write8(IPC_CONNECT
);
531 new_connection
->m_topic
= topic_name
;
532 new_connection
->m_sock
= sock
;
533 new_connection
->m_sockstrm
= stream
;
534 new_connection
->m_codeci
= codeci
;
535 new_connection
->m_codeco
= codeco
;
536 sock
->Callback(Client_OnRequest
);
537 sock
->CallbackData((char *)new_connection
);
538 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
543 // Send failure message
544 codeco
->Write8(IPC_FAIL
);