]> git.saurik.com Git - wxWidgets.git/blob - src/common/socket.cpp
use the currently active event loop for the event dispatching instead of wxYield...
[wxWidgets.git] / src / common / socket.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/socket.cpp
3 // Purpose: Socket handler classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
5 // Created: April 1997
6 // Copyright: (C) 1999-1997, Guilhem Lavaux
7 // (C) 2000-1999, Guillermo Rodriguez Garcia
8 // RCS_ID: $Id$
9 // License: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ==========================================================================
13 // Declarations
14 // ==========================================================================
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 #include "wx/socket.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/object.h"
29 #include "wx/string.h"
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #include "wx/event.h"
33 #include "wx/app.h"
34 #include "wx/utils.h"
35 #include "wx/timer.h"
36 #include "wx/module.h"
37 #endif
38
39 #include "wx/apptrait.h"
40 #include "wx/sckaddr.h"
41 #include "wx/stopwatch.h"
42 #include "wx/thread.h"
43 #include "wx/evtloop.h"
44
45 // DLL options compatibility check:
46 #include "wx/build.h"
47 WX_CHECK_BUILD_OPTIONS("wxNet")
48
49 // --------------------------------------------------------------------------
50 // macros and constants
51 // --------------------------------------------------------------------------
52
53 // discard buffer
54 #define MAX_DISCARD_SIZE (10 * 1024)
55
56 #define wxTRACE_Socket _T("wxSocket")
57
58 // --------------------------------------------------------------------------
59 // wxWin macros
60 // --------------------------------------------------------------------------
61
62 IMPLEMENT_CLASS(wxSocketBase, wxObject)
63 IMPLEMENT_CLASS(wxSocketServer, wxSocketBase)
64 IMPLEMENT_CLASS(wxSocketClient, wxSocketBase)
65 IMPLEMENT_CLASS(wxDatagramSocket, wxSocketBase)
66 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent)
67
68 // --------------------------------------------------------------------------
69 // private classes
70 // --------------------------------------------------------------------------
71
72 class wxSocketState : public wxObject
73 {
74 public:
75 wxSocketFlags m_flags;
76 wxSocketEventFlags m_eventmask;
77 bool m_notify;
78 void *m_clientData;
79
80 public:
81 wxSocketState() : wxObject() {}
82
83 DECLARE_NO_COPY_CLASS(wxSocketState)
84 };
85
86 // ============================================================================
87 // GSocketManager
88 // ============================================================================
89
90 GSocketManager *GSocketManager::ms_manager = NULL;
91
92 /* static */
93 void GSocketManager::Set(GSocketManager *manager)
94 {
95 wxASSERT_MSG( !ms_manager, "too late to set manager now" );
96
97 ms_manager = manager;
98 }
99
100 /* static */
101 void GSocketManager::Init()
102 {
103 wxASSERT_MSG( !ms_manager, "shouldn't be initialized twice" );
104
105 /*
106 Details: Initialize() creates a hidden window as a sink for socket
107 events, such as 'read completed'. wxMSW has only one message loop
108 for the main thread. If Initialize is called in a secondary thread,
109 the socket window will be created for the secondary thread, but
110 since there is no message loop on this thread, it will never
111 receive events and all socket operations will time out.
112 BTW, the main thread must not be stopped using sleep or block
113 on a semaphore (a bad idea in any case) or socket operations
114 will time out.
115
116 On the Mac side, Initialize() stores a pointer to the CFRunLoop for
117 the main thread. Because secondary threads do not have run loops,
118 adding event notifications to the "Current" loop would have no
119 effect at all, events would never fire.
120 */
121 wxASSERT_MSG( wxIsMainThread(),
122 "sockets must be initialized from the main thread" );
123
124 wxAppConsole * const app = wxAppConsole::GetInstance();
125 wxCHECK_RET( app, "sockets can't be initialized without wxApp" );
126
127 ms_manager = app->GetTraits()->GetSocketManager();
128 }
129
130 // ==========================================================================
131 // wxSocketBase
132 // ==========================================================================
133
134 // --------------------------------------------------------------------------
135 // Initialization and shutdown
136 // --------------------------------------------------------------------------
137
138 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
139 // to m_countInit with a crit section
140 size_t wxSocketBase::m_countInit = 0;
141
142 bool wxSocketBase::IsInitialized()
143 {
144 return m_countInit > 0;
145 }
146
147 bool wxSocketBase::Initialize()
148 {
149 if ( !m_countInit++ )
150 {
151 if ( !GSocket_Init() )
152 {
153 m_countInit--;
154
155 return false;
156 }
157 }
158
159 return true;
160 }
161
162 void wxSocketBase::Shutdown()
163 {
164 // we should be initialized
165 wxASSERT_MSG( m_countInit > 0, _T("extra call to Shutdown()") );
166 if ( --m_countInit == 0 )
167 {
168 GSocket_Cleanup();
169 }
170 }
171
172 // --------------------------------------------------------------------------
173 // Ctor and dtor
174 // --------------------------------------------------------------------------
175
176 void wxSocketBase::Init()
177 {
178 m_socket = NULL;
179 m_type = wxSOCKET_UNINIT;
180
181 // state
182 m_flags = 0;
183 m_connected =
184 m_establishing =
185 m_reading =
186 m_writing =
187 m_error =
188 m_closed = false;
189 m_lcount = 0;
190 m_timeout = 600;
191 m_beingDeleted = false;
192
193 // pushback buffer
194 m_unread = NULL;
195 m_unrd_size = 0;
196 m_unrd_cur = 0;
197
198 // events
199 m_id = wxID_ANY;
200 m_handler = NULL;
201 m_clientData = NULL;
202 m_notify = false;
203 m_eventmask = 0;
204
205 if ( !IsInitialized() )
206 {
207 // this Initialize() will be undone by wxSocketModule::OnExit(), all the
208 // other calls to it should be matched by a call to Shutdown()
209 Initialize();
210 }
211 }
212
213 wxSocketBase::wxSocketBase()
214 {
215 Init();
216 }
217
218 wxSocketBase::wxSocketBase(wxSocketFlags flags, wxSocketType type)
219 {
220 Init();
221
222 m_flags = flags;
223 m_type = type;
224 }
225
226 wxSocketBase::~wxSocketBase()
227 {
228 // Just in case the app called Destroy() *and* then deleted
229 // the socket immediately: don't leave dangling pointers.
230 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
231 if ( traits )
232 traits->RemoveFromPendingDelete(this);
233
234 // Shutdown and close the socket
235 if (!m_beingDeleted)
236 Close();
237
238 // Destroy the GSocket object
239 if (m_socket)
240 delete m_socket;
241
242 // Free the pushback buffer
243 if (m_unread)
244 free(m_unread);
245 }
246
247 bool wxSocketBase::Destroy()
248 {
249 // Delayed destruction: the socket will be deleted during the next
250 // idle loop iteration. This ensures that all pending events have
251 // been processed.
252 m_beingDeleted = true;
253
254 // Shutdown and close the socket
255 Close();
256
257 // Supress events from now on
258 Notify(false);
259
260 // schedule this object for deletion
261 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
262 if ( traits )
263 {
264 // let the traits object decide what to do with us
265 traits->ScheduleForDestroy(this);
266 }
267 else // no app or no traits
268 {
269 // in wxBase we might have no app object at all, don't leak memory
270 delete this;
271 }
272
273 return true;
274 }
275
276 // --------------------------------------------------------------------------
277 // Basic IO calls
278 // --------------------------------------------------------------------------
279
280 // The following IO operations update m_error and m_lcount:
281 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
282 //
283 // TODO: Should Connect, Accept and AcceptWith update m_error?
284
285 bool wxSocketBase::Close()
286 {
287 // Interrupt pending waits
288 InterruptWait();
289
290 if (m_socket)
291 {
292 // Disable callbacks
293 m_socket->UnsetCallback(GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
294 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG);
295
296 // Shutdown the connection
297 m_socket->Shutdown();
298 }
299
300 m_connected = false;
301 m_establishing = false;
302 return true;
303 }
304
305 wxSocketBase& wxSocketBase::Read(void* buffer, wxUint32 nbytes)
306 {
307 // Mask read events
308 m_reading = true;
309
310 m_lcount = _Read(buffer, nbytes);
311
312 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
313 if (m_flags & wxSOCKET_WAITALL)
314 m_error = (m_lcount != nbytes);
315 else
316 m_error = (m_lcount == 0);
317
318 // Allow read events from now on
319 m_reading = false;
320
321 return *this;
322 }
323
324 wxUint32 wxSocketBase::_Read(void* buffer_, wxUint32 nbytes)
325 {
326 char *buffer = (char *)buffer_;
327
328 int total;
329
330 // Try the pushback buffer first
331 total = GetPushback(buffer, nbytes, false);
332 nbytes -= total;
333 buffer = (char *)buffer + total;
334
335 // Return now in one of the following cases:
336 // - the socket is invalid,
337 // - we got all the data
338 if ( !m_socket ||
339 !nbytes )
340 return total;
341
342 // Possible combinations (they are checked in this order)
343 // wxSOCKET_NOWAIT
344 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
345 // wxSOCKET_BLOCK
346 // wxSOCKET_NONE
347 //
348 int ret;
349 if (m_flags & wxSOCKET_NOWAIT)
350 {
351 m_socket->SetNonBlocking(1);
352 ret = m_socket->Read(buffer, nbytes);
353 m_socket->SetNonBlocking(0);
354
355 if ( ret < 0 )
356 return 0;
357
358 total += ret;
359 }
360 else // blocking socket
361 {
362 for ( ;; )
363 {
364 // dispatch events unless disabled
365 if ( !(m_flags & wxSOCKET_BLOCK) && !WaitForRead() )
366 break;
367
368 ret = m_socket->Read(buffer, nbytes);
369 if ( ret == 0 )
370 {
371 // for connection-oriented (e.g. TCP) sockets we can only read 0
372 // bytes if the other end has been closed, and for connectionless
373 // ones (UDP) this flag doesn't make sense anyhow so we can set it to
374 // true too without doing any harm
375 m_closed = true;
376 break;
377 }
378
379 if ( ret < 0 )
380 {
381 // this will be always interpreted as error by Read()
382 return 0;
383 }
384
385 total += ret;
386
387 // if wxSOCKET_WAITALL is not set, we can leave now as we did read
388 // something
389 if ( !(m_flags & wxSOCKET_WAITALL) )
390 break;
391
392 // otherwise check if read the maximal requested amount of data
393 nbytes -= ret;
394 if ( !nbytes )
395 break;
396
397 // we didn't, so continue reading
398 buffer = (char *)buffer + ret;
399 }
400 }
401
402 return total;
403 }
404
405 wxSocketBase& wxSocketBase::ReadMsg(void* buffer, wxUint32 nbytes)
406 {
407 wxUint32 len, len2, sig, total;
408 bool error;
409 int old_flags;
410 struct
411 {
412 unsigned char sig[4];
413 unsigned char len[4];
414 } msg;
415
416 // Mask read events
417 m_reading = true;
418
419 total = 0;
420 error = true;
421 old_flags = m_flags;
422 SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL);
423
424 if (_Read(&msg, sizeof(msg)) != sizeof(msg))
425 goto exit;
426
427 sig = (wxUint32)msg.sig[0];
428 sig |= (wxUint32)(msg.sig[1] << 8);
429 sig |= (wxUint32)(msg.sig[2] << 16);
430 sig |= (wxUint32)(msg.sig[3] << 24);
431
432 if (sig != 0xfeeddead)
433 {
434 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
435 goto exit;
436 }
437
438 len = (wxUint32)msg.len[0];
439 len |= (wxUint32)(msg.len[1] << 8);
440 len |= (wxUint32)(msg.len[2] << 16);
441 len |= (wxUint32)(msg.len[3] << 24);
442
443 if (len > nbytes)
444 {
445 len2 = len - nbytes;
446 len = nbytes;
447 }
448 else
449 len2 = 0;
450
451 // Don't attemp to read if the msg was zero bytes long.
452 if (len)
453 {
454 total = _Read(buffer, len);
455
456 if (total != len)
457 goto exit;
458 }
459 if (len2)
460 {
461 char *discard_buffer = new char[MAX_DISCARD_SIZE];
462 long discard_len;
463
464 // NOTE: discarded bytes don't add to m_lcount.
465 do
466 {
467 discard_len = ((len2 > MAX_DISCARD_SIZE)? MAX_DISCARD_SIZE : len2);
468 discard_len = _Read(discard_buffer, (wxUint32)discard_len);
469 len2 -= (wxUint32)discard_len;
470 }
471 while ((discard_len > 0) && len2);
472
473 delete [] discard_buffer;
474
475 if (len2 != 0)
476 goto exit;
477 }
478 if (_Read(&msg, sizeof(msg)) != sizeof(msg))
479 goto exit;
480
481 sig = (wxUint32)msg.sig[0];
482 sig |= (wxUint32)(msg.sig[1] << 8);
483 sig |= (wxUint32)(msg.sig[2] << 16);
484 sig |= (wxUint32)(msg.sig[3] << 24);
485
486 if (sig != 0xdeadfeed)
487 {
488 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
489 goto exit;
490 }
491
492 // everything was OK
493 error = false;
494
495 exit:
496 m_error = error;
497 m_lcount = total;
498 m_reading = false;
499 SetFlags(old_flags);
500
501 return *this;
502 }
503
504 wxSocketBase& wxSocketBase::Peek(void* buffer, wxUint32 nbytes)
505 {
506 // Mask read events
507 m_reading = true;
508
509 m_lcount = _Read(buffer, nbytes);
510 Pushback(buffer, m_lcount);
511
512 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
513 if (m_flags & wxSOCKET_WAITALL)
514 m_error = (m_lcount != nbytes);
515 else
516 m_error = (m_lcount == 0);
517
518 // Allow read events again
519 m_reading = false;
520
521 return *this;
522 }
523
524 wxSocketBase& wxSocketBase::Write(const void *buffer, wxUint32 nbytes)
525 {
526 // Mask write events
527 m_writing = true;
528
529 m_lcount = _Write(buffer, nbytes);
530
531 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
532 if (m_flags & wxSOCKET_WAITALL)
533 m_error = (m_lcount != nbytes);
534 else
535 m_error = (m_lcount == 0);
536
537 // Allow write events again
538 m_writing = false;
539
540 return *this;
541 }
542
543 wxUint32 wxSocketBase::_Write(const void *buffer_, wxUint32 nbytes)
544 {
545 const char *buffer = (const char *)buffer_;
546
547 wxUint32 total = 0;
548
549 // If the socket is invalid or parameters are ill, return immediately
550 if (!m_socket || !buffer || !nbytes)
551 return 0;
552
553 // Possible combinations (they are checked in this order)
554 // wxSOCKET_NOWAIT
555 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
556 // wxSOCKET_BLOCK
557 // wxSOCKET_NONE
558 //
559 int ret;
560 if (m_flags & wxSOCKET_NOWAIT)
561 {
562 m_socket->SetNonBlocking(1);
563 ret = m_socket->Write(buffer, nbytes);
564 m_socket->SetNonBlocking(0);
565
566 if (ret > 0)
567 total = ret;
568 }
569 else // blocking socket
570 {
571 for ( ;; )
572 {
573 if ( !(m_flags & wxSOCKET_BLOCK) && !WaitForWrite() )
574 break;
575
576 ret = m_socket->Write(buffer, nbytes);
577
578 // see comments for similar logic for ret handling in _Read()
579 if ( ret == 0 )
580 {
581 m_closed = true;
582 break;
583 }
584
585 if ( ret < 0 )
586 {
587 return 0;
588 }
589
590 total += ret;
591 if ( !(m_flags & wxSOCKET_WAITALL) )
592 break;
593
594 nbytes -= ret;
595 if ( !nbytes )
596 break;
597
598 buffer = (const char *)buffer + ret;
599 }
600 }
601
602 return total;
603 }
604
605 wxSocketBase& wxSocketBase::WriteMsg(const void *buffer, wxUint32 nbytes)
606 {
607 wxUint32 total;
608 bool error;
609 struct
610 {
611 unsigned char sig[4];
612 unsigned char len[4];
613 } msg;
614
615 // Mask write events
616 m_writing = true;
617
618 error = true;
619 total = 0;
620 SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL);
621
622 msg.sig[0] = (unsigned char) 0xad;
623 msg.sig[1] = (unsigned char) 0xde;
624 msg.sig[2] = (unsigned char) 0xed;
625 msg.sig[3] = (unsigned char) 0xfe;
626
627 msg.len[0] = (unsigned char) (nbytes & 0xff);
628 msg.len[1] = (unsigned char) ((nbytes >> 8) & 0xff);
629 msg.len[2] = (unsigned char) ((nbytes >> 16) & 0xff);
630 msg.len[3] = (unsigned char) ((nbytes >> 24) & 0xff);
631
632 if (_Write(&msg, sizeof(msg)) < sizeof(msg))
633 goto exit;
634
635 total = _Write(buffer, nbytes);
636
637 if (total < nbytes)
638 goto exit;
639
640 msg.sig[0] = (unsigned char) 0xed;
641 msg.sig[1] = (unsigned char) 0xfe;
642 msg.sig[2] = (unsigned char) 0xad;
643 msg.sig[3] = (unsigned char) 0xde;
644 msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0;
645
646 if ((_Write(&msg, sizeof(msg))) < sizeof(msg))
647 goto exit;
648
649 // everything was OK
650 error = false;
651
652 exit:
653 m_error = error;
654 m_lcount = total;
655 m_writing = false;
656
657 return *this;
658 }
659
660 wxSocketBase& wxSocketBase::Unread(const void *buffer, wxUint32 nbytes)
661 {
662 if (nbytes != 0)
663 Pushback(buffer, nbytes);
664
665 m_error = false;
666 m_lcount = nbytes;
667
668 return *this;
669 }
670
671 wxSocketBase& wxSocketBase::Discard()
672 {
673 char *buffer = new char[MAX_DISCARD_SIZE];
674 wxUint32 ret;
675 wxUint32 total = 0;
676
677 // Mask read events
678 m_reading = true;
679
680 SetFlags(wxSOCKET_NOWAIT);
681
682 do
683 {
684 ret = _Read(buffer, MAX_DISCARD_SIZE);
685 total += ret;
686 }
687 while (ret == MAX_DISCARD_SIZE);
688
689 delete[] buffer;
690 m_lcount = total;
691 m_error = false;
692
693 // Allow read events again
694 m_reading = false;
695
696 return *this;
697 }
698
699 // --------------------------------------------------------------------------
700 // Wait functions
701 // --------------------------------------------------------------------------
702
703 // All Wait functions poll the socket using GSocket_Select() to
704 // check for the specified combination of conditions, until one
705 // of these conditions become true, an error occurs, or the
706 // timeout elapses. The polling loop runs the event loop so that
707 // this won't block the GUI.
708
709 bool wxSocketBase::_Wait(long seconds,
710 long milliseconds,
711 wxSocketEventFlags flags)
712 {
713 GSocketEventFlags result;
714 long timeout; // in ms
715
716 // Set this to true to interrupt ongoing waits
717 m_interrupt = false;
718
719 // Check for valid socket
720 if (!m_socket)
721 return false;
722
723 // Check for valid timeout value.
724 if (seconds != -1)
725 timeout = seconds * 1000 + milliseconds;
726 else
727 timeout = m_timeout * 1000;
728
729 // Get the active event loop
730 wxEventLoopBase * const eventLoop = wxEventLoop::GetActive();
731
732 #ifdef __WXMSW__
733 wxASSERT_MSG( !wxIsMainThread() || eventLoop,
734 "Sockets won't work without a running event loop" );
735 #endif // __WXMSW__
736
737 // Wait in an active polling loop.
738 //
739 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
740 // hurt. It has to be here because the (GSocket) event might arrive
741 // a bit delayed, and it has to be in OnRequest as well because we
742 // don't know whether the Wait functions are being used.
743 //
744 // Do this at least once (important if timeout == 0, when
745 // we are just polling). Also, if just polling, do not yield.
746
747 const wxMilliClock_t time_limit = wxGetLocalTimeMillis() + timeout;
748 bool done = false;
749 bool valid_result = false;
750
751 if (!eventLoop)
752 {
753 // This is used to avoid a busy loop on wxBase - having a select
754 // timeout of 50 ms per iteration should be enough.
755 if (timeout > 50)
756 m_socket->SetTimeout(50);
757 else
758 m_socket->SetTimeout(timeout);
759 }
760
761 while (!done)
762 {
763 result = m_socket->Select(flags | GSOCK_LOST_FLAG);
764
765 // Incoming connection (server) or connection established (client)
766 if (result & GSOCK_CONNECTION_FLAG)
767 {
768 m_connected = true;
769 m_establishing = false;
770 valid_result = true;
771 break;
772 }
773
774 // Data available or output buffer ready
775 if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG))
776 {
777 valid_result = true;
778 break;
779 }
780
781 // Connection lost
782 if (result & GSOCK_LOST_FLAG)
783 {
784 m_connected = false;
785 m_establishing = false;
786 valid_result = ((flags & GSOCK_LOST_FLAG) != 0);
787 break;
788 }
789
790 // Wait more?
791 long time_left = wxMilliClockToLong(time_limit - wxGetLocalTimeMillis());
792 if ((!timeout) || (time_left <= 0) || (m_interrupt))
793 done = true;
794 else
795 {
796 if (eventLoop)
797 {
798 // from the main thread itself we have to run the event loop to let the
799 // events (including the GUI events and the low-level (not wxWidgets)
800 // events from GSocket) be processed but from another thread it is
801 // enough to just call wxThread::Yield() which will give away the rest
802 // of our time slice: the explanation is that the events will be
803 // processed by the main thread anyhow, but we don't want to eat the
804 // CPU time uselessly while sitting in the loop waiting for the data
805 if ( wxIsMainThread() )
806 {
807 if ( eventLoop->Pending() )
808 eventLoop->Dispatch();
809 }
810 #if wxUSE_THREADS
811 else
812 wxThread::Yield();
813 #endif // wxUSE_THREADS
814 }
815 else
816 {
817 // If there's less than 50 ms left, just call select with that timeout.
818 if (time_left < 50)
819 m_socket->SetTimeout(time_left);
820 }
821 }
822 }
823
824 // Set timeout back to original value (we overwrote it for polling)
825 if (!eventLoop)
826 m_socket->SetTimeout(m_timeout*1000);
827
828 return valid_result;
829 }
830
831 bool wxSocketBase::Wait(long seconds, long milliseconds)
832 {
833 return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG |
834 GSOCK_OUTPUT_FLAG |
835 GSOCK_CONNECTION_FLAG |
836 GSOCK_LOST_FLAG);
837 }
838
839 bool wxSocketBase::WaitForRead(long seconds, long milliseconds)
840 {
841 // Check pushback buffer before entering _Wait
842 if (m_unread)
843 return true;
844
845 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
846 // _Wait because of the semantics of WaitForRead: a return
847 // value of true means that a GSocket_Read call will return
848 // immediately, not that there is actually data to read.
849
850 return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG |
851 GSOCK_LOST_FLAG);
852 }
853
854
855 bool wxSocketBase::WaitForWrite(long seconds, long milliseconds)
856 {
857 return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG);
858 }
859
860 bool wxSocketBase::WaitForLost(long seconds, long milliseconds)
861 {
862 return _Wait(seconds, milliseconds, GSOCK_LOST_FLAG);
863 }
864
865 // --------------------------------------------------------------------------
866 // Miscellaneous
867 // --------------------------------------------------------------------------
868
869 //
870 // Get local or peer address
871 //
872
873 bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const
874 {
875 GAddress *peer;
876
877 if (!m_socket)
878 return false;
879
880 peer = m_socket->GetPeer();
881
882 // copying a null address would just trigger an assert anyway
883
884 if (!peer)
885 return false;
886
887 addr_man.SetAddress(peer);
888 GAddress_destroy(peer);
889
890 return true;
891 }
892
893 bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const
894 {
895 GAddress *local;
896
897 if (!m_socket)
898 return false;
899
900 local = m_socket->GetLocal();
901 addr_man.SetAddress(local);
902 GAddress_destroy(local);
903
904 return true;
905 }
906
907 //
908 // Save and restore socket state
909 //
910
911 void wxSocketBase::SaveState()
912 {
913 wxSocketState *state;
914
915 state = new wxSocketState();
916
917 state->m_flags = m_flags;
918 state->m_notify = m_notify;
919 state->m_eventmask = m_eventmask;
920 state->m_clientData = m_clientData;
921
922 m_states.Append(state);
923 }
924
925 void wxSocketBase::RestoreState()
926 {
927 wxList::compatibility_iterator node;
928 wxSocketState *state;
929
930 node = m_states.GetLast();
931 if (!node)
932 return;
933
934 state = (wxSocketState *)node->GetData();
935
936 m_flags = state->m_flags;
937 m_notify = state->m_notify;
938 m_eventmask = state->m_eventmask;
939 m_clientData = state->m_clientData;
940
941 m_states.Erase(node);
942 delete state;
943 }
944
945 //
946 // Timeout and flags
947 //
948
949 void wxSocketBase::SetTimeout(long seconds)
950 {
951 m_timeout = seconds;
952
953 if (m_socket)
954 m_socket->SetTimeout(m_timeout * 1000);
955 }
956
957 void wxSocketBase::SetFlags(wxSocketFlags flags)
958 {
959 m_flags = flags;
960 }
961
962
963 // --------------------------------------------------------------------------
964 // Event handling
965 // --------------------------------------------------------------------------
966
967 // A note on how events are processed, which is probably the most
968 // difficult thing to get working right while keeping the same API
969 // and functionality for all platforms.
970 //
971 // When GSocket detects an event, it calls wx_socket_callback, which in
972 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
973 // object. OnRequest does some housekeeping, and if the event is to be
974 // propagated to the user, it creates a new wxSocketEvent object and
975 // posts it. The event is not processed immediately, but delayed with
976 // AddPendingEvent instead. This is necessary in order to decouple the
977 // event processing from wx_socket_callback; otherwise, subsequent IO
978 // calls made from the user event handler would fail, as gtk callbacks
979 // are not reentrant.
980 //
981 // Note that, unlike events, user callbacks (now deprecated) are _not_
982 // decoupled from wx_socket_callback and thus they suffer from a variety
983 // of problems. Avoid them where possible and use events instead.
984
985 extern "C"
986 void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket),
987 GSocketEvent notification,
988 char *cdata)
989 {
990 wxSocketBase *sckobj = (wxSocketBase *)cdata;
991
992 sckobj->OnRequest((wxSocketNotify) notification);
993 }
994
995 void wxSocketBase::OnRequest(wxSocketNotify notification)
996 {
997 // NOTE: We duplicate some of the code in _Wait, but this doesn't
998 // hurt. It has to be here because the (GSocket) event might arrive
999 // a bit delayed, and it has to be in _Wait as well because we don't
1000 // know whether the Wait functions are being used.
1001
1002 switch(notification)
1003 {
1004 case wxSOCKET_CONNECTION:
1005 m_establishing = false;
1006 m_connected = true;
1007 break;
1008
1009 // If we are in the middle of a R/W operation, do not
1010 // propagate events to users. Also, filter 'late' events
1011 // which are no longer valid.
1012
1013 case wxSOCKET_INPUT:
1014 if (m_reading || !m_socket->Select(GSOCK_INPUT_FLAG))
1015 return;
1016 break;
1017
1018 case wxSOCKET_OUTPUT:
1019 if (m_writing || !m_socket->Select(GSOCK_OUTPUT_FLAG))
1020 return;
1021 break;
1022
1023 case wxSOCKET_LOST:
1024 m_connected = false;
1025 m_establishing = false;
1026 break;
1027
1028 default:
1029 break;
1030 }
1031
1032 // Schedule the event
1033
1034 wxSocketEventFlags flag = 0;
1035 wxUnusedVar(flag);
1036 switch (notification)
1037 {
1038 case GSOCK_INPUT: flag = GSOCK_INPUT_FLAG; break;
1039 case GSOCK_OUTPUT: flag = GSOCK_OUTPUT_FLAG; break;
1040 case GSOCK_CONNECTION: flag = GSOCK_CONNECTION_FLAG; break;
1041 case GSOCK_LOST: flag = GSOCK_LOST_FLAG; break;
1042 default:
1043 wxLogWarning(_("wxSocket: unknown event!."));
1044 return;
1045 }
1046
1047 if (((m_eventmask & flag) == flag) && m_notify)
1048 {
1049 if (m_handler)
1050 {
1051 wxSocketEvent event(m_id);
1052 event.m_event = notification;
1053 event.m_clientData = m_clientData;
1054 event.SetEventObject(this);
1055
1056 m_handler->AddPendingEvent(event);
1057 }
1058 }
1059 }
1060
1061 void wxSocketBase::Notify(bool notify)
1062 {
1063 m_notify = notify;
1064 if (m_socket)
1065 m_socket->Notify(notify);
1066 }
1067
1068 void wxSocketBase::SetNotify(wxSocketEventFlags flags)
1069 {
1070 m_eventmask = flags;
1071 }
1072
1073 void wxSocketBase::SetEventHandler(wxEvtHandler& handler, int id)
1074 {
1075 m_handler = &handler;
1076 m_id = id;
1077 }
1078
1079 // --------------------------------------------------------------------------
1080 // Pushback buffer
1081 // --------------------------------------------------------------------------
1082
1083 void wxSocketBase::Pushback(const void *buffer, wxUint32 size)
1084 {
1085 if (!size) return;
1086
1087 if (m_unread == NULL)
1088 m_unread = malloc(size);
1089 else
1090 {
1091 void *tmp;
1092
1093 tmp = malloc(m_unrd_size + size);
1094 memcpy((char *)tmp + size, m_unread, m_unrd_size);
1095 free(m_unread);
1096
1097 m_unread = tmp;
1098 }
1099
1100 m_unrd_size += size;
1101
1102 memcpy(m_unread, buffer, size);
1103 }
1104
1105 wxUint32 wxSocketBase::GetPushback(void *buffer, wxUint32 size, bool peek)
1106 {
1107 if (!m_unrd_size)
1108 return 0;
1109
1110 if (size > (m_unrd_size-m_unrd_cur))
1111 size = m_unrd_size-m_unrd_cur;
1112
1113 memcpy(buffer, (char *)m_unread + m_unrd_cur, size);
1114
1115 if (!peek)
1116 {
1117 m_unrd_cur += size;
1118 if (m_unrd_size == m_unrd_cur)
1119 {
1120 free(m_unread);
1121 m_unread = NULL;
1122 m_unrd_size = 0;
1123 m_unrd_cur = 0;
1124 }
1125 }
1126
1127 return size;
1128 }
1129
1130
1131 // ==========================================================================
1132 // wxSocketServer
1133 // ==========================================================================
1134
1135 // --------------------------------------------------------------------------
1136 // Ctor
1137 // --------------------------------------------------------------------------
1138
1139 wxSocketServer::wxSocketServer(const wxSockAddress& addr_man,
1140 wxSocketFlags flags)
1141 : wxSocketBase(flags, wxSOCKET_SERVER)
1142 {
1143 wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") );
1144
1145 m_socket = GSocket_new();
1146
1147 if (!m_socket)
1148 {
1149 wxLogTrace( wxTRACE_Socket, _T("*** GSocket_new failed") );
1150 return;
1151 }
1152
1153 // Setup the socket as server
1154 m_socket->Notify(m_notify);
1155 m_socket->SetLocal(addr_man.GetAddress());
1156
1157 if (GetFlags() & wxSOCKET_REUSEADDR) {
1158 m_socket->SetReusable();
1159 }
1160 if (GetFlags() & wxSOCKET_BROADCAST) {
1161 m_socket->SetBroadcast();
1162 }
1163 if (GetFlags() & wxSOCKET_NOBIND) {
1164 m_socket->DontDoBind();
1165 }
1166
1167 if (m_socket->SetServer() != GSOCK_NOERROR)
1168 {
1169 delete m_socket;
1170 m_socket = NULL;
1171
1172 wxLogTrace( wxTRACE_Socket, _T("*** GSocket_SetServer failed") );
1173 return;
1174 }
1175
1176 m_socket->SetTimeout(m_timeout * 1000);
1177 m_socket->SetCallback(GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
1178 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
1179 wx_socket_callback, (char *)this);
1180
1181 wxLogTrace( wxTRACE_Socket, _T("wxSocketServer on fd %d"), m_socket->m_fd );
1182 }
1183
1184 // --------------------------------------------------------------------------
1185 // Accept
1186 // --------------------------------------------------------------------------
1187
1188 bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait)
1189 {
1190 GSocket *child_socket;
1191
1192 if (!m_socket)
1193 return false;
1194
1195 // If wait == false, then the call should be nonblocking.
1196 // When we are finished, we put the socket to blocking mode
1197 // again.
1198
1199 if (!wait)
1200 m_socket->SetNonBlocking(1);
1201
1202 child_socket = m_socket->WaitConnection();
1203
1204 if (!wait)
1205 m_socket->SetNonBlocking(0);
1206
1207 if (!child_socket)
1208 return false;
1209
1210 sock.m_type = wxSOCKET_BASE;
1211 sock.m_socket = child_socket;
1212 sock.m_connected = true;
1213
1214 sock.m_socket->SetTimeout(sock.m_timeout * 1000);
1215 sock.m_socket->SetCallback(GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
1216 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
1217 wx_socket_callback, (char *)&sock);
1218
1219 return true;
1220 }
1221
1222 wxSocketBase *wxSocketServer::Accept(bool wait)
1223 {
1224 wxSocketBase* sock = new wxSocketBase();
1225
1226 sock->SetFlags(m_flags);
1227
1228 if (!AcceptWith(*sock, wait))
1229 {
1230 sock->Destroy();
1231 sock = NULL;
1232 }
1233
1234 return sock;
1235 }
1236
1237 bool wxSocketServer::WaitForAccept(long seconds, long milliseconds)
1238 {
1239 return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG);
1240 }
1241
1242 bool wxSocketBase::GetOption(int level, int optname, void *optval, int *optlen)
1243 {
1244 wxASSERT_MSG( m_socket, _T("Socket not initialised") );
1245
1246 if (m_socket->GetSockOpt(level, optname, optval, optlen)
1247 != GSOCK_NOERROR)
1248 {
1249 return false;
1250 }
1251 return true;
1252 }
1253
1254 bool wxSocketBase::SetOption(int level, int optname, const void *optval,
1255 int optlen)
1256 {
1257 wxASSERT_MSG( m_socket, _T("Socket not initialised") );
1258
1259 if (m_socket->SetSockOpt(level, optname, optval, optlen)
1260 != GSOCK_NOERROR)
1261 {
1262 return false;
1263 }
1264 return true;
1265 }
1266
1267 bool wxSocketBase::SetLocal(const wxIPV4address& local)
1268 {
1269 GAddress* la = local.GetAddress();
1270
1271 // If the address is valid, save it for use when we call Connect
1272 if (la && la->m_addr)
1273 {
1274 m_localAddress = local;
1275
1276 return true;
1277 }
1278
1279 return false;
1280 }
1281
1282 // ==========================================================================
1283 // wxSocketClient
1284 // ==========================================================================
1285
1286 // --------------------------------------------------------------------------
1287 // Ctor and dtor
1288 // --------------------------------------------------------------------------
1289
1290 wxSocketClient::wxSocketClient(wxSocketFlags flags)
1291 : wxSocketBase(flags, wxSOCKET_CLIENT)
1292 {
1293 m_initialRecvBufferSize =
1294 m_initialSendBufferSize = -1;
1295 }
1296
1297 wxSocketClient::~wxSocketClient()
1298 {
1299 }
1300
1301 // --------------------------------------------------------------------------
1302 // Connect
1303 // --------------------------------------------------------------------------
1304
1305 bool wxSocketClient::DoConnect(const wxSockAddress& addr_man,
1306 const wxSockAddress* local,
1307 bool wait)
1308 {
1309 GSocketError err;
1310
1311 if (m_socket)
1312 {
1313 // Shutdown and destroy the socket
1314 Close();
1315 delete m_socket;
1316 }
1317
1318 m_socket = GSocket_new();
1319 m_connected = false;
1320 m_establishing = false;
1321
1322 if (!m_socket)
1323 return false;
1324
1325 m_socket->SetTimeout(m_timeout * 1000);
1326 m_socket->SetCallback(GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
1327 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
1328 wx_socket_callback, (char *)this);
1329
1330 // If wait == false, then the call should be nonblocking.
1331 // When we are finished, we put the socket to blocking mode
1332 // again.
1333
1334 if (!wait)
1335 m_socket->SetNonBlocking(1);
1336
1337 // Reuse makes sense for clients too, if we are trying to rebind to the same port
1338 if (GetFlags() & wxSOCKET_REUSEADDR)
1339 {
1340 m_socket->SetReusable();
1341 }
1342 if (GetFlags() & wxSOCKET_BROADCAST)
1343 {
1344 m_socket->SetBroadcast();
1345 }
1346 if (GetFlags() & wxSOCKET_NOBIND)
1347 {
1348 m_socket->DontDoBind();
1349 }
1350
1351 // If no local address was passed and one has been set, use the one that was Set
1352 if (!local && m_localAddress.GetAddress())
1353 {
1354 local = &m_localAddress;
1355 }
1356
1357 // Bind to the local IP address and port, when provided
1358 if (local)
1359 {
1360 GAddress* la = local->GetAddress();
1361
1362 if (la && la->m_addr)
1363 m_socket->SetLocal(la);
1364 }
1365
1366 #if defined(__WXMSW__) || defined(__WXGTK__)
1367 m_socket->SetInitialSocketBuffers(m_initialRecvBufferSize, m_initialSendBufferSize);
1368 #endif
1369
1370 m_socket->SetPeer(addr_man.GetAddress());
1371 err = m_socket->Connect(GSOCK_STREAMED);
1372
1373 //this will register for callbacks - must be called after m_socket->m_fd was initialized
1374 m_socket->Notify(m_notify);
1375
1376 if (!wait)
1377 m_socket->SetNonBlocking(0);
1378
1379 if (err != GSOCK_NOERROR)
1380 {
1381 if (err == GSOCK_WOULDBLOCK)
1382 m_establishing = true;
1383
1384 return false;
1385 }
1386
1387 m_connected = true;
1388 return true;
1389 }
1390
1391 bool wxSocketClient::Connect(const wxSockAddress& addr_man, bool wait)
1392 {
1393 return DoConnect(addr_man, NULL, wait);
1394 }
1395
1396 bool wxSocketClient::Connect(const wxSockAddress& addr_man,
1397 const wxSockAddress& local,
1398 bool wait)
1399 {
1400 return DoConnect(addr_man, &local, wait);
1401 }
1402
1403 bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds)
1404 {
1405 if (m_connected) // Already connected
1406 return true;
1407
1408 if (!m_establishing || !m_socket) // No connection in progress
1409 return false;
1410
1411 return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG |
1412 GSOCK_LOST_FLAG);
1413 }
1414
1415 // ==========================================================================
1416 // wxDatagramSocket
1417 // ==========================================================================
1418
1419 /* NOTE: experimental stuff - might change */
1420
1421 wxDatagramSocket::wxDatagramSocket( const wxSockAddress& addr,
1422 wxSocketFlags flags )
1423 : wxSocketBase( flags, wxSOCKET_DATAGRAM )
1424 {
1425 // Create the socket
1426 m_socket = GSocket_new();
1427
1428 if (!m_socket)
1429 {
1430 wxFAIL_MSG( _T("datagram socket not new'd") );
1431 return;
1432 }
1433 m_socket->Notify(m_notify);
1434 // Setup the socket as non connection oriented
1435 m_socket->SetLocal(addr.GetAddress());
1436 if (flags & wxSOCKET_REUSEADDR)
1437 {
1438 m_socket->SetReusable();
1439 }
1440 if (GetFlags() & wxSOCKET_BROADCAST)
1441 {
1442 m_socket->SetBroadcast();
1443 }
1444 if (GetFlags() & wxSOCKET_NOBIND)
1445 {
1446 m_socket->DontDoBind();
1447 }
1448 if ( m_socket->SetNonOriented() != GSOCK_NOERROR )
1449 {
1450 delete m_socket;
1451 m_socket = NULL;
1452 return;
1453 }
1454
1455 // Initialize all stuff
1456 m_connected = false;
1457 m_establishing = false;
1458 m_socket->SetTimeout( m_timeout );
1459 m_socket->SetCallback( GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
1460 GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
1461 wx_socket_callback, (char*)this );
1462 }
1463
1464 wxDatagramSocket& wxDatagramSocket::RecvFrom( wxSockAddress& addr,
1465 void* buf,
1466 wxUint32 nBytes )
1467 {
1468 Read(buf, nBytes);
1469 GetPeer(addr);
1470 return (*this);
1471 }
1472
1473 wxDatagramSocket& wxDatagramSocket::SendTo( const wxSockAddress& addr,
1474 const void* buf,
1475 wxUint32 nBytes )
1476 {
1477 wxASSERT_MSG( m_socket, _T("Socket not initialised") );
1478
1479 m_socket->SetPeer(addr.GetAddress());
1480 Write(buf, nBytes);
1481 return (*this);
1482 }
1483
1484 // ==========================================================================
1485 // wxSocketModule
1486 // ==========================================================================
1487
1488 class wxSocketModule : public wxModule
1489 {
1490 public:
1491 virtual bool OnInit()
1492 {
1493 // wxSocketBase will call GSocket_Init() itself when/if needed
1494 return true;
1495 }
1496
1497 virtual void OnExit()
1498 {
1499 if ( wxSocketBase::IsInitialized() )
1500 wxSocketBase::Shutdown();
1501 }
1502
1503 private:
1504 DECLARE_DYNAMIC_CLASS(wxSocketModule)
1505 };
1506
1507 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule)
1508
1509 #endif
1510 // wxUSE_SOCKETS