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