]>
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 (void)
196 wxDELETE(m_sockstrm
);
199 void wxTCPConnection::Compress(bool WXUNUSED(on
))
204 // Calls that CLIENT can make.
205 bool wxTCPConnection::Disconnect (void)
207 // Send the the disconnect message to the peer.
208 m_codeco
->Write8(IPC_DISCONNECT
);
214 bool wxTCPConnection::Execute (char *data
, int size
, wxDataFormat format
)
216 if (!m_sock
->IsConnected())
219 // Prepare EXECUTE message
220 m_codeco
->Write8(IPC_EXECUTE
);
221 m_codeco
->Write8(format
);
223 m_codeco
->WriteString(data
);
225 m_codeco
->Write32(size
);
226 m_codeco
->Write(data
, size
);
232 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxDataFormat format
)
234 if (!m_sock
->IsConnected())
237 m_codeco
->Write8(IPC_REQUEST
);
238 m_codeco
->WriteString(item
);
239 m_codeco
->Write8(format
);
241 // If Unpack doesn't initialize it.
244 ret
= m_codeci
->Read8();
251 s
= m_codeci
->Read32();
253 m_codeci
->Read(data
, s
);
261 bool wxTCPConnection::Poke (const wxString
& item
, char *data
, int size
, wxDataFormat format
)
263 if (!m_sock
->IsConnected())
266 m_codeco
->Write8(IPC_POKE
);
267 m_codeco
->WriteString(item
);
268 m_codeco
->Write8(format
);
270 m_codeco
->WriteString(data
);
272 m_codeco
->Write32(size
);
273 m_codeco
->Write(data
, size
);
279 bool wxTCPConnection::StartAdvise (const wxString
& item
)
283 if (!m_sock
->IsConnected())
286 m_codeco
->Write8(IPC_ADVISE_START
);
287 m_codeco
->WriteString(item
);
289 ret
= m_codeci
->Read8();
297 bool wxTCPConnection::StopAdvise (const wxString
& item
)
301 if (!m_sock
->IsConnected())
304 m_codeco
->Write8(IPC_ADVISE_STOP
);
305 m_codeco
->WriteString(item
);
307 msg
= m_codeci
->Read8();
315 // Calls that SERVER can make
316 bool wxTCPConnection::Advise (const wxString
& item
,
317 char *data
, int size
, wxDataFormat format
)
319 if (!m_sock
->IsConnected())
322 m_codeco
->Write8(IPC_ADVISE
);
323 m_codeco
->WriteString(item
);
324 m_codeco
->Write8(format
);
326 m_codeco
->WriteString(data
);
328 m_codeco
->Write32(size
);
329 m_codeco
->Write(data
, size
);
335 void Client_OnRequest(wxSocketBase
& sock
, wxSocketBase::wxRequestEvent evt
,
339 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
340 wxDataInputStream
*codeci
;
341 wxDataOutputStream
*codeco
;
342 wxString topic_name
= connection
->m_topic
;
345 // The socket handler signals us that we lost the connection: destroy all.
346 if (evt
== wxSocketBase::EVT_LOST
) {
348 connection
->OnDisconnect();
352 // Receive message number.
353 codeci
= connection
->m_codeci
;
354 codeco
= connection
->m_codeco
;
355 msg
= codeci
->Read8();
363 format
= (wxDataFormat
)codeci
->Read8();
364 size
= codeci
->Read32();
365 data
= new char[size
];
366 codeci
->Read(data
, size
);
368 connection
->OnExecute (topic_name
, data
, size
, format
);
378 item
= codeci
->ReadString();
379 format
= (wxDataFormat
)codeci
->Read8();
380 size
= codeci
->Read32();
381 data
= new char[size
];
382 codeci
->Read(data
, size
);
384 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
389 case IPC_ADVISE_START
: {
390 item
= codeci
->ReadString();
392 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
394 codeco
->Write8(IPC_ADVISE_START
);
396 codeco
->Write8(IPC_FAIL
);
400 case IPC_ADVISE_STOP
: {
401 item
= codeci
->ReadString();
403 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
405 codeco
->Write8(IPC_ADVISE_STOP
);
407 codeco
->Write8(IPC_FAIL
);
416 item
= codeci
->ReadString();
417 format
= (wxDataFormat
)codeci
->Read8();
418 size
= codeci
->Read32();
419 data
= new char[size
];
420 codeci
->Read(data
, size
);
422 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
431 item
= codeci
->ReadString();
432 format
= (wxDataFormat
)codeci
->Read8();
435 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
438 codeco
->Write8(IPC_REQUEST_REPLY
);
439 if (user_size
!= -1) {
440 codeco
->Write32(user_size
);
441 codeco
->Write(user_data
, user_size
);
443 codeco
->WriteString(user_data
);
445 codeco
->Write8(IPC_FAIL
);
449 case IPC_DISCONNECT
: {
451 connection
->OnDisconnect();
455 codeco
->Write8(IPC_FAIL
);
460 void Server_OnRequest(wxSocketServer
& server
,
461 wxSocketBase::wxRequestEvent evt
, char *cdata
)
463 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
464 wxSocketStream
*stream
;
465 wxDataInputStream
*codeci
;
466 wxDataOutputStream
*codeco
;
468 if (evt
!= wxSocketBase::EVT_ACCEPT
)
471 /* Accept the connection, getting a new socket */
472 wxSocketBase
*sock
= server
.Accept();
474 sock
->SetNotify(wxSocketBase::REQ_READ
| wxSocketBase::REQ_LOST
);
476 stream
= new wxSocketStream(*sock
);
477 codeci
= new wxDataInputStream(*stream
);
478 codeco
= new wxDataOutputStream(*stream
);
484 msg
= codeci
->Read8();
486 if (msg
== IPC_CONNECT
) {
488 topic_name
= codeci
->ReadString();
490 /* Register new socket with the notifier */
491 wxTCPConnection
*new_connection
=
492 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
493 if (new_connection
) {
494 if (!new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
))) {
495 delete new_connection
;
496 codeco
->Write8(IPC_FAIL
);
499 // Acknowledge success
500 codeco
->Write8(IPC_CONNECT
);
502 new_connection
->m_topic
= topic_name
;
503 new_connection
->m_sockstrm
= stream
;
504 new_connection
->m_codeci
= codeci
;
505 new_connection
->m_codeco
= codeco
;
506 sock
->Callback(Client_OnRequest
);
507 sock
->CallbackData((char *)new_connection
);
510 // Send failure message
511 codeco
->Write8(IPC_FAIL
);