]>
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"
35 #include "wx/socket.h"
36 #include "wx/sckipc.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer
, wxServerBase
)
44 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient
, wxClientBase
)
45 IMPLEMENT_CLASS(wxTCPConnection
, wxConnectionBase
)
47 // It seems to be already defined somewhere in the Xt includes.
65 void Server_OnRequest(wxSocketServer
& server
,
68 void Client_OnRequest(wxSocketBase
& sock
,
72 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
76 wxTCPClient::wxTCPClient ()
81 wxTCPClient::~wxTCPClient ()
85 bool wxTCPClient::ValidHost(const wxString
& host
)
89 return addr
.Hostname(host
);
92 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
93 const wxString
& server_name
,
94 const wxString
& topic
)
96 wxSocketClient
*client
= new wxSocketClient();
97 wxSocketStream
*stream
= new wxSocketStream(*client
);
98 wxDataInputStream
*data_is
= new wxDataInputStream(*stream
);
99 wxDataOutputStream
*data_os
= new wxDataOutputStream(*stream
);
102 addr
.Service(server_name
);
105 if (client
->Connect(addr
))
109 // 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
)
118 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
122 if (!connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
125 // and fall through to delete everything else
129 connection
->m_topic
= topic
;
130 connection
->m_sock
= client
;
131 connection
->m_sockstrm
= stream
;
132 connection
->m_codeci
= data_is
;
133 connection
->m_codeco
= data_os
;
134 client
->Callback(Client_OnRequest
);
135 client
->CallbackData((char *)connection
);
136 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
137 client
->Notify(TRUE
);
144 // something went wrong
152 wxConnectionBase
*wxTCPClient::OnMakeConnection()
154 return new wxTCPConnection
;
157 // ---------------------------------------------------------------------------
159 // ---------------------------------------------------------------------------
161 wxTCPServer::wxTCPServer ()
166 bool wxTCPServer::Create(const wxString
& server_name
)
169 wxSocketServer
*server
;
171 addr
.LocalHost(); // GRG
172 addr
.Service(server_name
);
174 // Create a socket listening on specified port
175 server
= new wxSocketServer(addr
);
176 server
->Callback((wxSocketBase::wxSockCbk
)Server_OnRequest
);
177 server
->CallbackData((char *)this);
178 server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
179 server
->Notify(TRUE
); // GRG
184 wxTCPServer::~wxTCPServer()
188 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
190 return new wxTCPConnection();
193 // ---------------------------------------------------------------------------
195 // ---------------------------------------------------------------------------
197 wxTCPConnection::wxTCPConnection ()
198 : wxConnectionBase(),
199 m_sock(NULL
), m_sockstrm(NULL
), m_codeci(NULL
), m_codeco(NULL
)
203 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
207 wxTCPConnection::~wxTCPConnection ()
212 wxDELETE(m_sockstrm
);
215 void wxTCPConnection::Compress(bool WXUNUSED(on
))
220 // Calls that CLIENT can make.
221 bool wxTCPConnection::Disconnect ()
223 // Send the the disconnect message to the peer.
224 m_codeco
->Write8(IPC_DISCONNECT
);
230 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
232 if (!m_sock
->IsConnected())
235 // Prepare EXECUTE message
236 m_codeco
->Write8(IPC_EXECUTE
);
237 m_codeco
->Write8(format
);
240 size
= strlen(data
) + 1; // includes final NUL
242 m_codeco
->Write32(size
);
243 m_sockstrm
->Write(data
, size
);
248 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
250 if (!m_sock
->IsConnected())
253 m_codeco
->Write8(IPC_REQUEST
);
254 m_codeco
->WriteString(item
);
255 m_codeco
->Write8(format
);
257 // If Unpack doesn't initialize it.
260 ret
= m_codeci
->Read8();
268 s
= m_codeci
->Read32();
270 m_sockstrm
->Read(data
, s
);
278 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
280 if (!m_sock
->IsConnected())
283 m_codeco
->Write8(IPC_POKE
);
284 m_codeco
->WriteString(item
);
285 m_codeco
->Write8(format
);
288 size
= strlen(data
) + 1; // includes final NUL
290 m_codeco
->Write32(size
);
291 m_sockstrm
->Write(data
, size
);
296 bool wxTCPConnection::StartAdvise (const wxString
& item
)
300 if (!m_sock
->IsConnected())
303 m_codeco
->Write8(IPC_ADVISE_START
);
304 m_codeco
->WriteString(item
);
306 ret
= m_codeci
->Read8();
314 bool wxTCPConnection::StopAdvise (const wxString
& item
)
318 if (!m_sock
->IsConnected())
321 m_codeco
->Write8(IPC_ADVISE_STOP
);
322 m_codeco
->WriteString(item
);
324 msg
= m_codeci
->Read8();
332 // Calls that SERVER can make
333 bool wxTCPConnection::Advise (const wxString
& item
,
334 wxChar
*data
, int size
, wxIPCFormat format
)
336 if (!m_sock
->IsConnected())
339 m_codeco
->Write8(IPC_ADVISE
);
340 m_codeco
->WriteString(item
);
341 m_codeco
->Write8(format
);
344 size
= strlen(data
) + 1; // includes final NUL
346 m_codeco
->Write32(size
);
347 m_sockstrm
->Write(data
, size
);
352 void Client_OnRequest(wxSocketBase
& sock
, wxSocketNotify evt
,
356 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
357 wxDataInputStream
*codeci
;
358 wxDataOutputStream
*codeco
;
359 wxSocketStream
*sockstrm
;
360 wxString topic_name
= connection
->m_topic
;
363 // The socket handler signals us that we lost the connection: destroy all.
364 if (evt
== wxSOCKET_LOST
)
367 connection
->OnDisconnect();
371 // Receive message number.
372 codeci
= connection
->m_codeci
;
373 codeco
= connection
->m_codeco
;
374 sockstrm
= connection
->m_sockstrm
;
375 msg
= codeci
->Read8();
385 format
= (wxIPCFormat
)codeci
->Read8();
386 size
= codeci
->Read32();
387 data
= new char[size
];
388 sockstrm
->Read(data
, size
);
390 connection
->OnExecute (topic_name
, data
, size
, format
);
401 item
= codeci
->ReadString();
402 format
= (wxIPCFormat
)codeci
->Read8();
403 size
= codeci
->Read32();
404 data
= new char[size
];
405 sockstrm
->Read(data
, size
);
407 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
412 case IPC_ADVISE_START
:
414 item
= codeci
->ReadString();
416 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
418 codeco
->Write8(IPC_ADVISE_START
);
420 codeco
->Write8(IPC_FAIL
);
424 case IPC_ADVISE_STOP
:
426 item
= codeci
->ReadString();
428 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
430 codeco
->Write8(IPC_ADVISE_STOP
);
432 codeco
->Write8(IPC_FAIL
);
442 item
= codeci
->ReadString();
443 format
= (wxIPCFormat
)codeci
->Read8();
444 size
= codeci
->Read32();
445 data
= new wxChar
[size
];
446 sockstrm
->Read(data
, size
);
448 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
458 item
= codeci
->ReadString();
459 format
= (wxIPCFormat
)codeci
->Read8();
462 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
466 codeco
->Write8(IPC_REQUEST_REPLY
);
469 user_size
= strlen(user_data
) + 1; // includes final NUL
471 codeco
->Write32(user_size
);
472 sockstrm
->Write(user_data
, user_size
);
475 codeco
->Write8(IPC_FAIL
);
482 connection
->OnDisconnect();
486 codeco
->Write8(IPC_FAIL
);
491 void Server_OnRequest(wxSocketServer
& server
,
492 wxSocketNotify evt
, char *cdata
)
494 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
495 wxSocketStream
*stream
;
496 wxDataInputStream
*codeci
;
497 wxDataOutputStream
*codeco
;
499 if (evt
!= wxSOCKET_CONNECTION
)
502 /* Accept the connection, getting a new socket */
503 wxSocketBase
*sock
= server
.Accept();
507 stream
= new wxSocketStream(*sock
);
508 codeci
= new wxDataInputStream(*stream
);
509 codeco
= new wxDataOutputStream(*stream
);
512 msg
= codeci
->Read8();
514 if (msg
== IPC_CONNECT
)
517 topic_name
= codeci
->ReadString();
519 /* Register new socket with the notifier */
520 wxTCPConnection
*new_connection
=
521 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
524 if (!new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
526 delete new_connection
;
527 codeco
->Write8(IPC_FAIL
);
530 // Acknowledge success
531 codeco
->Write8(IPC_CONNECT
);
532 new_connection
->m_topic
= topic_name
;
533 new_connection
->m_sock
= sock
;
534 new_connection
->m_sockstrm
= stream
;
535 new_connection
->m_codeci
= codeci
;
536 new_connection
->m_codeco
= codeco
;
537 sock
->Callback(Client_OnRequest
);
538 sock
->CallbackData((char *)new_connection
);
539 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
544 // Send failure message
545 codeco
->Write8(IPC_FAIL
);