]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckipc.cpp
menu title is drawn in bold font and the commands from it are ignored
[wxWidgets.git] / src / common / sckipc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sckipc.cpp
3 // Purpose: Interprocess communication implementation (wxSocket version)
4 // Author: Julian Smart, Guilhem Lavaux
5 // Modified by: Guilhem Lavaux (big rewrite) May 1997, 1998
6 // Created: 1993
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart 1993, Guilhem Lavaux 1997, 1998
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "sckipc.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28
29 #include "wx/socket.h"
30 #include "wx/sckipc.h"
31
32 #ifdef __BORLANDC__
33 #pragma hdrstop
34 #endif
35
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase)
38 IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase)
39 IMPLEMENT_DYNAMIC_CLASS(wxTCPConnection, wxConnectionBase)
40 #endif
41
42 // It seems to be already defined somewhere in the Xt includes.
43 #ifndef __XT__
44 // Message codes
45 enum {
46 IPC_EXECUTE = 1,
47 IPC_REQUEST,
48 IPC_POKE,
49 IPC_ADVISE_START,
50 IPC_ADVISE_REQUEST,
51 IPC_ADVISE,
52 IPC_ADVISE_STOP,
53 IPC_REQUEST_REPLY,
54 IPC_FAIL,
55 IPC_CONNECT,
56 IPC_DISCONNECT
57 };
58 #endif
59
60 void Server_OnRequest(wxSocketServer& server,
61 wxSocketBase::wxRequestEvent evt,
62 char *cdata);
63 void Client_OnRequest(wxSocketBase& sock,
64 wxSocketBase::wxRequestEvent evt,
65 char *cdata);
66
67 // ---------------------------------------------------------------------------
68 // wxTCPClient
69 // ---------------------------------------------------------------------------
70
71 wxTCPClient::wxTCPClient (void)
72 : wxClientBase()
73 {
74 }
75
76 wxTCPClient::~wxTCPClient (void)
77 {
78 }
79
80 bool wxTCPClient::ValidHost(const wxString& host)
81 {
82 wxIPV4address addr;
83
84 return addr.Hostname(host);
85 }
86
87 wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
88 const wxString& server_name,
89 const wxString& topic)
90 {
91 wxIPV4address addr;
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);
97
98 client->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
99 addr.Service(server_name);
100 addr.Hostname(host);
101
102 if (!client->Connect(addr)) {
103 delete client;
104 return NULL;
105 }
106 client->Notify(FALSE);
107
108 // Send topic name, and enquire whether this has succeeded
109 unsigned char msg;
110
111 data_os.Write8(IPC_CONNECT);
112 data_os.WriteString(topic);
113
114 msg = data_is.Read8();
115
116 // OK! Confirmation.
117 if (msg == IPC_CONNECT) {
118 wxTCPConnection *connection = (wxTCPConnection *)OnMakeConnection ();
119 if (connection) {
120 if (!connection->IsKindOf(CLASSINFO(wxTCPConnection))) {
121 delete connection;
122 return NULL;
123 }
124 connection->m_topic = topic;
125 client->Callback(Client_OnRequest);
126 client->CallbackData((char *)connection);
127 client->Notify(TRUE);
128 return connection;
129 } else {
130 delete client;
131 return NULL;
132 }
133 } else {
134 delete client;
135 return NULL;
136 }
137 return NULL;
138 }
139
140 wxConnectionBase *wxTCPClient::OnMakeConnection()
141 {
142 return new wxTCPConnection;
143 }
144
145 // ---------------------------------------------------------------------------
146 // wxTCPServer
147 // ---------------------------------------------------------------------------
148
149 wxTCPServer::wxTCPServer (void)
150 : wxServerBase()
151 {
152 }
153
154 bool wxTCPServer::Create(const wxString& server_name)
155 {
156 wxIPV4address addr;
157 wxSocketHandler *hsock = &wxSocketHandler::Master();
158 wxSocketServer *server;
159
160 addr.Service(server_name);
161
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);
166
167 server->CallbackData((char *)this);
168
169 return TRUE;
170 }
171
172 wxTCPServer::~wxTCPServer(void)
173 {
174 }
175
176 wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) )
177 {
178 return new wxTCPConnection();
179 }
180
181 // ---------------------------------------------------------------------------
182 // wxTCPConnection
183 // ---------------------------------------------------------------------------
184
185 wxTCPConnection::wxTCPConnection (void)
186 : wxConnectionBase(),
187 m_sock(NULL), m_sockstrm(NULL), m_codeci(NULL), m_codeco(NULL)
188 {
189 }
190
191 wxTCPConnection::wxTCPConnection(char *buffer, int size)
192 {
193 }
194
195 wxTCPConnection::~wxTCPConnection (void)
196 {
197 wxDELETE(m_sock);
198 wxDELETE(m_codeci);
199 wxDELETE(m_codeco);
200 wxDELETE(m_sockstrm);
201 }
202
203 void wxTCPConnection::Compress(bool WXUNUSED(on))
204 {
205 // Use wxLZWStream
206 }
207
208 // Calls that CLIENT can make.
209 bool wxTCPConnection::Disconnect (void)
210 {
211 // Send the the disconnect message to the peer.
212 m_codeco->Write8(IPC_DISCONNECT);
213 m_sock->Close();
214
215 return TRUE;
216 }
217
218 bool wxTCPConnection::Execute (char *data, int size, wxIPCFormat format)
219 {
220 if (!m_sock->IsConnected())
221 return FALSE;
222
223 // Prepare EXECUTE message
224 m_codeco->Write8(IPC_EXECUTE);
225 m_codeco->Write8(format);
226 if (size < 0)
227 m_codeco->WriteString(data);
228 else {
229 m_codeco->Write32(size);
230 m_codeco->Write(data, size);
231 }
232
233 return TRUE;
234 }
235
236 char *wxTCPConnection::Request (const wxString& item, int *size, wxIPCFormat format)
237 {
238 if (!m_sock->IsConnected())
239 return NULL;
240
241 m_codeco->Write8(IPC_REQUEST);
242 m_codeco->WriteString(item);
243 m_codeco->Write8(format);
244
245 // If Unpack doesn't initialize it.
246 int ret;
247
248 ret = m_codeci->Read8();
249 if (ret == IPC_FAIL)
250 return NULL;
251 else {
252 size_t s;
253 char *data = NULL;
254
255 s = m_codeci->Read32();
256 data = new char[s];
257 m_codeci->Read(data, s);
258
259 if (size)
260 *size = s;
261 return data;
262 }
263 }
264
265 bool wxTCPConnection::Poke (const wxString& item, char *data, int size, wxIPCFormat format)
266 {
267 if (!m_sock->IsConnected())
268 return FALSE;
269
270 m_codeco->Write8(IPC_POKE);
271 m_codeco->WriteString(item);
272 m_codeco->Write8(format);
273 if (size < 0)
274 m_codeco->WriteString(data);
275 else {
276 m_codeco->Write32(size);
277 m_codeco->Write(data, size);
278 }
279
280 return TRUE;
281 }
282
283 bool wxTCPConnection::StartAdvise (const wxString& item)
284 {
285 int ret;
286
287 if (!m_sock->IsConnected())
288 return FALSE;
289
290 m_codeco->Write8(IPC_ADVISE_START);
291 m_codeco->WriteString(item);
292
293 ret = m_codeci->Read8();
294
295 if (ret != IPC_FAIL)
296 return TRUE;
297 else
298 return FALSE;
299 }
300
301 bool wxTCPConnection::StopAdvise (const wxString& item)
302 {
303 int msg;
304
305 if (!m_sock->IsConnected())
306 return FALSE;
307
308 m_codeco->Write8(IPC_ADVISE_STOP);
309 m_codeco->WriteString(item);
310
311 msg = m_codeci->Read8();
312
313 if (msg != IPC_FAIL)
314 return TRUE;
315 else
316 return FALSE;
317 }
318
319 // Calls that SERVER can make
320 bool wxTCPConnection::Advise (const wxString& item,
321 char *data, int size, wxIPCFormat format)
322 {
323 if (!m_sock->IsConnected())
324 return FALSE;
325
326 m_codeco->Write8(IPC_ADVISE);
327 m_codeco->WriteString(item);
328 m_codeco->Write8(format);
329 if (size < 0)
330 m_codeco->WriteString(data);
331 else {
332 m_codeco->Write32(size);
333 m_codeco->Write(data, size);
334 }
335
336 return TRUE;
337 }
338
339 void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
340 char *cdata)
341 {
342 int msg = 0;
343 wxTCPConnection *connection = (wxTCPConnection *)cdata;
344 wxDataInputStream *codeci;
345 wxDataOutputStream *codeco;
346 wxString topic_name = connection->m_topic;
347 wxString item;
348
349 // The socket handler signals us that we lost the connection: destroy all.
350 if (evt == wxSocketBase::EVT_LOST) {
351 sock.Close();
352 connection->OnDisconnect();
353 return;
354 }
355
356 // Receive message number.
357 codeci = connection->m_codeci;
358 codeco = connection->m_codeco;
359 msg = codeci->Read8();
360
361 switch (msg) {
362 case IPC_EXECUTE: {
363 char *data;
364 size_t size;
365 wxIPCFormat format;
366
367 format = (wxIPCFormat)codeci->Read8();
368 size = codeci->Read32();
369 data = new char[size];
370 codeci->Read(data, size);
371
372 connection->OnExecute (topic_name, data, size, format);
373
374 delete [] data;
375 break;
376 }
377 case IPC_ADVISE: {
378 char *data;
379 size_t size;
380 wxIPCFormat format;
381
382 item = codeci->ReadString();
383 format = (wxIPCFormat)codeci->Read8();
384 size = codeci->Read32();
385 data = new char[size];
386 codeci->Read(data, size);
387
388 connection->OnAdvise (topic_name, item, data, size, format);
389
390 delete [] data;
391 break;
392 }
393 case IPC_ADVISE_START: {
394 item = codeci->ReadString();
395
396 bool ok = connection->OnStartAdvise (topic_name, item);
397 if (ok)
398 codeco->Write8(IPC_ADVISE_START);
399 else
400 codeco->Write8(IPC_FAIL);
401
402 break;
403 }
404 case IPC_ADVISE_STOP: {
405 item = codeci->ReadString();
406
407 bool ok = connection->OnStopAdvise (topic_name, item);
408 if (ok)
409 codeco->Write8(IPC_ADVISE_STOP);
410 else
411 codeco->Write8(IPC_FAIL);
412
413 break;
414 }
415 case IPC_POKE: {
416 wxIPCFormat format;
417 size_t size;
418 char *data;
419
420 item = codeci->ReadString();
421 format = (wxIPCFormat)codeci->Read8();
422 size = codeci->Read32();
423 data = new char[size];
424 codeci->Read(data, size);
425
426 connection->OnPoke (topic_name, item, data, size, format);
427
428 delete [] data;
429
430 break;
431 }
432 case IPC_REQUEST: {
433 wxIPCFormat format;
434
435 item = codeci->ReadString();
436 format = (wxIPCFormat)codeci->Read8();
437
438 int user_size = -1;
439 char *user_data = connection->OnRequest (topic_name, item, &user_size, format);
440
441 if (user_data) {
442 codeco->Write8(IPC_REQUEST_REPLY);
443 if (user_size != -1) {
444 codeco->Write32(user_size);
445 codeco->Write(user_data, user_size);
446 } else
447 codeco->WriteString(user_data);
448 } else
449 codeco->Write8(IPC_FAIL);
450
451 break;
452 }
453 case IPC_DISCONNECT: {
454 sock.Close();
455 connection->OnDisconnect();
456 break;
457 }
458 default:
459 codeco->Write8(IPC_FAIL);
460 break;
461 }
462 }
463
464 void Server_OnRequest(wxSocketServer& server,
465 wxSocketBase::wxRequestEvent evt, char *cdata)
466 {
467 wxTCPServer *ipcserv = (wxTCPServer *)cdata;
468 wxSocketStream *stream;
469 wxDataInputStream *codeci;
470 wxDataOutputStream *codeco;
471
472 if (evt != wxSocketBase::EVT_ACCEPT)
473 return;
474
475 /* Accept the connection, getting a new socket */
476 wxSocketBase *sock = server.Accept();
477 sock->Notify(FALSE);
478 sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
479
480 stream = new wxSocketStream(*sock);
481 codeci = new wxDataInputStream(*stream);
482 codeco = new wxDataOutputStream(*stream);
483
484 if (!sock->Ok())
485 return;
486
487 int msg;
488 msg = codeci->Read8();
489
490 if (msg == IPC_CONNECT) {
491 wxString topic_name;
492 topic_name = codeci->ReadString();
493
494 /* Register new socket with the notifier */
495 wxTCPConnection *new_connection =
496 (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name);
497 if (new_connection) {
498 if (!new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) {
499 delete new_connection;
500 codeco->Write8(IPC_FAIL);
501 return;
502 }
503 // Acknowledge success
504 codeco->Write8(IPC_CONNECT);
505
506 new_connection->m_topic = topic_name;
507 new_connection->m_sockstrm = stream;
508 new_connection->m_codeci = codeci;
509 new_connection->m_codeco = codeco;
510 sock->Callback(Client_OnRequest);
511 sock->CallbackData((char *)new_connection);
512 sock->Notify(TRUE);
513 } else {
514 // Send failure message
515 codeco->Write8(IPC_FAIL);
516 }
517 }
518 }