]>
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"
30 #if wxUSE_SOCKETS && wxUSE_IPC
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
)))
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
);
143 // and fall through to delete everything else
149 // Something went wrong, delete everything
158 wxConnectionBase
*wxTCPClient::OnMakeConnection()
160 return new wxTCPConnection
;
163 // ---------------------------------------------------------------------------
165 // ---------------------------------------------------------------------------
167 wxTCPServer::wxTCPServer ()
172 bool wxTCPServer::Create(const wxString
& server_name
)
174 wxSocketServer
*server
;
176 // wxIPV4address defaults to INADDR_ANY:0
178 addr
.Service(server_name
);
180 // Create a socket listening on specified port
181 server
= new wxSocketServer(addr
, SCKIPC_FLAGS
);
182 server
->Callback((wxSocketBase::wxSockCbk
)Server_OnRequest
);
183 server
->CallbackData((char *)this);
184 server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
185 server
->Notify(TRUE
);
190 wxTCPServer::~wxTCPServer()
194 wxConnectionBase
*wxTCPServer::OnAcceptConnection( const wxString
& WXUNUSED(topic
) )
196 return new wxTCPConnection();
199 // ---------------------------------------------------------------------------
201 // ---------------------------------------------------------------------------
203 wxTCPConnection::wxTCPConnection ()
204 : wxConnectionBase(),
205 m_sock(NULL
), m_sockstrm(NULL
), m_codeci(NULL
), m_codeco(NULL
)
209 wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer
), int WXUNUSED(size
))
213 wxTCPConnection::~wxTCPConnection ()
217 wxDELETE(m_sockstrm
);
219 if (m_sock
) m_sock
->Destroy();
222 void wxTCPConnection::Compress(bool WXUNUSED(on
))
227 // Calls that CLIENT can make.
228 bool wxTCPConnection::Disconnect ()
230 // Send the the disconnect message to the peer.
231 m_codeco
->Write8(IPC_DISCONNECT
);
232 m_sock
->Callback(NULL
);
238 bool wxTCPConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
240 if (!m_sock
->IsConnected())
243 // Prepare EXECUTE message
244 m_codeco
->Write8(IPC_EXECUTE
);
245 m_codeco
->Write8(format
);
248 size
= strlen(data
) + 1; // includes final NUL
250 m_codeco
->Write32(size
);
251 m_sockstrm
->Write(data
, size
);
256 char *wxTCPConnection::Request (const wxString
& item
, int *size
, wxIPCFormat format
)
258 if (!m_sock
->IsConnected())
261 m_codeco
->Write8(IPC_REQUEST
);
262 m_codeco
->WriteString(item
);
263 m_codeco
->Write8(format
);
265 // If Unpack doesn't initialize it.
268 ret
= m_codeci
->Read8();
276 s
= m_codeci
->Read32();
278 m_sockstrm
->Read(data
, s
);
286 bool wxTCPConnection::Poke (const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
288 if (!m_sock
->IsConnected())
291 m_codeco
->Write8(IPC_POKE
);
292 m_codeco
->WriteString(item
);
293 m_codeco
->Write8(format
);
296 size
= strlen(data
) + 1; // includes final NUL
298 m_codeco
->Write32(size
);
299 m_sockstrm
->Write(data
, size
);
304 bool wxTCPConnection::StartAdvise (const wxString
& item
)
308 if (!m_sock
->IsConnected())
311 m_codeco
->Write8(IPC_ADVISE_START
);
312 m_codeco
->WriteString(item
);
314 ret
= m_codeci
->Read8();
322 bool wxTCPConnection::StopAdvise (const wxString
& item
)
326 if (!m_sock
->IsConnected())
329 m_codeco
->Write8(IPC_ADVISE_STOP
);
330 m_codeco
->WriteString(item
);
332 msg
= m_codeci
->Read8();
340 // Calls that SERVER can make
341 bool wxTCPConnection::Advise (const wxString
& item
,
342 wxChar
*data
, int size
, wxIPCFormat format
)
344 if (!m_sock
->IsConnected())
347 m_codeco
->Write8(IPC_ADVISE
);
348 m_codeco
->WriteString(item
);
349 m_codeco
->Write8(format
);
352 size
= strlen(data
) + 1; // includes final NUL
354 m_codeco
->Write32(size
);
355 m_sockstrm
->Write(data
, size
);
360 void Client_OnRequest(wxSocketBase
& sock
,
365 wxTCPConnection
*connection
= (wxTCPConnection
*)cdata
;
366 wxDataInputStream
*codeci
;
367 wxDataOutputStream
*codeco
;
368 wxSocketStream
*sockstrm
;
369 wxString topic_name
= connection
->m_topic
;
372 // The socket handler signals us that we lost the connection: destroy all.
373 if (evt
== wxSOCKET_LOST
)
377 connection
->OnDisconnect();
381 // Receive message number.
382 codeci
= connection
->m_codeci
;
383 codeco
= connection
->m_codeco
;
384 sockstrm
= connection
->m_sockstrm
;
385 msg
= codeci
->Read8();
395 format
= (wxIPCFormat
)codeci
->Read8();
396 size
= codeci
->Read32();
397 data
= new char[size
];
398 sockstrm
->Read(data
, size
);
400 connection
->OnExecute (topic_name
, data
, size
, format
);
411 item
= codeci
->ReadString();
412 format
= (wxIPCFormat
)codeci
->Read8();
413 size
= codeci
->Read32();
414 data
= new char[size
];
415 sockstrm
->Read(data
, size
);
417 connection
->OnAdvise (topic_name
, item
, data
, size
, format
);
422 case IPC_ADVISE_START
:
424 item
= codeci
->ReadString();
426 bool ok
= connection
->OnStartAdvise (topic_name
, item
);
428 codeco
->Write8(IPC_ADVISE_START
);
430 codeco
->Write8(IPC_FAIL
);
434 case IPC_ADVISE_STOP
:
436 item
= codeci
->ReadString();
438 bool ok
= connection
->OnStopAdvise (topic_name
, item
);
440 codeco
->Write8(IPC_ADVISE_STOP
);
442 codeco
->Write8(IPC_FAIL
);
452 item
= codeci
->ReadString();
453 format
= (wxIPCFormat
)codeci
->Read8();
454 size
= codeci
->Read32();
455 data
= new wxChar
[size
];
456 sockstrm
->Read(data
, size
);
458 connection
->OnPoke (topic_name
, item
, data
, size
, format
);
468 item
= codeci
->ReadString();
469 format
= (wxIPCFormat
)codeci
->Read8();
472 char *user_data
= connection
->OnRequest (topic_name
, item
, &user_size
, format
);
476 codeco
->Write8(IPC_REQUEST_REPLY
);
479 user_size
= strlen(user_data
) + 1; // includes final NUL
481 codeco
->Write32(user_size
);
482 sockstrm
->Write(user_data
, user_size
);
485 codeco
->Write8(IPC_FAIL
);
491 wxLogDebug("IPC_DISCONNECT");
494 connection
->OnDisconnect();
498 codeco
->Write8(IPC_FAIL
);
503 void Server_OnRequest(wxSocketServer
& server
,
507 wxTCPServer
*ipcserv
= (wxTCPServer
*)cdata
;
508 wxSocketStream
*stream
;
509 wxDataInputStream
*codeci
;
510 wxDataOutputStream
*codeco
;
512 if (evt
!= wxSOCKET_CONNECTION
)
515 // Accept the connection, getting a new socket
516 wxSocketBase
*sock
= server
.Accept();
523 stream
= new wxSocketStream(*sock
);
524 codeci
= new wxDataInputStream(*stream
);
525 codeco
= new wxDataOutputStream(*stream
);
528 msg
= codeci
->Read8();
530 if (msg
== IPC_CONNECT
)
533 topic_name
= codeci
->ReadString();
535 wxTCPConnection
*new_connection
=
536 (wxTCPConnection
*)ipcserv
->OnAcceptConnection (topic_name
);
540 if (new_connection
->IsKindOf(CLASSINFO(wxTCPConnection
)))
542 // Acknowledge success
543 codeco
->Write8(IPC_CONNECT
);
544 new_connection
->m_topic
= topic_name
;
545 new_connection
->m_sock
= sock
;
546 new_connection
->m_sockstrm
= stream
;
547 new_connection
->m_codeci
= codeci
;
548 new_connection
->m_codeco
= codeco
;
549 sock
->Callback(Client_OnRequest
);
550 sock
->CallbackData((char *)new_connection
);
551 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
557 delete new_connection
;
558 // and fall through to delete everything else
563 // Something went wrong, send failure message and delete everything
564 codeco
->Write8(IPC_FAIL
);
573 // wxUSE_SOCKETS && wxUSE_IPC