]>
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
,
73 // All sockets will be created with the following flags
75 #define SCKIPC_FLAGS (wxSOCKET_WAITALL)
77 // ---------------------------------------------------------------------------
79 // ---------------------------------------------------------------------------
81 wxTCPClient::wxTCPClient ()
86 wxTCPClient::~wxTCPClient ()
90 bool wxTCPClient::ValidHost(const wxString
& host
)
94 return addr
.Hostname(host
);
97 wxConnectionBase
*wxTCPClient::MakeConnection (const wxString
& host
,
98 const wxString
& server_name
,
99 const wxString
& topic
)
101 wxSocketClient
*client
= new wxSocketClient(SCKIPC_FLAGS
);
102 wxSocketStream
*stream
= new wxSocketStream(*client
);
103 wxDataInputStream
*data_is
= new wxDataInputStream(*stream
);
104 wxDataOutputStream
*data_os
= new wxDataOutputStream(*stream
);
107 addr
.Service(server_name
);
110 if (client
->Connect(addr
))
114 // Send topic name, and enquire whether this has succeeded
115 data_os
->Write8(IPC_CONNECT
);
116 data_os
->WriteString(topic
);
118 msg
= data_is
->Read8();
121 if (msg
== IPC_CONNECT
)
123 wxTCPConnection
*connection
= (wxTCPConnection
*)OnMakeConnection ();
127 if (!connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
130 // and fall through to delete everything else
134 connection
->m_topic
= topic
;
135 connection
->m_sock
= client
;
136 connection
->m_sockstrm
= stream
;
137 connection
->m_codeci
= data_is
;
138 connection
->m_codeco
= data_os
;
139 client
->Callback(Client_OnRequest
);
140 client
->CallbackData((char *)connection
);
141 client
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
142 client
->Notify(TRUE
);
149 // something went wrong
157 wxConnectionBase
*wxTCPClient::OnMakeConnection()
159 return new wxTCPConnection
;
162 // ---------------------------------------------------------------------------
164 // ---------------------------------------------------------------------------
166 wxTCPServer::wxTCPServer ()
171 bool wxTCPServer::Create(const wxString
& server_name
)
173 wxSocketServer
*server
;
175 // wxIPV4address defaults to INADDR_ANY:0
177 addr
.Service(server_name
);
179 // Create a socket listening on specified port
180 server
= new wxSocketServer(addr
, SCKIPC_FLAGS
);
181 server
->Callback((wxSocketBase::wxSockCbk
)Server_OnRequest
);
182 server
->CallbackData((char *)this);
183 server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
184 server
->Notify(TRUE
);
189 wxTCPServer::~wxTCPServer()
193 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
195 return new wxTCPConnection();
198 // ---------------------------------------------------------------------------
200 // ---------------------------------------------------------------------------
202 wxTCPConnection::wxTCPConnection ()
203 : wxConnectionBase(),
204 m_sock(NULL
), m_sockstrm(NULL
), m_codeci(NULL
), m_codeco(NULL
)
208 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
212 wxTCPConnection::~wxTCPConnection ()
217 wxDELETE(m_sockstrm
);
220 void wxTCPConnection::Compress(bool WXUNUSED(on
))
225 // Calls that CLIENT can make.
226 bool wxTCPConnection::Disconnect ()
228 // Send the the disconnect message to the peer.
229 m_codeco
->Write8(IPC_DISCONNECT
);
235 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
237 if (!m_sock
->IsConnected())
240 // Prepare EXECUTE message
241 m_codeco
->Write8(IPC_EXECUTE
);
242 m_codeco
->Write8(format
);
245 size
= strlen(data
) + 1; // includes final NUL
247 m_codeco
->Write32(size
);
248 m_sockstrm
->Write(data
, size
);
253 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
255 if (!m_sock
->IsConnected())
258 m_codeco
->Write8(IPC_REQUEST
);
259 m_codeco
->WriteString(item
);
260 m_codeco
->Write8(format
);
262 // If Unpack doesn't initialize it.
265 ret
= m_codeci
->Read8();
273 s
= m_codeci
->Read32();
275 m_sockstrm
->Read(data
, s
);
283 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
285 if (!m_sock
->IsConnected())
288 m_codeco
->Write8(IPC_POKE
);
289 m_codeco
->WriteString(item
);
290 m_codeco
->Write8(format
);
293 size
= strlen(data
) + 1; // includes final NUL
295 m_codeco
->Write32(size
);
296 m_sockstrm
->Write(data
, size
);
301 bool wxTCPConnection::StartAdvise (const wxString
& item
)
305 if (!m_sock
->IsConnected())
308 m_codeco
->Write8(IPC_ADVISE_START
);
309 m_codeco
->WriteString(item
);
311 ret
= m_codeci
->Read8();
319 bool wxTCPConnection::StopAdvise (const wxString
& item
)
323 if (!m_sock
->IsConnected())
326 m_codeco
->Write8(IPC_ADVISE_STOP
);
327 m_codeco
->WriteString(item
);
329 msg
= m_codeci
->Read8();
337 // Calls that SERVER can make
338 bool wxTCPConnection::Advise (const wxString
& item
,
339 wxChar
*data
, int size
, wxIPCFormat format
)
341 if (!m_sock
->IsConnected())
344 m_codeco
->Write8(IPC_ADVISE
);
345 m_codeco
->WriteString(item
);
346 m_codeco
->Write8(format
);
349 size
= strlen(data
) + 1; // includes final NUL
351 m_codeco
->Write32(size
);
352 m_sockstrm
->Write(data
, size
);
357 void Client_OnRequest(wxSocketBase
& sock
, wxSocketNotify evt
,
361 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
362 wxDataInputStream
*codeci
;
363 wxDataOutputStream
*codeco
;
364 wxSocketStream
*sockstrm
;
365 wxString topic_name
= connection
->m_topic
;
368 // The socket handler signals us that we lost the connection: destroy all.
369 if (evt
== wxSOCKET_LOST
)
372 connection
->OnDisconnect();
376 // Receive message number.
377 codeci
= connection
->m_codeci
;
378 codeco
= connection
->m_codeco
;
379 sockstrm
= connection
->m_sockstrm
;
380 msg
= codeci
->Read8();
390 format
= (wxIPCFormat
)codeci
->Read8();
391 size
= codeci
->Read32();
392 data
= new char[size
];
393 sockstrm
->Read(data
, size
);
395 connection
->OnExecute (topic_name
, data
, size
, format
);
406 item
= codeci
->ReadString();
407 format
= (wxIPCFormat
)codeci
->Read8();
408 size
= codeci
->Read32();
409 data
= new char[size
];
410 sockstrm
->Read(data
, size
);
412 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
417 case IPC_ADVISE_START
:
419 item
= codeci
->ReadString();
421 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
423 codeco
->Write8(IPC_ADVISE_START
);
425 codeco
->Write8(IPC_FAIL
);
429 case IPC_ADVISE_STOP
:
431 item
= codeci
->ReadString();
433 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
435 codeco
->Write8(IPC_ADVISE_STOP
);
437 codeco
->Write8(IPC_FAIL
);
447 item
= codeci
->ReadString();
448 format
= (wxIPCFormat
)codeci
->Read8();
449 size
= codeci
->Read32();
450 data
= new wxChar
[size
];
451 sockstrm
->Read(data
, size
);
453 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
463 item
= codeci
->ReadString();
464 format
= (wxIPCFormat
)codeci
->Read8();
467 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
471 codeco
->Write8(IPC_REQUEST_REPLY
);
474 user_size
= strlen(user_data
) + 1; // includes final NUL
476 codeco
->Write32(user_size
);
477 sockstrm
->Write(user_data
, user_size
);
480 codeco
->Write8(IPC_FAIL
);
487 connection
->OnDisconnect();
491 codeco
->Write8(IPC_FAIL
);
496 void Server_OnRequest(wxSocketServer
& server
,
497 wxSocketNotify evt
, char *cdata
)
499 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
500 wxSocketStream
*stream
;
501 wxDataInputStream
*codeci
;
502 wxDataOutputStream
*codeco
;
504 if (evt
!= wxSOCKET_CONNECTION
)
507 /* Accept the connection, getting a new socket */
508 wxSocketBase
*sock
= server
.Accept();
512 stream
= new wxSocketStream(*sock
);
513 codeci
= new wxDataInputStream(*stream
);
514 codeco
= new wxDataOutputStream(*stream
);
517 msg
= codeci
->Read8();
519 if (msg
== IPC_CONNECT
)
522 topic_name
= codeci
->ReadString();
524 /* Register new socket with the notifier */
525 wxTCPConnection
*new_connection
=
526 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
529 if (!new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
531 delete new_connection
;
532 codeco
->Write8(IPC_FAIL
);
535 // Acknowledge success
536 codeco
->Write8(IPC_CONNECT
);
537 new_connection
->m_topic
= topic_name
;
538 new_connection
->m_sock
= sock
;
539 new_connection
->m_sockstrm
= stream
;
540 new_connection
->m_codeci
= codeci
;
541 new_connection
->m_codeco
= codeco
;
542 sock
->Callback(Client_OnRequest
);
543 sock
->CallbackData((char *)new_connection
);
544 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
549 // Send failure message
550 codeco
->Write8(IPC_FAIL
);