]> git.saurik.com Git - wxWidgets.git/blob - src/common/socket.cpp
our sockets are always non-blocking anyhow so throw away all the code dealing with...
[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) 1999-2000, Guillermo Rodriguez Garcia
8 // (C) 2008 Vadim Zeitlin
9 // RCS_ID: $Id$
10 // License: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ==========================================================================
14 // Declarations
15 // ==========================================================================
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #if wxUSE_SOCKETS
25
26 #include "wx/socket.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/object.h"
30 #include "wx/string.h"
31 #include "wx/intl.h"
32 #include "wx/log.h"
33 #include "wx/event.h"
34 #include "wx/app.h"
35 #include "wx/utils.h"
36 #include "wx/timer.h"
37 #include "wx/module.h"
38 #endif
39
40 #include "wx/apptrait.h"
41 #include "wx/sckaddr.h"
42 #include "wx/stopwatch.h"
43 #include "wx/thread.h"
44 #include "wx/evtloop.h"
45
46 #include "wx/private/fd.h"
47 #include "wx/private/socket.h"
48
49 // DLL options compatibility check:
50 #include "wx/build.h"
51 WX_CHECK_BUILD_OPTIONS("wxNet")
52
53 // --------------------------------------------------------------------------
54 // macros and constants
55 // --------------------------------------------------------------------------
56
57 // discard buffer
58 #define MAX_DISCARD_SIZE (10 * 1024)
59
60 #define wxTRACE_Socket _T("wxSocket")
61
62 // --------------------------------------------------------------------------
63 // wxWin macros
64 // --------------------------------------------------------------------------
65
66 IMPLEMENT_CLASS(wxSocketBase, wxObject)
67 IMPLEMENT_CLASS(wxSocketServer, wxSocketBase)
68 IMPLEMENT_CLASS(wxSocketClient, wxSocketBase)
69 IMPLEMENT_CLASS(wxDatagramSocket, wxSocketBase)
70 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent)
71
72 // ----------------------------------------------------------------------------
73 // private functions
74 // ----------------------------------------------------------------------------
75
76 namespace
77 {
78
79 void SetTimeValFromMS(timeval& tv, unsigned long ms)
80 {
81 tv.tv_sec = (ms / 1000);
82 tv.tv_usec = (ms % 1000) * 1000;
83 }
84
85 } // anonymous namespace
86
87 // --------------------------------------------------------------------------
88 // private classes
89 // --------------------------------------------------------------------------
90
91 class wxSocketState : public wxObject
92 {
93 public:
94 wxSocketFlags m_flags;
95 wxSocketEventFlags m_eventmask;
96 bool m_notify;
97 void *m_clientData;
98
99 public:
100 wxSocketState() : wxObject() {}
101
102 DECLARE_NO_COPY_CLASS(wxSocketState)
103 };
104
105 // ============================================================================
106 // wxSocketManager
107 // ============================================================================
108
109 wxSocketManager *wxSocketManager::ms_manager = NULL;
110
111 /* static */
112 void wxSocketManager::Set(wxSocketManager *manager)
113 {
114 wxASSERT_MSG( !ms_manager, "too late to set manager now" );
115
116 ms_manager = manager;
117 }
118
119 /* static */
120 void wxSocketManager::Init()
121 {
122 wxASSERT_MSG( !ms_manager, "shouldn't be initialized twice" );
123
124 /*
125 Details: Initialize() creates a hidden window as a sink for socket
126 events, such as 'read completed'. wxMSW has only one message loop
127 for the main thread. If Initialize is called in a secondary thread,
128 the socket window will be created for the secondary thread, but
129 since there is no message loop on this thread, it will never
130 receive events and all socket operations will time out.
131 BTW, the main thread must not be stopped using sleep or block
132 on a semaphore (a bad idea in any case) or socket operations
133 will time out.
134
135 On the Mac side, Initialize() stores a pointer to the CFRunLoop for
136 the main thread. Because secondary threads do not have run loops,
137 adding event notifications to the "Current" loop would have no
138 effect at all, events would never fire.
139 */
140 wxASSERT_MSG( wxIsMainThread(),
141 "sockets must be initialized from the main thread" );
142
143 wxAppConsole * const app = wxAppConsole::GetInstance();
144 wxCHECK_RET( app, "sockets can't be initialized without wxApp" );
145
146 ms_manager = app->GetTraits()->GetSocketManager();
147 }
148
149 // ==========================================================================
150 // wxSocketImpl
151 // ==========================================================================
152
153 wxSocketImpl::wxSocketImpl(wxSocketBase& wxsocket)
154 : m_wxsocket(&wxsocket)
155 {
156 m_fd = INVALID_SOCKET;
157 m_detected = 0;
158 m_local = NULL;
159 m_peer = NULL;
160 m_error = wxSOCKET_NOERROR;
161 m_server = false;
162 m_stream = true;
163
164 SetTimeout(wxsocket.GetTimeout() * 1000);
165
166 m_establishing = false;
167 m_reusable = false;
168 m_broadcast = false;
169 m_dobind = true;
170 m_initialRecvBufferSize = -1;
171 m_initialSendBufferSize = -1;
172 }
173
174 wxSocketImpl::~wxSocketImpl()
175 {
176 if (m_fd != INVALID_SOCKET)
177 Shutdown();
178
179 if (m_local)
180 GAddress_destroy(m_local);
181
182 if (m_peer)
183 GAddress_destroy(m_peer);
184 }
185
186 bool wxSocketImpl::PreCreateCheck(GAddress *addr)
187 {
188 if ( m_fd != INVALID_SOCKET )
189 {
190 m_error = wxSOCKET_INVSOCK;
191 return false;
192 }
193
194 if ( !addr || !addr->m_addr )
195 {
196 m_error = wxSOCKET_INVADDR;
197 return false;
198 }
199
200 return true;
201 }
202
203 void wxSocketImpl::PostCreation()
204 {
205 // FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option
206 #ifdef SO_NOSIGPIPE
207 EnableSocketOption(SO_NOSIGPIPE);
208 #endif
209
210 if ( m_reusable )
211 EnableSocketOption(SO_REUSEADDR);
212
213 if ( m_broadcast )
214 {
215 wxASSERT_MSG( !m_stream, "broadcasting is for datagram sockets only" );
216
217 EnableSocketOption(SO_BROADCAST);
218 }
219
220 if ( m_initialRecvBufferSize >= 0 )
221 SetSocketOption(SO_RCVBUF, m_initialRecvBufferSize);
222 if ( m_initialSendBufferSize >= 0 )
223 SetSocketOption(SO_SNDBUF, m_initialSendBufferSize);
224
225 // we always put our sockets in unblocked mode and handle blocking
226 // ourselves in DoRead/Write() if wxSOCKET_WAITALL is specified
227 UnblockAndRegisterWithEventLoop();
228 }
229
230 wxSocketError wxSocketImpl::UpdateLocalAddress()
231 {
232 WX_SOCKLEN_T lenAddr = sizeof(*m_local->m_addr);
233 if ( getsockname(m_fd, m_local->m_addr, &lenAddr) != 0 )
234 {
235 Close();
236 m_error = wxSOCKET_IOERR;
237 return m_error;
238 }
239
240 m_local->m_len = lenAddr;
241
242 return wxSOCKET_NOERROR;
243 }
244
245 wxSocketError wxSocketImpl::CreateServer()
246 {
247 if ( !PreCreateCheck(m_local) )
248 return m_error;
249
250 m_server = true;
251 m_stream = true;
252
253 // do create the socket
254 m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0);
255
256 if ( m_fd == INVALID_SOCKET )
257 {
258 m_error = wxSOCKET_IOERR;
259 return wxSOCKET_IOERR;
260 }
261
262 PostCreation();
263
264 // and then bind to and listen on it
265 //
266 // FIXME: should we test for m_dobind here?
267 if ( bind(m_fd, m_local->m_addr, m_local->m_len) != 0 )
268 m_error = wxSOCKET_IOERR;
269
270 if ( IsOk() )
271 {
272 if ( listen(m_fd, 5) != 0 )
273 m_error = wxSOCKET_IOERR;
274 }
275
276 if ( !IsOk() )
277 {
278 Close();
279 return m_error;
280 }
281
282 // finally retrieve the address we effectively bound to
283 return UpdateLocalAddress();
284 }
285
286 wxSocketError wxSocketImpl::CreateClient(bool wait)
287 {
288 if ( !PreCreateCheck(m_peer) )
289 return m_error;
290
291 m_fd = socket(m_peer->m_realfamily, SOCK_STREAM, 0);
292
293 if ( m_fd == INVALID_SOCKET )
294 {
295 m_error = wxSOCKET_IOERR;
296 return wxSOCKET_IOERR;
297 }
298
299 PostCreation();
300
301 // If a local address has been set, then bind to it before calling connect
302 if ( m_local && m_local->m_addr )
303 {
304 if ( bind(m_fd, m_local->m_addr, m_local->m_len) != 0 )
305 {
306 Close();
307 m_error = wxSOCKET_IOERR;
308 return m_error;
309 }
310 }
311
312 // Do connect now
313 int rc = connect(m_fd, m_peer->m_addr, m_peer->m_len);
314 if ( rc == SOCKET_ERROR )
315 {
316 wxSocketError err = GetLastError();
317 if ( err == wxSOCKET_WOULDBLOCK )
318 {
319 m_establishing = true;
320
321 // block waiting for connection if we should (otherwise just return
322 // wxSOCKET_WOULDBLOCK to the caller)
323 if ( wait )
324 {
325 err = SelectWithTimeout(wxSOCKET_CONNECTION_FLAG)
326 ? wxSOCKET_NOERROR
327 : wxSOCKET_TIMEDOUT;
328 m_establishing = false;
329 }
330 }
331
332 m_error = err;
333 }
334 else // connected
335 {
336 m_error = wxSOCKET_NOERROR;
337 }
338
339 return m_error;
340 }
341
342
343 wxSocketError wxSocketImpl::CreateUDP()
344 {
345 if ( !PreCreateCheck(m_local) )
346 return m_error;
347
348 m_stream = false;
349 m_server = false;
350
351 m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0);
352
353 if ( m_fd == INVALID_SOCKET )
354 {
355 m_error = wxSOCKET_IOERR;
356 return wxSOCKET_IOERR;
357 }
358
359 PostCreation();
360
361 if ( m_dobind )
362 {
363 if ( bind(m_fd, m_local->m_addr, m_local->m_len) != 0 )
364 {
365 Close();
366 m_error = wxSOCKET_IOERR;
367 return m_error;
368 }
369
370 return UpdateLocalAddress();
371 }
372
373 return wxSOCKET_NOERROR;
374 }
375
376 wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket)
377 {
378 wxSockAddr from;
379 WX_SOCKLEN_T fromlen = sizeof(from);
380 const int fd = accept(m_fd, &from, &fromlen);
381
382 if ( fd == INVALID_SOCKET )
383 return NULL;
384
385 wxSocketImpl * const sock = Create(wxsocket);
386 sock->m_fd = fd;
387
388 sock->m_peer = GAddress_new();
389 _GAddress_translate_from(sock->m_peer, &from, fromlen);
390
391 sock->UnblockAndRegisterWithEventLoop();
392
393 return sock;
394 }
395
396
397 void wxSocketImpl::Close()
398 {
399 if ( m_fd != INVALID_SOCKET )
400 {
401 DoClose();
402 m_fd = INVALID_SOCKET;
403 }
404 }
405
406 /*
407 * Disallow further read/write operations on this socket, close
408 * the fd and disable all callbacks.
409 */
410 void wxSocketImpl::Shutdown()
411 {
412 if ( m_fd != INVALID_SOCKET )
413 {
414 shutdown(m_fd, 1 /* SD_SEND */);
415 Close();
416 }
417
418 m_detected = wxSOCKET_LOST_FLAG;
419 }
420
421 /*
422 * Sets the timeout for blocking calls. Time is expressed in
423 * milliseconds.
424 */
425 void wxSocketImpl::SetTimeout(unsigned long millis)
426 {
427 SetTimeValFromMS(m_timeout, millis);
428 }
429
430 void wxSocketImpl::NotifyOnStateChange(wxSocketNotify event)
431 {
432 m_wxsocket->OnRequest(event);
433 }
434
435 /* Address handling */
436
437 /*
438 * Set or get the local or peer address for this socket. The 'set'
439 * functions return wxSOCKET_NOERROR on success, an error code otherwise.
440 * The 'get' functions return a pointer to a GAddress object on success,
441 * or NULL otherwise, in which case they set the error code of the
442 * corresponding socket.
443 *
444 * Error codes:
445 * wxSOCKET_INVSOCK - the socket is not valid.
446 * wxSOCKET_INVADDR - the address is not valid.
447 */
448 wxSocketError wxSocketImpl::SetLocal(GAddress *address)
449 {
450 /* the socket must be initialized, or it must be a server */
451 if (m_fd != INVALID_SOCKET && !m_server)
452 {
453 m_error = wxSOCKET_INVSOCK;
454 return wxSOCKET_INVSOCK;
455 }
456
457 /* check address */
458 if (address == NULL || address->m_family == wxSOCKET_NOFAMILY)
459 {
460 m_error = wxSOCKET_INVADDR;
461 return wxSOCKET_INVADDR;
462 }
463
464 if (m_local)
465 GAddress_destroy(m_local);
466
467 m_local = GAddress_copy(address);
468
469 return wxSOCKET_NOERROR;
470 }
471
472 wxSocketError wxSocketImpl::SetPeer(GAddress *address)
473 {
474 /* check address */
475 if (address == NULL || address->m_family == wxSOCKET_NOFAMILY)
476 {
477 m_error = wxSOCKET_INVADDR;
478 return wxSOCKET_INVADDR;
479 }
480
481 if (m_peer)
482 GAddress_destroy(m_peer);
483
484 m_peer = GAddress_copy(address);
485
486 return wxSOCKET_NOERROR;
487 }
488
489 GAddress *wxSocketImpl::GetLocal()
490 {
491 GAddress *address;
492 wxSockAddr addr;
493 WX_SOCKLEN_T size = sizeof(addr);
494 wxSocketError err;
495
496 /* try to get it from the m_local var first */
497 if (m_local)
498 return GAddress_copy(m_local);
499
500 /* else, if the socket is initialized, try getsockname */
501 if (m_fd == INVALID_SOCKET)
502 {
503 m_error = wxSOCKET_INVSOCK;
504 return NULL;
505 }
506
507 if (getsockname(m_fd, (sockaddr*)&addr, &size) == SOCKET_ERROR)
508 {
509 m_error = wxSOCKET_IOERR;
510 return NULL;
511 }
512
513 /* got a valid address from getsockname, create a GAddress object */
514 if ((address = GAddress_new()) == NULL)
515 {
516 m_error = wxSOCKET_MEMERR;
517 return NULL;
518 }
519
520 if ((err = _GAddress_translate_from(address, (sockaddr*)&addr, size)) != wxSOCKET_NOERROR)
521 {
522 GAddress_destroy(address);
523 m_error = err;
524 return NULL;
525 }
526
527 return address;
528 }
529
530 GAddress *wxSocketImpl::GetPeer()
531 {
532 /* try to get it from the m_peer var */
533 if (m_peer)
534 return GAddress_copy(m_peer);
535
536 return NULL;
537 }
538
539 // ==========================================================================
540 // wxSocketBase
541 // ==========================================================================
542
543 // --------------------------------------------------------------------------
544 // Initialization and shutdown
545 // --------------------------------------------------------------------------
546
547 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
548 // to m_countInit with a crit section
549 size_t wxSocketBase::m_countInit = 0;
550
551 bool wxSocketBase::IsInitialized()
552 {
553 return m_countInit > 0;
554 }
555
556 bool wxSocketBase::Initialize()
557 {
558 if ( !m_countInit++ )
559 {
560 wxSocketManager * const manager = wxSocketManager::Get();
561 if ( !manager || !manager->OnInit() )
562 {
563 m_countInit--;
564
565 return false;
566 }
567 }
568
569 return true;
570 }
571
572 void wxSocketBase::Shutdown()
573 {
574 // we should be initialized
575 wxASSERT_MSG( m_countInit > 0, _T("extra call to Shutdown()") );
576 if ( --m_countInit == 0 )
577 {
578 wxSocketManager * const manager = wxSocketManager::Get();
579 wxCHECK_RET( manager, "should have a socket manager" );
580
581 manager->OnExit();
582 }
583 }
584
585 // --------------------------------------------------------------------------
586 // Ctor and dtor
587 // --------------------------------------------------------------------------
588
589 void wxSocketBase::Init()
590 {
591 m_impl = NULL;
592 m_type = wxSOCKET_UNINIT;
593
594 // state
595 m_flags = 0;
596 m_connected =
597 m_establishing =
598 m_reading =
599 m_writing =
600 m_error =
601 m_closed = false;
602 m_lcount = 0;
603 m_timeout = 600;
604 m_beingDeleted = false;
605
606 // pushback buffer
607 m_unread = NULL;
608 m_unrd_size = 0;
609 m_unrd_cur = 0;
610
611 // events
612 m_id = wxID_ANY;
613 m_handler = NULL;
614 m_clientData = NULL;
615 m_notify = false;
616 m_eventmask =
617 m_eventsgot = 0;
618
619 if ( !IsInitialized() )
620 {
621 // this Initialize() will be undone by wxSocketModule::OnExit(), all
622 // the other calls to it should be matched by a call to Shutdown()
623 Initialize();
624 }
625 }
626
627 wxSocketBase::wxSocketBase()
628 {
629 Init();
630 }
631
632 wxSocketBase::wxSocketBase(wxSocketFlags flags, wxSocketType type)
633 {
634 Init();
635
636 SetFlags(flags);
637
638 m_type = type;
639 }
640
641 wxSocketBase::~wxSocketBase()
642 {
643 // Just in case the app called Destroy() *and* then deleted the socket
644 // immediately: don't leave dangling pointers.
645 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
646 if ( traits )
647 traits->RemoveFromPendingDelete(this);
648
649 // Shutdown and close the socket
650 if (!m_beingDeleted)
651 Close();
652
653 // Destroy the implementation object
654 delete m_impl;
655
656 // Free the pushback buffer
657 if (m_unread)
658 free(m_unread);
659 }
660
661 bool wxSocketBase::Destroy()
662 {
663 // Delayed destruction: the socket will be deleted during the next idle
664 // loop iteration. This ensures that all pending events have been
665 // processed.
666 m_beingDeleted = true;
667
668 // Shutdown and close the socket
669 Close();
670
671 // Suppress events from now on
672 Notify(false);
673
674 // schedule this object for deletion
675 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
676 if ( traits )
677 {
678 // let the traits object decide what to do with us
679 traits->ScheduleForDestroy(this);
680 }
681 else // no app or no traits
682 {
683 // in wxBase we might have no app object at all, don't leak memory
684 delete this;
685 }
686
687 return true;
688 }
689
690 // ----------------------------------------------------------------------------
691 // simply accessors
692 // ----------------------------------------------------------------------------
693
694 wxSocketError wxSocketBase::LastError() const
695 {
696 return m_impl->GetError();
697 }
698
699 // --------------------------------------------------------------------------
700 // Basic IO calls
701 // --------------------------------------------------------------------------
702
703 // The following IO operations update m_error and m_lcount:
704 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
705 bool wxSocketBase::Close()
706 {
707 // Interrupt pending waits
708 InterruptWait();
709
710 if (m_impl)
711 m_impl->Shutdown();
712
713 m_connected = false;
714 m_establishing = false;
715 return true;
716 }
717
718 wxSocketBase& wxSocketBase::Read(void* buffer, wxUint32 nbytes)
719 {
720 // Mask read events
721 m_reading = true;
722
723 m_lcount = DoRead(buffer, nbytes);
724
725 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
726 if (m_flags & wxSOCKET_WAITALL)
727 m_error = (m_lcount != nbytes);
728 else
729 m_error = (m_lcount == 0);
730
731 // Allow read events from now on
732 m_reading = false;
733
734 return *this;
735 }
736
737 wxUint32 wxSocketBase::DoRead(void* buffer_, wxUint32 nbytes)
738 {
739 // We use pointer arithmetic here which doesn't work with void pointers.
740 char *buffer = static_cast<char *>(buffer_);
741
742 // Try the push back buffer first, even before checking whether the socket
743 // is valid to allow reading previously pushed back data from an already
744 // closed socket.
745 wxUint32 total = GetPushback(buffer, nbytes, false);
746 nbytes -= total;
747 buffer += total;
748
749 // If it's indeed closed or if read everything, there is nothing more to do.
750 if ( !m_impl || !nbytes )
751 return total;
752
753 wxCHECK_MSG( buffer, 0, "NULL buffer" );
754
755
756 // wxSOCKET_NOWAIT overrides all the other flags and means that we are
757 // polling the socket and don't block at all.
758 if ( m_flags & wxSOCKET_NOWAIT )
759 {
760 int ret = m_impl->Read(buffer, nbytes);
761 if ( ret < 0 )
762 return 0;
763
764 total += ret;
765 }
766 else // blocking socket
767 {
768 for ( ;; )
769 {
770 // Wait until socket becomes ready for reading
771 if ( !WaitForRead() )
772 break;
773
774 const int ret = m_impl->Read(buffer, nbytes);
775 if ( ret == 0 )
776 {
777 // for connection-oriented (e.g. TCP) sockets we can only read
778 // 0 bytes if the other end has been closed, and for
779 // connectionless ones (UDP) this flag doesn't make sense
780 // anyhow so we can set it to true too without doing any harm
781 m_closed = true;
782 break;
783 }
784
785 if ( ret < 0 )
786 {
787 // this will be always interpreted as error by Read()
788 return 0;
789 }
790
791 total += ret;
792
793 // If wxSOCKET_WAITALL is not set, we can leave now as we did read
794 // something and we don't need to wait for all nbytes bytes to be
795 // read.
796 if ( !(m_flags & wxSOCKET_WAITALL) )
797 break;
798
799 // Otherwise continue reading until we do read everything.
800 nbytes -= ret;
801 if ( !nbytes )
802 break;
803
804 buffer += ret;
805 }
806 }
807
808 return total;
809 }
810
811 wxSocketBase& wxSocketBase::ReadMsg(void* buffer, wxUint32 nbytes)
812 {
813 wxUint32 len, len2, sig, total;
814 bool error;
815 int old_flags;
816 struct
817 {
818 unsigned char sig[4];
819 unsigned char len[4];
820 } msg;
821
822 // Mask read events
823 m_reading = true;
824
825 total = 0;
826 error = true;
827 old_flags = m_flags;
828 SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL);
829
830 if (DoRead(&msg, sizeof(msg)) != sizeof(msg))
831 goto exit;
832
833 sig = (wxUint32)msg.sig[0];
834 sig |= (wxUint32)(msg.sig[1] << 8);
835 sig |= (wxUint32)(msg.sig[2] << 16);
836 sig |= (wxUint32)(msg.sig[3] << 24);
837
838 if (sig != 0xfeeddead)
839 {
840 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
841 goto exit;
842 }
843
844 len = (wxUint32)msg.len[0];
845 len |= (wxUint32)(msg.len[1] << 8);
846 len |= (wxUint32)(msg.len[2] << 16);
847 len |= (wxUint32)(msg.len[3] << 24);
848
849 if (len > nbytes)
850 {
851 len2 = len - nbytes;
852 len = nbytes;
853 }
854 else
855 len2 = 0;
856
857 // Don't attempt to read if the msg was zero bytes long.
858 if (len)
859 {
860 total = DoRead(buffer, len);
861
862 if (total != len)
863 goto exit;
864 }
865
866 if (len2)
867 {
868 char *discard_buffer = new char[MAX_DISCARD_SIZE];
869 long discard_len;
870
871 // NOTE: discarded bytes don't add to m_lcount.
872 do
873 {
874 discard_len = ((len2 > MAX_DISCARD_SIZE)? MAX_DISCARD_SIZE : len2);
875 discard_len = DoRead(discard_buffer, (wxUint32)discard_len);
876 len2 -= (wxUint32)discard_len;
877 }
878 while ((discard_len > 0) && len2);
879
880 delete [] discard_buffer;
881
882 if (len2 != 0)
883 goto exit;
884 }
885 if (DoRead(&msg, sizeof(msg)) != sizeof(msg))
886 goto exit;
887
888 sig = (wxUint32)msg.sig[0];
889 sig |= (wxUint32)(msg.sig[1] << 8);
890 sig |= (wxUint32)(msg.sig[2] << 16);
891 sig |= (wxUint32)(msg.sig[3] << 24);
892
893 if (sig != 0xdeadfeed)
894 {
895 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
896 goto exit;
897 }
898
899 // everything was OK
900 error = false;
901
902 exit:
903 m_error = error;
904 m_lcount = total;
905 m_reading = false;
906 SetFlags(old_flags);
907
908 return *this;
909 }
910
911 wxSocketBase& wxSocketBase::Peek(void* buffer, wxUint32 nbytes)
912 {
913 // Mask read events
914 m_reading = true;
915
916 m_lcount = DoRead(buffer, nbytes);
917 Pushback(buffer, m_lcount);
918
919 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
920 if (m_flags & wxSOCKET_WAITALL)
921 m_error = (m_lcount != nbytes);
922 else
923 m_error = (m_lcount == 0);
924
925 // Allow read events again
926 m_reading = false;
927
928 return *this;
929 }
930
931 wxSocketBase& wxSocketBase::Write(const void *buffer, wxUint32 nbytes)
932 {
933 // Mask write events
934 m_writing = true;
935
936 m_lcount = DoWrite(buffer, nbytes);
937
938 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
939 if (m_flags & wxSOCKET_WAITALL)
940 m_error = (m_lcount != nbytes);
941 else
942 m_error = (m_lcount == 0);
943
944 // Allow write events again
945 m_writing = false;
946
947 return *this;
948 }
949
950 // This function is a mirror image of DoRead() except that it doesn't use the
951 // push back buffer, please see comments there
952 wxUint32 wxSocketBase::DoWrite(const void *buffer_, wxUint32 nbytes)
953 {
954 const char *buffer = static_cast<const char *>(buffer_);
955
956 // Return if there is nothing to read or the socket is (already?) closed.
957 if ( !m_impl || !nbytes )
958 return 0;
959
960 wxCHECK_MSG( buffer, 0, "NULL buffer" );
961
962 wxUint32 total = 0;
963 if ( m_flags & wxSOCKET_NOWAIT )
964 {
965 const int ret = m_impl->Write(buffer, nbytes);
966 if ( ret > 0 )
967 total += ret;
968 }
969 else // blocking socket
970 {
971 for ( ;; )
972 {
973 if ( !WaitForWrite() )
974 break;
975
976 const int ret = m_impl->Write(buffer, nbytes);
977 if ( ret == 0 )
978 {
979 m_closed = true;
980 break;
981 }
982
983 if ( ret < 0 )
984 return 0;
985
986 total += ret;
987 if ( !(m_flags & wxSOCKET_WAITALL) )
988 break;
989
990 nbytes -= ret;
991 if ( !nbytes )
992 break;
993
994 buffer += ret;
995 }
996 }
997
998 return total;
999 }
1000
1001 wxSocketBase& wxSocketBase::WriteMsg(const void *buffer, wxUint32 nbytes)
1002 {
1003 wxUint32 total;
1004 bool error;
1005 struct
1006 {
1007 unsigned char sig[4];
1008 unsigned char len[4];
1009 } msg;
1010
1011 // Mask write events
1012 m_writing = true;
1013
1014 error = true;
1015 total = 0;
1016 SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL);
1017
1018 msg.sig[0] = (unsigned char) 0xad;
1019 msg.sig[1] = (unsigned char) 0xde;
1020 msg.sig[2] = (unsigned char) 0xed;
1021 msg.sig[3] = (unsigned char) 0xfe;
1022
1023 msg.len[0] = (unsigned char) (nbytes & 0xff);
1024 msg.len[1] = (unsigned char) ((nbytes >> 8) & 0xff);
1025 msg.len[2] = (unsigned char) ((nbytes >> 16) & 0xff);
1026 msg.len[3] = (unsigned char) ((nbytes >> 24) & 0xff);
1027
1028 if (DoWrite(&msg, sizeof(msg)) < sizeof(msg))
1029 goto exit;
1030
1031 total = DoWrite(buffer, nbytes);
1032
1033 if (total < nbytes)
1034 goto exit;
1035
1036 msg.sig[0] = (unsigned char) 0xed;
1037 msg.sig[1] = (unsigned char) 0xfe;
1038 msg.sig[2] = (unsigned char) 0xad;
1039 msg.sig[3] = (unsigned char) 0xde;
1040 msg.len[0] =
1041 msg.len[1] =
1042 msg.len[2] =
1043 msg.len[3] = (char) 0;
1044
1045 if ((DoWrite(&msg, sizeof(msg))) < sizeof(msg))
1046 goto exit;
1047
1048 // everything was OK
1049 error = false;
1050
1051 exit:
1052 m_error = error;
1053 m_lcount = total;
1054 m_writing = false;
1055
1056 return *this;
1057 }
1058
1059 wxSocketBase& wxSocketBase::Unread(const void *buffer, wxUint32 nbytes)
1060 {
1061 if (nbytes != 0)
1062 Pushback(buffer, nbytes);
1063
1064 m_error = false;
1065 m_lcount = nbytes;
1066
1067 return *this;
1068 }
1069
1070 wxSocketBase& wxSocketBase::Discard()
1071 {
1072 char *buffer = new char[MAX_DISCARD_SIZE];
1073 wxUint32 ret;
1074 wxUint32 total = 0;
1075
1076 // Mask read events
1077 m_reading = true;
1078
1079 SetFlags(wxSOCKET_NOWAIT);
1080
1081 do
1082 {
1083 ret = DoRead(buffer, MAX_DISCARD_SIZE);
1084 total += ret;
1085 }
1086 while (ret == MAX_DISCARD_SIZE);
1087
1088 delete[] buffer;
1089 m_lcount = total;
1090 m_error = false;
1091
1092 // Allow read events again
1093 m_reading = false;
1094
1095 return *this;
1096 }
1097
1098 // --------------------------------------------------------------------------
1099 // Wait functions
1100 // --------------------------------------------------------------------------
1101
1102 /*
1103 This function will check for the events specified in the flags parameter,
1104 and it will return a mask indicating which operations can be performed.
1105 */
1106 wxSocketEventFlags wxSocketImpl::Select(wxSocketEventFlags flags,
1107 const timeval *timeout)
1108 {
1109 wxSocketEventFlags result = 0;
1110
1111 if (m_fd == INVALID_SOCKET)
1112 return (wxSOCKET_LOST_FLAG & flags);
1113
1114 struct timeval tv;
1115 if ( timeout )
1116 tv = *timeout;
1117 else
1118 tv.tv_sec = tv.tv_usec = 0;
1119
1120 fd_set readfds;
1121 fd_set writefds;
1122 fd_set exceptfds;
1123 wxFD_ZERO(&readfds);
1124 wxFD_ZERO(&writefds);
1125 wxFD_ZERO(&exceptfds);
1126 wxFD_SET(m_fd, &readfds);
1127 if (flags & wxSOCKET_OUTPUT_FLAG || flags & wxSOCKET_CONNECTION_FLAG)
1128 wxFD_SET(m_fd, &writefds);
1129 wxFD_SET(m_fd, &exceptfds);
1130
1131 /* Check 'sticky' CONNECTION flag first */
1132 result |= wxSOCKET_CONNECTION_FLAG & m_detected;
1133
1134 /* If we have already detected a LOST event, then don't try
1135 * to do any further processing.
1136 */
1137 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
1138 {
1139 m_establishing = false;
1140 return (wxSOCKET_LOST_FLAG & flags);
1141 }
1142
1143 /* Try select now */
1144 if (select(m_fd + 1, &readfds, &writefds, &exceptfds, &tv) < 0)
1145 {
1146 /* What to do here? */
1147 return (result & flags);
1148 }
1149
1150 /* Check for exceptions and errors */
1151 if (wxFD_ISSET(m_fd, &exceptfds))
1152 {
1153 m_establishing = false;
1154 m_detected = wxSOCKET_LOST_FLAG;
1155
1156 /* LOST event: Abort any further processing */
1157 return (wxSOCKET_LOST_FLAG & flags);
1158 }
1159
1160 /* Check for readability */
1161 if (wxFD_ISSET(m_fd, &readfds))
1162 {
1163 result |= wxSOCKET_INPUT_FLAG;
1164
1165 if (m_server && m_stream)
1166 {
1167 /* This is a TCP server socket that detected a connection.
1168 While the INPUT_FLAG is also set, it doesn't matter on
1169 this kind of sockets, as we can only Accept() from them. */
1170 m_detected |= wxSOCKET_CONNECTION_FLAG;
1171 }
1172 }
1173
1174 /* Check for writability */
1175 if (wxFD_ISSET(m_fd, &writefds))
1176 {
1177 if (m_establishing && !m_server)
1178 {
1179 int error;
1180 SOCKOPTLEN_T len = sizeof(error);
1181 m_establishing = false;
1182 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
1183
1184 if (error)
1185 {
1186 m_detected = wxSOCKET_LOST_FLAG;
1187
1188 /* LOST event: Abort any further processing */
1189 return (wxSOCKET_LOST_FLAG & flags);
1190 }
1191 else
1192 {
1193 m_detected |= wxSOCKET_CONNECTION_FLAG;
1194 }
1195 }
1196 else
1197 {
1198 result |= wxSOCKET_OUTPUT_FLAG;
1199 }
1200 }
1201
1202 return (result | m_detected) & flags;
1203 }
1204
1205 bool
1206 wxSocketBase::DoWait(long seconds, long milliseconds, wxSocketEventFlags flags)
1207 {
1208 wxCHECK_MSG( m_impl, false, "can't wait on invalid socket" );
1209
1210 // This can be set to true from Interrupt() to exit this function a.s.a.p.
1211 m_interrupt = false;
1212
1213
1214 // Use either the provided timeout or the default timeout value associated
1215 // with this socket.
1216 //
1217 // TODO: allow waiting forever, see #9443
1218 const long timeout = seconds == -1 ? m_timeout * 1000
1219 : seconds * 1000 + milliseconds;
1220 const wxMilliClock_t timeEnd = wxGetLocalTimeMillis() + timeout;
1221
1222 // Get the active event loop which we'll use for the message dispatching
1223 // when running in the main thread unless this was explicitly disabled by
1224 // setting wxSOCKET_BLOCK flag
1225 wxEventLoopBase *eventLoop;
1226 if ( !(m_flags & wxSOCKET_BLOCK) && wxIsMainThread() )
1227 {
1228 eventLoop = wxEventLoop::GetActive();
1229 }
1230 else // in worker thread
1231 {
1232 // We never dispatch messages from threads other than the main one.
1233 eventLoop = NULL;
1234 }
1235
1236 // Wait until we receive the event we're waiting for or the timeout expires
1237 // (but note that we always execute the loop at least once, even if timeout
1238 // is 0 as this is used for polling)
1239 bool gotEvent = false;
1240 for ( bool firstTime = true; !m_interrupt ; firstTime = false )
1241 {
1242 long timeLeft = wxMilliClockToLong(timeEnd - wxGetLocalTimeMillis());
1243 if ( timeLeft < 0 )
1244 {
1245 if ( !firstTime )
1246 break; // timed out
1247
1248 timeLeft = 0;
1249 }
1250
1251 // This function is only called if wxSOCKET_BLOCK flag was not used and
1252 // so we should dispatch the events if there is an event loop capable
1253 // of doing it.
1254 wxSocketEventFlags events;
1255 if ( eventLoop )
1256 {
1257 // reset them before starting to wait
1258 m_eventsgot = 0;
1259
1260 eventLoop->DispatchTimeout(timeLeft);
1261
1262 events = m_eventsgot;
1263 }
1264 else // no event loop or waiting in another thread
1265 {
1266 // as explained below, we should always check for wxSOCKET_LOST_FLAG
1267 timeval tv;
1268 SetTimeValFromMS(tv, timeLeft);
1269 events = m_impl->Select(flags | wxSOCKET_LOST_FLAG, &tv);
1270 }
1271
1272 // always check for wxSOCKET_LOST_FLAG, even if flags doesn't include
1273 // it, as continuing to wait for anything else after getting it is
1274 // pointless
1275 if ( events & wxSOCKET_LOST_FLAG )
1276 {
1277 m_connected = false;
1278 m_establishing = false;
1279 if ( flags & wxSOCKET_LOST_FLAG )
1280 gotEvent = true;
1281 break;
1282 }
1283
1284 // otherwise mask out the bits we're not interested in
1285 events &= flags;
1286
1287 // Incoming connection (server) or connection established (client)?
1288 if ( events & wxSOCKET_CONNECTION_FLAG )
1289 {
1290 m_connected = true;
1291 m_establishing = false;
1292 gotEvent = true;
1293 break;
1294 }
1295
1296 // Data available or output buffer ready?
1297 if ( (events & wxSOCKET_INPUT_FLAG) || (events & wxSOCKET_OUTPUT_FLAG) )
1298 {
1299 gotEvent = true;
1300 break;
1301 }
1302 }
1303
1304 return gotEvent;
1305 }
1306
1307 bool wxSocketBase::Wait(long seconds, long milliseconds)
1308 {
1309 return DoWait(seconds, milliseconds,
1310 wxSOCKET_INPUT_FLAG |
1311 wxSOCKET_OUTPUT_FLAG |
1312 wxSOCKET_CONNECTION_FLAG |
1313 wxSOCKET_LOST_FLAG
1314 );
1315 }
1316
1317 bool wxSocketBase::WaitForRead(long seconds, long milliseconds)
1318 {
1319 // Check pushback buffer before entering DoWait
1320 if ( m_unread )
1321 return true;
1322
1323 // Note that wxSOCKET_INPUT_LOST has to be explicitly passed to DoWait
1324 // because of the semantics of WaitForRead: a return value of true means
1325 // that a Read call will return immediately, not that there is
1326 // actually data to read.
1327 return DoWait(seconds, milliseconds, wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
1328 }
1329
1330
1331 bool wxSocketBase::WaitForWrite(long seconds, long milliseconds)
1332 {
1333 return DoWait(seconds, milliseconds, wxSOCKET_OUTPUT_FLAG | wxSOCKET_LOST_FLAG);
1334 }
1335
1336 bool wxSocketBase::WaitForLost(long seconds, long milliseconds)
1337 {
1338 return DoWait(seconds, milliseconds, wxSOCKET_LOST_FLAG);
1339 }
1340
1341 // --------------------------------------------------------------------------
1342 // Miscellaneous
1343 // --------------------------------------------------------------------------
1344
1345 //
1346 // Get local or peer address
1347 //
1348
1349 bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const
1350 {
1351 GAddress *peer;
1352
1353 if (!m_impl)
1354 return false;
1355
1356 peer = m_impl->GetPeer();
1357
1358 // copying a null address would just trigger an assert anyway
1359
1360 if (!peer)
1361 return false;
1362
1363 addr_man.SetAddress(peer);
1364 GAddress_destroy(peer);
1365
1366 return true;
1367 }
1368
1369 bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const
1370 {
1371 GAddress *local;
1372
1373 if (!m_impl)
1374 return false;
1375
1376 local = m_impl->GetLocal();
1377 addr_man.SetAddress(local);
1378 GAddress_destroy(local);
1379
1380 return true;
1381 }
1382
1383 //
1384 // Save and restore socket state
1385 //
1386
1387 void wxSocketBase::SaveState()
1388 {
1389 wxSocketState *state;
1390
1391 state = new wxSocketState();
1392
1393 state->m_flags = m_flags;
1394 state->m_notify = m_notify;
1395 state->m_eventmask = m_eventmask;
1396 state->m_clientData = m_clientData;
1397
1398 m_states.Append(state);
1399 }
1400
1401 void wxSocketBase::RestoreState()
1402 {
1403 wxList::compatibility_iterator node;
1404 wxSocketState *state;
1405
1406 node = m_states.GetLast();
1407 if (!node)
1408 return;
1409
1410 state = (wxSocketState *)node->GetData();
1411
1412 m_flags = state->m_flags;
1413 m_notify = state->m_notify;
1414 m_eventmask = state->m_eventmask;
1415 m_clientData = state->m_clientData;
1416
1417 m_states.Erase(node);
1418 delete state;
1419 }
1420
1421 //
1422 // Timeout and flags
1423 //
1424
1425 void wxSocketBase::SetTimeout(long seconds)
1426 {
1427 m_timeout = seconds;
1428
1429 if (m_impl)
1430 m_impl->SetTimeout(m_timeout * 1000);
1431 }
1432
1433 void wxSocketBase::SetFlags(wxSocketFlags flags)
1434 {
1435 // Do some sanity checking on the flags used: not all values can be used
1436 // together.
1437 wxASSERT_MSG( !(flags & wxSOCKET_NOWAIT) ||
1438 !(flags & (wxSOCKET_WAITALL | wxSOCKET_BLOCK)),
1439 "Using wxSOCKET_WAITALL or wxSOCKET_BLOCK with "
1440 "wxSOCKET_NOWAIT doesn't make sense" );
1441
1442 m_flags = flags;
1443 }
1444
1445
1446 // --------------------------------------------------------------------------
1447 // Event handling
1448 // --------------------------------------------------------------------------
1449
1450 void wxSocketBase::OnRequest(wxSocketNotify notification)
1451 {
1452 wxSocketEventFlags flag = 0;
1453 switch ( notification )
1454 {
1455 case wxSOCKET_INPUT:
1456 flag = wxSOCKET_INPUT_FLAG;
1457 break;
1458
1459 case wxSOCKET_OUTPUT:
1460 flag = wxSOCKET_OUTPUT_FLAG;
1461 break;
1462
1463 case wxSOCKET_CONNECTION:
1464 flag = wxSOCKET_CONNECTION_FLAG;
1465 break;
1466
1467 case wxSOCKET_LOST:
1468 flag = wxSOCKET_LOST_FLAG;
1469 break;
1470
1471 default:
1472 wxFAIL_MSG( "unknown wxSocket notification" );
1473 }
1474
1475 // if we lost the connection the socket is now closed
1476 if ( notification == wxSOCKET_LOST )
1477 m_closed = true;
1478
1479 // remember the events which were generated for this socket, we're going to
1480 // use this in DoWait()
1481 m_eventsgot |= flag;
1482
1483 // send the wx event if enabled and we're interested in it
1484 if ( m_notify && (m_eventmask & flag) && m_handler )
1485 {
1486 // If we are in the middle of a R/W operation, do not propagate events
1487 // to users. Also, filter 'late' events which are no longer valid.
1488 if ( notification == wxSOCKET_INPUT )
1489 {
1490 if ( m_reading || !m_impl->Select(wxSOCKET_INPUT_FLAG) )
1491 return;
1492 }
1493 else if ( notification == wxSOCKET_OUTPUT )
1494 {
1495 if ( m_writing || !m_impl->Select(wxSOCKET_OUTPUT_FLAG) )
1496 return;
1497 }
1498
1499 wxSocketEvent event(m_id);
1500 event.m_event = notification;
1501 event.m_clientData = m_clientData;
1502 event.SetEventObject(this);
1503
1504 m_handler->AddPendingEvent(event);
1505 }
1506 }
1507
1508 void wxSocketBase::Notify(bool notify)
1509 {
1510 m_notify = notify;
1511 }
1512
1513 void wxSocketBase::SetNotify(wxSocketEventFlags flags)
1514 {
1515 m_eventmask = flags;
1516 }
1517
1518 void wxSocketBase::SetEventHandler(wxEvtHandler& handler, int id)
1519 {
1520 m_handler = &handler;
1521 m_id = id;
1522 }
1523
1524 // --------------------------------------------------------------------------
1525 // Pushback buffer
1526 // --------------------------------------------------------------------------
1527
1528 void wxSocketBase::Pushback(const void *buffer, wxUint32 size)
1529 {
1530 if (!size) return;
1531
1532 if (m_unread == NULL)
1533 m_unread = malloc(size);
1534 else
1535 {
1536 void *tmp;
1537
1538 tmp = malloc(m_unrd_size + size);
1539 memcpy((char *)tmp + size, m_unread, m_unrd_size);
1540 free(m_unread);
1541
1542 m_unread = tmp;
1543 }
1544
1545 m_unrd_size += size;
1546
1547 memcpy(m_unread, buffer, size);
1548 }
1549
1550 wxUint32 wxSocketBase::GetPushback(void *buffer, wxUint32 size, bool peek)
1551 {
1552 wxCHECK_MSG( buffer, 0, "NULL buffer" );
1553
1554 if (!m_unrd_size)
1555 return 0;
1556
1557 if (size > (m_unrd_size-m_unrd_cur))
1558 size = m_unrd_size-m_unrd_cur;
1559
1560 memcpy(buffer, (char *)m_unread + m_unrd_cur, size);
1561
1562 if (!peek)
1563 {
1564 m_unrd_cur += size;
1565 if (m_unrd_size == m_unrd_cur)
1566 {
1567 free(m_unread);
1568 m_unread = NULL;
1569 m_unrd_size = 0;
1570 m_unrd_cur = 0;
1571 }
1572 }
1573
1574 return size;
1575 }
1576
1577
1578 // ==========================================================================
1579 // wxSocketServer
1580 // ==========================================================================
1581
1582 // --------------------------------------------------------------------------
1583 // Ctor
1584 // --------------------------------------------------------------------------
1585
1586 wxSocketServer::wxSocketServer(const wxSockAddress& addr_man,
1587 wxSocketFlags flags)
1588 : wxSocketBase(flags, wxSOCKET_SERVER)
1589 {
1590 wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") );
1591
1592 m_impl = wxSocketImpl::Create(*this);
1593
1594 if (!m_impl)
1595 {
1596 wxLogTrace( wxTRACE_Socket, _T("*** Failed to create m_impl") );
1597 return;
1598 }
1599
1600 // Setup the socket as server
1601 m_impl->SetLocal(addr_man.GetAddress());
1602
1603 if (GetFlags() & wxSOCKET_REUSEADDR) {
1604 m_impl->SetReusable();
1605 }
1606 if (GetFlags() & wxSOCKET_BROADCAST) {
1607 m_impl->SetBroadcast();
1608 }
1609 if (GetFlags() & wxSOCKET_NOBIND) {
1610 m_impl->DontDoBind();
1611 }
1612
1613 if (m_impl->CreateServer() != wxSOCKET_NOERROR)
1614 {
1615 delete m_impl;
1616 m_impl = NULL;
1617
1618 wxLogTrace( wxTRACE_Socket, _T("*** CreateServer() failed") );
1619 return;
1620 }
1621
1622 wxLogTrace( wxTRACE_Socket, _T("wxSocketServer on fd %d"), m_impl->m_fd );
1623 }
1624
1625 // --------------------------------------------------------------------------
1626 // Accept
1627 // --------------------------------------------------------------------------
1628
1629 bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait)
1630 {
1631 if ( !m_impl || (m_impl->m_fd == INVALID_SOCKET) || !m_impl->IsServer() )
1632 {
1633 wxFAIL_MSG( "can only be called for a valid server socket" );
1634
1635 m_error = wxSOCKET_INVSOCK;
1636
1637 return false;
1638 }
1639
1640 if ( wait )
1641 {
1642 // wait until we get a connection
1643 if ( !m_impl->SelectWithTimeout(wxSOCKET_INPUT_FLAG) )
1644 {
1645 m_error = wxSOCKET_TIMEDOUT;
1646
1647 return false;
1648 }
1649 }
1650
1651 sock.m_impl = m_impl->Accept(sock);
1652
1653 if ( !sock.m_impl )
1654 {
1655 m_error = m_impl->GetLastError();
1656
1657 return false;
1658 }
1659
1660 sock.m_type = wxSOCKET_BASE;
1661 sock.m_connected = true;
1662
1663 return true;
1664 }
1665
1666 wxSocketBase *wxSocketServer::Accept(bool wait)
1667 {
1668 wxSocketBase* sock = new wxSocketBase();
1669
1670 sock->SetFlags(m_flags);
1671
1672 if (!AcceptWith(*sock, wait))
1673 {
1674 sock->Destroy();
1675 sock = NULL;
1676 }
1677
1678 return sock;
1679 }
1680
1681 bool wxSocketServer::WaitForAccept(long seconds, long milliseconds)
1682 {
1683 return DoWait(seconds, milliseconds, wxSOCKET_CONNECTION_FLAG);
1684 }
1685
1686 bool wxSocketBase::GetOption(int level, int optname, void *optval, int *optlen)
1687 {
1688 wxASSERT_MSG( m_impl, _T("Socket not initialised") );
1689
1690 SOCKOPTLEN_T lenreal = *optlen;
1691 if ( getsockopt(m_impl->m_fd, level, optname,
1692 static_cast<char *>(optval), &lenreal) != 0 )
1693 return false;
1694
1695 *optlen = lenreal;
1696
1697 return true;
1698 }
1699
1700 bool
1701 wxSocketBase::SetOption(int level, int optname, const void *optval, int optlen)
1702 {
1703 wxASSERT_MSG( m_impl, _T("Socket not initialised") );
1704
1705 return setsockopt(m_impl->m_fd, level, optname,
1706 static_cast<const char *>(optval), optlen) == 0;
1707 }
1708
1709 bool wxSocketBase::SetLocal(const wxIPV4address& local)
1710 {
1711 GAddress* la = local.GetAddress();
1712
1713 // If the address is valid, save it for use when we call Connect
1714 if (la && la->m_addr)
1715 {
1716 m_localAddress = local;
1717
1718 return true;
1719 }
1720
1721 return false;
1722 }
1723
1724 // ==========================================================================
1725 // wxSocketClient
1726 // ==========================================================================
1727
1728 // --------------------------------------------------------------------------
1729 // Ctor and dtor
1730 // --------------------------------------------------------------------------
1731
1732 wxSocketClient::wxSocketClient(wxSocketFlags flags)
1733 : wxSocketBase(flags, wxSOCKET_CLIENT)
1734 {
1735 m_initialRecvBufferSize =
1736 m_initialSendBufferSize = -1;
1737 }
1738
1739 wxSocketClient::~wxSocketClient()
1740 {
1741 }
1742
1743 // --------------------------------------------------------------------------
1744 // Connect
1745 // --------------------------------------------------------------------------
1746
1747 bool wxSocketClient::DoConnect(const wxSockAddress& remote,
1748 const wxSockAddress* local,
1749 bool wait)
1750 {
1751 if ( m_impl )
1752 {
1753 // Shutdown and destroy the old socket
1754 Close();
1755 delete m_impl;
1756 }
1757
1758 m_connected = false;
1759 m_establishing = false;
1760
1761 // Create and set up the new one
1762 m_impl = wxSocketImpl::Create(*this);
1763 if ( !m_impl )
1764 return false;
1765
1766 // Reuse makes sense for clients too, if we are trying to rebind to the same port
1767 if (GetFlags() & wxSOCKET_REUSEADDR)
1768 m_impl->SetReusable();
1769 if (GetFlags() & wxSOCKET_BROADCAST)
1770 m_impl->SetBroadcast();
1771 if (GetFlags() & wxSOCKET_NOBIND)
1772 m_impl->DontDoBind();
1773
1774 // Bind to the local IP address and port, when provided or if one had been
1775 // set before
1776 if ( !local && m_localAddress.GetAddress() )
1777 local = &m_localAddress;
1778
1779 if ( local )
1780 m_impl->SetLocal(local->GetAddress());
1781
1782 m_impl->SetInitialSocketBuffers(m_initialRecvBufferSize, m_initialSendBufferSize);
1783
1784 m_impl->SetPeer(remote.GetAddress());
1785
1786 // Finally do create the socket and connect to the peer
1787 const wxSocketError err = m_impl->CreateClient(wait);
1788
1789 if ( err != wxSOCKET_NOERROR )
1790 {
1791 if ( err == wxSOCKET_WOULDBLOCK )
1792 {
1793 wxASSERT_MSG( !wait, "shouldn't get this for blocking connect" );
1794
1795 m_establishing = true;
1796 }
1797
1798 return false;
1799 }
1800
1801 m_connected = true;
1802 return true;
1803 }
1804
1805 bool wxSocketClient::Connect(const wxSockAddress& remote, bool wait)
1806 {
1807 return DoConnect(remote, NULL, wait);
1808 }
1809
1810 bool wxSocketClient::Connect(const wxSockAddress& remote,
1811 const wxSockAddress& local,
1812 bool wait)
1813 {
1814 return DoConnect(remote, &local, wait);
1815 }
1816
1817 bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds)
1818 {
1819 if ( m_connected )
1820 {
1821 // this happens if the initial attempt to connect succeeded without
1822 // blocking
1823 return true;
1824 }
1825
1826 wxCHECK_MSG( m_establishing && m_impl, false,
1827 "No connection establishment attempt in progress" );
1828
1829 // we must specify wxSOCKET_LOST_FLAG here explicitly because we must return
1830 // true if the connection establishment process is finished, whether it is
1831 // over because we successfully connected or because we were not able to
1832 // connect
1833 return DoWait(seconds, milliseconds,
1834 wxSOCKET_CONNECTION_FLAG | wxSOCKET_LOST_FLAG);
1835 }
1836
1837 // ==========================================================================
1838 // wxDatagramSocket
1839 // ==========================================================================
1840
1841 wxDatagramSocket::wxDatagramSocket( const wxSockAddress& addr,
1842 wxSocketFlags flags )
1843 : wxSocketBase( flags, wxSOCKET_DATAGRAM )
1844 {
1845 // Create the socket
1846 m_impl = wxSocketImpl::Create(*this);
1847
1848 if (!m_impl)
1849 return;
1850
1851 // Setup the socket as non connection oriented
1852 m_impl->SetLocal(addr.GetAddress());
1853 if (flags & wxSOCKET_REUSEADDR)
1854 {
1855 m_impl->SetReusable();
1856 }
1857 if (GetFlags() & wxSOCKET_BROADCAST)
1858 {
1859 m_impl->SetBroadcast();
1860 }
1861 if (GetFlags() & wxSOCKET_NOBIND)
1862 {
1863 m_impl->DontDoBind();
1864 }
1865
1866 if ( m_impl->CreateUDP() != wxSOCKET_NOERROR )
1867 {
1868 delete m_impl;
1869 m_impl = NULL;
1870 return;
1871 }
1872
1873 // Initialize all stuff
1874 m_connected = false;
1875 m_establishing = false;
1876 }
1877
1878 wxDatagramSocket& wxDatagramSocket::RecvFrom( wxSockAddress& addr,
1879 void* buf,
1880 wxUint32 nBytes )
1881 {
1882 Read(buf, nBytes);
1883 GetPeer(addr);
1884 return (*this);
1885 }
1886
1887 wxDatagramSocket& wxDatagramSocket::SendTo( const wxSockAddress& addr,
1888 const void* buf,
1889 wxUint32 nBytes )
1890 {
1891 wxASSERT_MSG( m_impl, _T("Socket not initialised") );
1892
1893 m_impl->SetPeer(addr.GetAddress());
1894 Write(buf, nBytes);
1895 return (*this);
1896 }
1897
1898 // ==========================================================================
1899 // wxSocketModule
1900 // ==========================================================================
1901
1902 class wxSocketModule : public wxModule
1903 {
1904 public:
1905 virtual bool OnInit()
1906 {
1907 // wxSocketBase will call Initialize() itself only if sockets are
1908 // really used, don't do it from here
1909 return true;
1910 }
1911
1912 virtual void OnExit()
1913 {
1914 if ( wxSocketBase::IsInitialized() )
1915 wxSocketBase::Shutdown();
1916 }
1917
1918 private:
1919 DECLARE_DYNAMIC_CLASS(wxSocketModule)
1920 };
1921
1922 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule)
1923
1924 #endif // wxUSE_SOCKETS