]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/sckipc.cpp | |
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 | |
7 | // (callbacks deprecated) Mar 2000 | |
8 | // Vadim Zeitlin (added support for Unix sockets) Apr 2002 | |
9 | // (use buffering, many fixes/cleanup) Oct 2008 | |
10 | // Created: 1993 | |
11 | // RCS-ID: $Id$ | |
12 | // Copyright: (c) Julian Smart 1993 | |
13 | // (c) Guilhem Lavaux 1997, 1998 | |
14 | // (c) 2000 Guillermo Rodriguez <guille@iies.es> | |
15 | // Licence: wxWindows licence | |
16 | ///////////////////////////////////////////////////////////////////////////// | |
17 | ||
18 | // ========================================================================== | |
19 | // declarations | |
20 | // ========================================================================== | |
21 | ||
22 | // -------------------------------------------------------------------------- | |
23 | // headers | |
24 | // -------------------------------------------------------------------------- | |
25 | ||
26 | // For compilers that support precompilation, includes "wx.h". | |
27 | #include "wx/wxprec.h" | |
28 | ||
29 | #ifdef __BORLANDC__ | |
30 | #pragma hdrstop | |
31 | #endif | |
32 | ||
33 | #if wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS | |
34 | ||
35 | #include "wx/sckipc.h" | |
36 | ||
37 | #ifndef WX_PRECOMP | |
38 | #include "wx/log.h" | |
39 | #include "wx/event.h" | |
40 | #include "wx/module.h" | |
41 | #endif | |
42 | ||
43 | #include <stdlib.h> | |
44 | #include <stdio.h> | |
45 | #include <errno.h> | |
46 | ||
47 | #include "wx/socket.h" | |
48 | ||
49 | // -------------------------------------------------------------------------- | |
50 | // macros and constants | |
51 | // -------------------------------------------------------------------------- | |
52 | ||
53 | namespace | |
54 | { | |
55 | ||
56 | // Message codes (don't change them to avoid breaking the existing code using | |
57 | // wxIPC protocol!) | |
58 | enum IPCCode | |
59 | { | |
60 | IPC_EXECUTE = 1, | |
61 | IPC_REQUEST = 2, | |
62 | IPC_POKE = 3, | |
63 | IPC_ADVISE_START = 4, | |
64 | IPC_ADVISE_REQUEST = 5, | |
65 | IPC_ADVISE = 6, | |
66 | IPC_ADVISE_STOP = 7, | |
67 | IPC_REQUEST_REPLY = 8, | |
68 | IPC_FAIL = 9, | |
69 | IPC_CONNECT = 10, | |
70 | IPC_DISCONNECT = 11, | |
71 | IPC_MAX | |
72 | }; | |
73 | ||
74 | } // anonymous namespace | |
75 | ||
76 | // headers needed for umask() | |
77 | #ifdef __UNIX_LIKE__ | |
78 | #include <sys/types.h> | |
79 | #include <sys/stat.h> | |
80 | #endif // __UNIX_LIKE__ | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // private functions | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | // get the address object for the given server name, the caller must delete it | |
87 | static wxSockAddress * | |
88 | GetAddressFromName(const wxString& serverName, | |
89 | const wxString& host = wxEmptyString) | |
90 | { | |
91 | // we always use INET sockets under non-Unix systems | |
92 | #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) | |
93 | // under Unix, if the server name looks like a path, create a AF_UNIX | |
94 | // socket instead of AF_INET one | |
95 | if ( serverName.Find(_T('/')) != wxNOT_FOUND ) | |
96 | { | |
97 | wxUNIXaddress *addr = new wxUNIXaddress; | |
98 | addr->Filename(serverName); | |
99 | ||
100 | return addr; | |
101 | } | |
102 | #endif // Unix/!Unix | |
103 | { | |
104 | wxIPV4address *addr = new wxIPV4address; | |
105 | addr->Service(serverName); | |
106 | if ( !host.empty() ) | |
107 | { | |
108 | addr->Hostname(host); | |
109 | } | |
110 | ||
111 | return addr; | |
112 | } | |
113 | } | |
114 | ||
115 | // -------------------------------------------------------------------------- | |
116 | // wxTCPEventHandler stuff (private class) | |
117 | // -------------------------------------------------------------------------- | |
118 | ||
119 | class wxTCPEventHandler : public wxEvtHandler | |
120 | { | |
121 | public: | |
122 | wxTCPEventHandler() : wxEvtHandler() { } | |
123 | ||
124 | void Client_OnRequest(wxSocketEvent& event); | |
125 | void Server_OnRequest(wxSocketEvent& event); | |
126 | ||
127 | private: | |
128 | void HandleDisconnect(wxTCPConnection *connection); | |
129 | ||
130 | DECLARE_EVENT_TABLE() | |
131 | wxDECLARE_NO_COPY_CLASS(wxTCPEventHandler); | |
132 | }; | |
133 | ||
134 | enum | |
135 | { | |
136 | _CLIENT_ONREQUEST_ID = 1000, | |
137 | _SERVER_ONREQUEST_ID | |
138 | }; | |
139 | ||
140 | // -------------------------------------------------------------------------- | |
141 | // wxTCPEventHandlerModule (private class) | |
142 | // -------------------------------------------------------------------------- | |
143 | ||
144 | class wxTCPEventHandlerModule : public wxModule | |
145 | { | |
146 | public: | |
147 | wxTCPEventHandlerModule() : wxModule() { } | |
148 | ||
149 | // get the global wxTCPEventHandler creating it if necessary | |
150 | static wxTCPEventHandler& GetHandler() | |
151 | { | |
152 | if ( !ms_handler ) | |
153 | ms_handler = new wxTCPEventHandler; | |
154 | ||
155 | return *ms_handler; | |
156 | } | |
157 | ||
158 | // as ms_handler is initialized on demand, don't do anything in OnInit() | |
159 | virtual bool OnInit() { return true; } | |
160 | virtual void OnExit() { wxDELETE(ms_handler); } | |
161 | ||
162 | private: | |
163 | static wxTCPEventHandler *ms_handler; | |
164 | ||
165 | DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule) | |
166 | wxDECLARE_NO_COPY_CLASS(wxTCPEventHandlerModule); | |
167 | }; | |
168 | ||
169 | IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule, wxModule) | |
170 | ||
171 | wxTCPEventHandler *wxTCPEventHandlerModule::ms_handler = NULL; | |
172 | ||
173 | // -------------------------------------------------------------------------- | |
174 | // wxIPCSocketStreams | |
175 | // -------------------------------------------------------------------------- | |
176 | ||
177 | #define USE_BUFFER | |
178 | ||
179 | // this class contains the various (related) streams used by wxTCPConnection | |
180 | // and also provides a way to read from the socket stream directly | |
181 | // | |
182 | // for writing to the stream use the IPCOutput class below | |
183 | class wxIPCSocketStreams | |
184 | { | |
185 | public: | |
186 | // ctor initializes all the streams on top of the given socket | |
187 | // | |
188 | // note that we use a bigger than default buffer size which matches the | |
189 | // typical Ethernet MTU (minus TCP header overhead) | |
190 | wxIPCSocketStreams(wxSocketBase& sock) | |
191 | : m_socketStream(sock), | |
192 | #ifdef USE_BUFFER | |
193 | m_bufferedOut(m_socketStream, 1448), | |
194 | #else | |
195 | m_bufferedOut(m_socketStream), | |
196 | #endif | |
197 | m_dataIn(m_socketStream), | |
198 | m_dataOut(m_bufferedOut) | |
199 | { | |
200 | } | |
201 | ||
202 | // expose the IO methods needed by IPC code (notice that writing is only | |
203 | // done via IPCOutput) | |
204 | ||
205 | // flush output | |
206 | void Flush() | |
207 | { | |
208 | #ifdef USE_BUFFER | |
209 | m_bufferedOut.Sync(); | |
210 | #endif | |
211 | } | |
212 | ||
213 | // simple wrappers around the functions with the same name in | |
214 | // wxDataInputStream | |
215 | wxUint8 Read8() | |
216 | { | |
217 | Flush(); | |
218 | return m_dataIn.Read8(); | |
219 | } | |
220 | ||
221 | wxUint32 Read32() | |
222 | { | |
223 | Flush(); | |
224 | return m_dataIn.Read32(); | |
225 | } | |
226 | ||
227 | wxString ReadString() | |
228 | { | |
229 | Flush(); | |
230 | return m_dataIn.ReadString(); | |
231 | } | |
232 | ||
233 | // read arbitrary (size-prepended) data | |
234 | // | |
235 | // connection parameter is needed to call its GetBufferAtLeast() method | |
236 | void *ReadData(wxConnectionBase *conn, size_t *size) | |
237 | { | |
238 | Flush(); | |
239 | ||
240 | wxCHECK_MSG( conn, NULL, "NULL connection parameter" ); | |
241 | wxCHECK_MSG( size, NULL, "NULL size parameter" ); | |
242 | ||
243 | *size = Read32(); | |
244 | ||
245 | void * const data = conn->GetBufferAtLeast(*size); | |
246 | wxCHECK_MSG( data, NULL, "IPC buffer allocation failed" ); | |
247 | ||
248 | m_socketStream.Read(data, *size); | |
249 | ||
250 | return data; | |
251 | } | |
252 | ||
253 | // same as above but for data preceded by the format | |
254 | void * | |
255 | ReadFormatData(wxConnectionBase *conn, wxIPCFormat *format, size_t *size) | |
256 | { | |
257 | wxCHECK_MSG( format, NULL, "NULL format parameter" ); | |
258 | ||
259 | *format = static_cast<wxIPCFormat>(Read8()); | |
260 | ||
261 | return ReadData(conn, size); | |
262 | } | |
263 | ||
264 | ||
265 | // these methods are only used by IPCOutput and not directly | |
266 | wxDataOutputStream& GetDataOut() { return m_dataOut; } | |
267 | wxOutputStream& GetUnformattedOut() { return m_bufferedOut; } | |
268 | ||
269 | private: | |
270 | // this is the low-level underlying stream using the connection socket | |
271 | wxSocketStream m_socketStream; | |
272 | ||
273 | // the buffered stream is used to avoid writing all pieces of an IPC | |
274 | // request to the socket one by one but to instead do it all at once when | |
275 | // we're done with it | |
276 | #ifdef USE_BUFFER | |
277 | wxBufferedOutputStream m_bufferedOut; | |
278 | #else | |
279 | wxOutputStream& m_bufferedOut; | |
280 | #endif | |
281 | ||
282 | // finally the data streams are used to be able to write typed data into | |
283 | // the above streams easily | |
284 | wxDataInputStream m_dataIn; | |
285 | wxDataOutputStream m_dataOut; | |
286 | ||
287 | wxDECLARE_NO_COPY_CLASS(wxIPCSocketStreams); | |
288 | }; | |
289 | ||
290 | namespace | |
291 | { | |
292 | ||
293 | // an object of this class should be instantiated on the stack to write to the | |
294 | // underlying socket stream | |
295 | // | |
296 | // this class is intentionally separated from wxIPCSocketStreams to ensure that | |
297 | // Flush() is always called | |
298 | class IPCOutput | |
299 | { | |
300 | public: | |
301 | // construct an object associated with the given streams (which must have | |
302 | // life time greater than ours as we keep a reference to it) | |
303 | IPCOutput(wxIPCSocketStreams *streams) | |
304 | : m_streams(*streams) | |
305 | { | |
306 | wxASSERT_MSG( streams, "NULL streams pointer" ); | |
307 | } | |
308 | ||
309 | // dtor calls Flush() really sending the IPC data to the network | |
310 | ~IPCOutput() { m_streams.Flush(); } | |
311 | ||
312 | ||
313 | // write a byte | |
314 | void Write8(wxUint8 i) | |
315 | { | |
316 | m_streams.GetDataOut().Write8(i); | |
317 | } | |
318 | ||
319 | // write the reply code and a string | |
320 | void Write(IPCCode code, const wxString& str) | |
321 | { | |
322 | Write8(code); | |
323 | m_streams.GetDataOut().WriteString(str); | |
324 | } | |
325 | ||
326 | // write the reply code, a string and a format in this order | |
327 | void Write(IPCCode code, const wxString& str, wxIPCFormat format) | |
328 | { | |
329 | Write(code, str); | |
330 | Write8(format); | |
331 | } | |
332 | ||
333 | // write arbitrary data | |
334 | void WriteData(const void *data, size_t size) | |
335 | { | |
336 | m_streams.GetDataOut().Write32(size); | |
337 | m_streams.GetUnformattedOut().Write(data, size); | |
338 | } | |
339 | ||
340 | ||
341 | private: | |
342 | wxIPCSocketStreams& m_streams; | |
343 | ||
344 | wxDECLARE_NO_COPY_CLASS(IPCOutput); | |
345 | }; | |
346 | ||
347 | } // anonymous namespace | |
348 | ||
349 | // ========================================================================== | |
350 | // implementation | |
351 | // ========================================================================== | |
352 | ||
353 | IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase) | |
354 | IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase) | |
355 | IMPLEMENT_CLASS(wxTCPConnection, wxConnectionBase) | |
356 | ||
357 | // -------------------------------------------------------------------------- | |
358 | // wxTCPClient | |
359 | // -------------------------------------------------------------------------- | |
360 | ||
361 | wxTCPClient::wxTCPClient() | |
362 | : wxClientBase() | |
363 | { | |
364 | } | |
365 | ||
366 | bool wxTCPClient::ValidHost(const wxString& host) | |
367 | { | |
368 | wxIPV4address addr; | |
369 | ||
370 | return addr.Hostname(host); | |
371 | } | |
372 | ||
373 | wxConnectionBase *wxTCPClient::MakeConnection(const wxString& host, | |
374 | const wxString& serverName, | |
375 | const wxString& topic) | |
376 | { | |
377 | wxSockAddress *addr = GetAddressFromName(serverName, host); | |
378 | if ( !addr ) | |
379 | return NULL; | |
380 | ||
381 | wxSocketClient * const client = new wxSocketClient(wxSOCKET_WAITALL); | |
382 | wxIPCSocketStreams * const streams = new wxIPCSocketStreams(*client); | |
383 | ||
384 | bool ok = client->Connect(*addr); | |
385 | delete addr; | |
386 | ||
387 | if ( ok ) | |
388 | { | |
389 | // Send topic name, and enquire whether this has succeeded | |
390 | IPCOutput(streams).Write(IPC_CONNECT, topic); | |
391 | ||
392 | unsigned char msg = streams->Read8(); | |
393 | ||
394 | // OK! Confirmation. | |
395 | if (msg == IPC_CONNECT) | |
396 | { | |
397 | wxTCPConnection * | |
398 | connection = (wxTCPConnection *)OnMakeConnection (); | |
399 | ||
400 | if (connection) | |
401 | { | |
402 | if (connection->IsKindOf(CLASSINFO(wxTCPConnection))) | |
403 | { | |
404 | connection->m_topic = topic; | |
405 | connection->m_sock = client; | |
406 | connection->m_streams = streams; | |
407 | client->SetEventHandler(wxTCPEventHandlerModule::GetHandler(), | |
408 | _CLIENT_ONREQUEST_ID); | |
409 | client->SetClientData(connection); | |
410 | client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); | |
411 | client->Notify(true); | |
412 | return connection; | |
413 | } | |
414 | else | |
415 | { | |
416 | delete connection; | |
417 | // and fall through to delete everything else | |
418 | } | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
423 | // Something went wrong, delete everything | |
424 | delete streams; | |
425 | client->Destroy(); | |
426 | ||
427 | return NULL; | |
428 | } | |
429 | ||
430 | wxConnectionBase *wxTCPClient::OnMakeConnection() | |
431 | { | |
432 | return new wxTCPConnection(); | |
433 | } | |
434 | ||
435 | // -------------------------------------------------------------------------- | |
436 | // wxTCPServer | |
437 | // -------------------------------------------------------------------------- | |
438 | ||
439 | wxTCPServer::wxTCPServer() | |
440 | : wxServerBase() | |
441 | { | |
442 | m_server = NULL; | |
443 | } | |
444 | ||
445 | bool wxTCPServer::Create(const wxString& serverName) | |
446 | { | |
447 | // Destroy previous server, if any | |
448 | if (m_server) | |
449 | { | |
450 | m_server->SetClientData(NULL); | |
451 | m_server->Destroy(); | |
452 | m_server = NULL; | |
453 | } | |
454 | ||
455 | wxSockAddress *addr = GetAddressFromName(serverName); | |
456 | if ( !addr ) | |
457 | return false; | |
458 | ||
459 | #ifdef __UNIX_LIKE__ | |
460 | mode_t umaskOld; | |
461 | if ( addr->Type() == wxSockAddress::UNIX ) | |
462 | { | |
463 | // ensure that the file doesn't exist as otherwise calling socket() | |
464 | // would fail | |
465 | int rc = remove(serverName.fn_str()); | |
466 | if ( rc < 0 && errno != ENOENT ) | |
467 | { | |
468 | delete addr; | |
469 | ||
470 | return false; | |
471 | } | |
472 | ||
473 | // also set the umask to prevent the others from reading our file | |
474 | umaskOld = umask(077); | |
475 | } | |
476 | else | |
477 | { | |
478 | // unused anyhow but shut down the compiler warnings | |
479 | umaskOld = 0; | |
480 | } | |
481 | #endif // __UNIX_LIKE__ | |
482 | ||
483 | // Create a socket listening on the specified port (reusing it to allow | |
484 | // restarting the server listening on the same port as was used by the | |
485 | // previous instance of this server) | |
486 | m_server = new wxSocketServer(*addr, wxSOCKET_WAITALL | wxSOCKET_REUSEADDR); | |
487 | ||
488 | #ifdef __UNIX_LIKE__ | |
489 | if ( addr->Type() == wxSockAddress::UNIX ) | |
490 | { | |
491 | // restore the umask | |
492 | umask(umaskOld); | |
493 | ||
494 | // save the file name to remove it later | |
495 | m_filename = serverName; | |
496 | } | |
497 | #endif // __UNIX_LIKE__ | |
498 | ||
499 | delete addr; | |
500 | ||
501 | if (!m_server->Ok()) | |
502 | { | |
503 | m_server->Destroy(); | |
504 | m_server = NULL; | |
505 | ||
506 | return false; | |
507 | } | |
508 | ||
509 | m_server->SetEventHandler(wxTCPEventHandlerModule::GetHandler(), | |
510 | _SERVER_ONREQUEST_ID); | |
511 | m_server->SetClientData(this); | |
512 | m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); | |
513 | m_server->Notify(true); | |
514 | ||
515 | return true; | |
516 | } | |
517 | ||
518 | wxTCPServer::~wxTCPServer() | |
519 | { | |
520 | if ( m_server ) | |
521 | { | |
522 | m_server->SetClientData(NULL); | |
523 | m_server->Destroy(); | |
524 | } | |
525 | ||
526 | #ifdef __UNIX_LIKE__ | |
527 | if ( !m_filename.empty() ) | |
528 | { | |
529 | if ( remove(m_filename.fn_str()) != 0 ) | |
530 | { | |
531 | wxLogDebug(_T("Stale AF_UNIX file '%s' left."), m_filename.c_str()); | |
532 | } | |
533 | } | |
534 | #endif // __UNIX_LIKE__ | |
535 | } | |
536 | ||
537 | wxConnectionBase * | |
538 | wxTCPServer::OnAcceptConnection(const wxString& WXUNUSED(topic)) | |
539 | { | |
540 | return new wxTCPConnection(); | |
541 | } | |
542 | ||
543 | // -------------------------------------------------------------------------- | |
544 | // wxTCPConnection | |
545 | // -------------------------------------------------------------------------- | |
546 | ||
547 | void wxTCPConnection::Init() | |
548 | { | |
549 | m_sock = NULL; | |
550 | m_streams = NULL; | |
551 | } | |
552 | ||
553 | wxTCPConnection::~wxTCPConnection() | |
554 | { | |
555 | Disconnect(); | |
556 | ||
557 | if ( m_sock ) | |
558 | { | |
559 | m_sock->SetClientData(NULL); | |
560 | m_sock->Destroy(); | |
561 | } | |
562 | ||
563 | delete m_streams; | |
564 | } | |
565 | ||
566 | void wxTCPConnection::Compress(bool WXUNUSED(on)) | |
567 | { | |
568 | // TODO | |
569 | } | |
570 | ||
571 | // Calls that CLIENT can make. | |
572 | bool wxTCPConnection::Disconnect() | |
573 | { | |
574 | if ( !GetConnected() ) | |
575 | return true; | |
576 | ||
577 | // Send the disconnect message to the peer. | |
578 | IPCOutput(m_streams).Write8(IPC_DISCONNECT); | |
579 | ||
580 | if ( m_sock ) | |
581 | { | |
582 | m_sock->Notify(false); | |
583 | m_sock->Close(); | |
584 | } | |
585 | ||
586 | SetConnected(false); | |
587 | ||
588 | return true; | |
589 | } | |
590 | ||
591 | bool wxTCPConnection::DoExecute(const void *data, | |
592 | size_t size, | |
593 | wxIPCFormat format) | |
594 | { | |
595 | if ( !m_sock->IsConnected() ) | |
596 | return false; | |
597 | ||
598 | // Prepare EXECUTE message | |
599 | IPCOutput out(m_streams); | |
600 | out.Write8(IPC_EXECUTE); | |
601 | out.Write8(format); | |
602 | ||
603 | out.WriteData(data, size); | |
604 | ||
605 | return true; | |
606 | } | |
607 | ||
608 | const void *wxTCPConnection::Request(const wxString& item, | |
609 | size_t *size, | |
610 | wxIPCFormat format) | |
611 | { | |
612 | if ( !m_sock->IsConnected() ) | |
613 | return NULL; | |
614 | ||
615 | IPCOutput(m_streams).Write(IPC_REQUEST, item, format); | |
616 | ||
617 | const int ret = m_streams->Read8(); | |
618 | if ( ret != IPC_REQUEST_REPLY ) | |
619 | return NULL; | |
620 | ||
621 | // ReadData() needs a non-NULL size pointer but the client code can call us | |
622 | // with NULL pointer (this makes sense if it knows that it always works | |
623 | // with NUL-terminated strings) | |
624 | size_t sizeFallback; | |
625 | return m_streams->ReadData(this, size ? size : &sizeFallback); | |
626 | } | |
627 | ||
628 | bool wxTCPConnection::DoPoke(const wxString& item, | |
629 | const void *data, | |
630 | size_t size, | |
631 | wxIPCFormat format) | |
632 | { | |
633 | if ( !m_sock->IsConnected() ) | |
634 | return false; | |
635 | ||
636 | IPCOutput out(m_streams); | |
637 | out.Write(IPC_POKE, item, format); | |
638 | out.WriteData(data, size); | |
639 | ||
640 | return true; | |
641 | } | |
642 | ||
643 | bool wxTCPConnection::StartAdvise(const wxString& item) | |
644 | { | |
645 | if ( !m_sock->IsConnected() ) | |
646 | return false; | |
647 | ||
648 | IPCOutput(m_streams).Write(IPC_ADVISE_START, item); | |
649 | ||
650 | const int ret = m_streams->Read8(); | |
651 | ||
652 | return ret == IPC_ADVISE_START; | |
653 | } | |
654 | ||
655 | bool wxTCPConnection::StopAdvise (const wxString& item) | |
656 | { | |
657 | if ( !m_sock->IsConnected() ) | |
658 | return false; | |
659 | ||
660 | IPCOutput(m_streams).Write(IPC_ADVISE_STOP, item); | |
661 | ||
662 | const int ret = m_streams->Read8(); | |
663 | ||
664 | return ret == IPC_ADVISE_STOP; | |
665 | } | |
666 | ||
667 | // Calls that SERVER can make | |
668 | bool wxTCPConnection::DoAdvise(const wxString& item, | |
669 | const void *data, | |
670 | size_t size, | |
671 | wxIPCFormat format) | |
672 | { | |
673 | if ( !m_sock->IsConnected() ) | |
674 | return false; | |
675 | ||
676 | IPCOutput out(m_streams); | |
677 | out.Write(IPC_ADVISE, item, format); | |
678 | out.WriteData(data, size); | |
679 | ||
680 | return true; | |
681 | } | |
682 | ||
683 | // -------------------------------------------------------------------------- | |
684 | // wxTCPEventHandler (private class) | |
685 | // -------------------------------------------------------------------------- | |
686 | ||
687 | BEGIN_EVENT_TABLE(wxTCPEventHandler, wxEvtHandler) | |
688 | EVT_SOCKET(_CLIENT_ONREQUEST_ID, wxTCPEventHandler::Client_OnRequest) | |
689 | EVT_SOCKET(_SERVER_ONREQUEST_ID, wxTCPEventHandler::Server_OnRequest) | |
690 | END_EVENT_TABLE() | |
691 | ||
692 | void wxTCPEventHandler::HandleDisconnect(wxTCPConnection *connection) | |
693 | { | |
694 | // connection was closed (either gracefully or not): destroy everything | |
695 | connection->m_sock->Notify(false); | |
696 | connection->m_sock->Close(); | |
697 | ||
698 | connection->SetConnected(false); | |
699 | connection->OnDisconnect(); | |
700 | } | |
701 | ||
702 | void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event) | |
703 | { | |
704 | wxSocketBase *sock = event.GetSocket(); | |
705 | if (!sock) | |
706 | return; | |
707 | ||
708 | wxSocketNotify evt = event.GetSocketEvent(); | |
709 | wxTCPConnection * const | |
710 | connection = static_cast<wxTCPConnection *>(sock->GetClientData()); | |
711 | ||
712 | // This socket is being deleted; skip this event | |
713 | if (!connection) | |
714 | return; | |
715 | ||
716 | if ( evt == wxSOCKET_LOST ) | |
717 | { | |
718 | HandleDisconnect(connection); | |
719 | return; | |
720 | } | |
721 | ||
722 | // Receive message number. | |
723 | wxIPCSocketStreams * const streams = connection->m_streams; | |
724 | ||
725 | const wxString topic = connection->m_topic; | |
726 | wxString item; | |
727 | ||
728 | bool error = false; | |
729 | ||
730 | const int msg = streams->Read8(); | |
731 | switch ( msg ) | |
732 | { | |
733 | case IPC_EXECUTE: | |
734 | { | |
735 | wxIPCFormat format; | |
736 | size_t size wxDUMMY_INITIALIZE(0); | |
737 | void * const | |
738 | data = streams->ReadFormatData(connection, &format, &size); | |
739 | if ( data ) | |
740 | connection->OnExecute(topic, data, size, format); | |
741 | else | |
742 | error = true; | |
743 | } | |
744 | break; | |
745 | ||
746 | case IPC_ADVISE: | |
747 | { | |
748 | item = streams->ReadString(); | |
749 | ||
750 | wxIPCFormat format; | |
751 | size_t size wxDUMMY_INITIALIZE(0); | |
752 | void * const | |
753 | data = streams->ReadFormatData(connection, &format, &size); | |
754 | ||
755 | if ( data ) | |
756 | connection->OnAdvise(topic, item, data, size, format); | |
757 | else | |
758 | error = true; | |
759 | } | |
760 | break; | |
761 | ||
762 | case IPC_ADVISE_START: | |
763 | { | |
764 | item = streams->ReadString(); | |
765 | ||
766 | IPCOutput(streams).Write8(connection->OnStartAdvise(topic, item) | |
767 | ? IPC_ADVISE_START | |
768 | : IPC_FAIL); | |
769 | } | |
770 | break; | |
771 | ||
772 | case IPC_ADVISE_STOP: | |
773 | { | |
774 | item = streams->ReadString(); | |
775 | ||
776 | IPCOutput(streams).Write8(connection->OnStopAdvise(topic, item) | |
777 | ? IPC_ADVISE_STOP | |
778 | : IPC_FAIL); | |
779 | } | |
780 | break; | |
781 | ||
782 | case IPC_POKE: | |
783 | { | |
784 | item = streams->ReadString(); | |
785 | wxIPCFormat format = (wxIPCFormat)streams->Read8(); | |
786 | ||
787 | size_t size wxDUMMY_INITIALIZE(0); | |
788 | void * const data = streams->ReadData(connection, &size); | |
789 | ||
790 | if ( data ) | |
791 | connection->OnPoke(topic, item, data, size, format); | |
792 | else | |
793 | error = true; | |
794 | } | |
795 | break; | |
796 | ||
797 | case IPC_REQUEST: | |
798 | { | |
799 | item = streams->ReadString(); | |
800 | ||
801 | wxIPCFormat format = (wxIPCFormat)streams->Read8(); | |
802 | ||
803 | size_t user_size = wxNO_LEN; | |
804 | const void *user_data = connection->OnRequest(topic, | |
805 | item, | |
806 | &user_size, | |
807 | format); | |
808 | ||
809 | if ( !user_data ) | |
810 | { | |
811 | IPCOutput(streams).Write8(IPC_FAIL); | |
812 | break; | |
813 | } | |
814 | ||
815 | IPCOutput out(streams); | |
816 | out.Write8(IPC_REQUEST_REPLY); | |
817 | ||
818 | if ( user_size == wxNO_LEN ) | |
819 | { | |
820 | switch ( format ) | |
821 | { | |
822 | case wxIPC_TEXT: | |
823 | case wxIPC_UTF8TEXT: | |
824 | user_size = strlen((const char *)user_data) + 1; // includes final NUL | |
825 | break; | |
826 | case wxIPC_UNICODETEXT: | |
827 | user_size = (wcslen((const wchar_t *)user_data) + 1) * sizeof(wchar_t); // includes final NUL | |
828 | break; | |
829 | default: | |
830 | user_size = 0; | |
831 | } | |
832 | } | |
833 | ||
834 | out.WriteData(user_data, user_size); | |
835 | } | |
836 | break; | |
837 | ||
838 | case IPC_DISCONNECT: | |
839 | HandleDisconnect(connection); | |
840 | break; | |
841 | ||
842 | case IPC_FAIL: | |
843 | wxLogDebug("Unexpected IPC_FAIL received"); | |
844 | error = true; | |
845 | break; | |
846 | ||
847 | default: | |
848 | wxLogDebug("Unknown message code %d received.", msg); | |
849 | error = true; | |
850 | break; | |
851 | } | |
852 | ||
853 | if ( error ) | |
854 | IPCOutput(streams).Write8(IPC_FAIL); | |
855 | } | |
856 | ||
857 | void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event) | |
858 | { | |
859 | wxSocketServer *server = (wxSocketServer *) event.GetSocket(); | |
860 | if (!server) | |
861 | return; | |
862 | wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData(); | |
863 | ||
864 | // This socket is being deleted; skip this event | |
865 | if (!ipcserv) | |
866 | return; | |
867 | ||
868 | if (event.GetSocketEvent() != wxSOCKET_CONNECTION) | |
869 | return; | |
870 | ||
871 | // Accept the connection, getting a new socket | |
872 | wxSocketBase *sock = server->Accept(); | |
873 | if (!sock) | |
874 | return; | |
875 | if (!sock->Ok()) | |
876 | { | |
877 | sock->Destroy(); | |
878 | return; | |
879 | } | |
880 | ||
881 | wxIPCSocketStreams *streams = new wxIPCSocketStreams(*sock); | |
882 | ||
883 | { | |
884 | IPCOutput out(streams); | |
885 | ||
886 | const int msg = streams->Read8(); | |
887 | if ( msg == IPC_CONNECT ) | |
888 | { | |
889 | const wxString topic = streams->ReadString(); | |
890 | ||
891 | wxTCPConnection *new_connection = | |
892 | (wxTCPConnection *)ipcserv->OnAcceptConnection (topic); | |
893 | ||
894 | if (new_connection) | |
895 | { | |
896 | if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) | |
897 | { | |
898 | // Acknowledge success | |
899 | out.Write8(IPC_CONNECT); | |
900 | ||
901 | new_connection->m_sock = sock; | |
902 | new_connection->m_streams = streams; | |
903 | new_connection->m_topic = topic; | |
904 | sock->SetEventHandler(wxTCPEventHandlerModule::GetHandler(), | |
905 | _CLIENT_ONREQUEST_ID); | |
906 | sock->SetClientData(new_connection); | |
907 | sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); | |
908 | sock->Notify(true); | |
909 | return; | |
910 | } | |
911 | else | |
912 | { | |
913 | delete new_connection; | |
914 | // and fall through to delete everything else | |
915 | } | |
916 | } | |
917 | } | |
918 | ||
919 | // Something went wrong, send failure message and delete everything | |
920 | out.Write8(IPC_FAIL); | |
921 | } // IPCOutput object is destroyed here, before destroying stream | |
922 | ||
923 | delete streams; | |
924 | sock->Destroy(); | |
925 | } | |
926 | ||
927 | #endif // wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS |