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