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