1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:       src/common/socket.cpp 
   3 // Purpose:    Socket handler classes 
   4 // Authors:    Guilhem Lavaux, Guillermo Rodriguez Garcia 
   6 // Copyright:  (C) 1999-1997, Guilhem Lavaux 
   7 //             (C) 1999-2000, Guillermo Rodriguez Garcia 
   8 //             (C) 2008 Vadim Zeitlin 
  10 // License:    wxWindows licence 
  11 ///////////////////////////////////////////////////////////////////////////// 
  13 // ========================================================================== 
  15 // ========================================================================== 
  17 // For compilers that support precompilation, includes "wx.h". 
  18 #include "wx/wxprec.h" 
  26 #include "wx/socket.h" 
  29     #include "wx/object.h" 
  30     #include "wx/string.h" 
  37     #include "wx/module.h" 
  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" 
  46 #include "wx/private/fd.h" 
  47 #include "wx/private/socket.h" 
  49 // DLL options compatibility check: 
  51 WX_CHECK_BUILD_OPTIONS("wxNet") 
  53 // -------------------------------------------------------------------------- 
  54 // macros and constants 
  55 // -------------------------------------------------------------------------- 
  58 #define MAX_DISCARD_SIZE (10 * 1024) 
  60 #define wxTRACE_Socket _T("wxSocket") 
  62 // -------------------------------------------------------------------------- 
  64 // -------------------------------------------------------------------------- 
  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
) 
  72 // ---------------------------------------------------------------------------- 
  74 // ---------------------------------------------------------------------------- 
  79 void SetTimeValFromMS(timeval
& tv
, unsigned long ms
) 
  81     tv
.tv_sec  
= (ms 
/ 1000); 
  82     tv
.tv_usec 
= (ms 
% 1000) * 1000; 
  85 } // anonymous namespace 
  87 // -------------------------------------------------------------------------- 
  89 // -------------------------------------------------------------------------- 
  91 class wxSocketState 
: public wxObject
 
  94     wxSocketFlags            m_flags
; 
  95     wxSocketEventFlags       m_eventmask
; 
 100     wxSocketState() : wxObject() {} 
 102     DECLARE_NO_COPY_CLASS(wxSocketState
) 
 105 // ============================================================================ 
 107 // ============================================================================ 
 109 wxSocketManager 
*wxSocketManager::ms_manager 
= NULL
; 
 112 void wxSocketManager::Set(wxSocketManager 
*manager
) 
 114     wxASSERT_MSG( !ms_manager
, "too late to set manager now" ); 
 116     ms_manager 
= manager
; 
 120 void wxSocketManager::Init() 
 122     wxASSERT_MSG( !ms_manager
, "shouldn't be initialized twice" ); 
 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 
 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. 
 140     wxASSERT_MSG( wxIsMainThread(), 
 141                     "sockets must be initialized from the main thread" ); 
 143     wxAppConsole 
* const app 
= wxAppConsole::GetInstance(); 
 144     wxCHECK_RET( app
, "sockets can't be initialized without wxApp" ); 
 146     ms_manager 
= app
->GetTraits()->GetSocketManager(); 
 149 // ========================================================================== 
 151 // ========================================================================== 
 153 wxSocketImpl::wxSocketImpl(wxSocketBase
& wxsocket
) 
 154     : m_wxsocket(&wxsocket
) 
 156     m_fd              
= INVALID_SOCKET
; 
 159     m_error           
= wxSOCKET_NOERROR
; 
 163     SetTimeout(wxsocket
.GetTimeout() * 1000); 
 165     m_establishing    
= false; 
 169     m_initialRecvBufferSize 
= -1; 
 170     m_initialSendBufferSize 
= -1; 
 173 wxSocketImpl::~wxSocketImpl() 
 175     if (m_fd 
!= INVALID_SOCKET
) 
 179         GAddress_destroy(m_local
); 
 182         GAddress_destroy(m_peer
); 
 185 bool wxSocketImpl::PreCreateCheck(GAddress 
*addr
) 
 187     if ( m_fd 
!= INVALID_SOCKET 
) 
 189         m_error 
= wxSOCKET_INVSOCK
; 
 193     if ( !addr 
|| !addr
->m_addr 
) 
 195         m_error 
= wxSOCKET_INVADDR
; 
 202 void wxSocketImpl::PostCreation() 
 204     // FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option 
 206     EnableSocketOption(SO_NOSIGPIPE
); 
 210         EnableSocketOption(SO_REUSEADDR
); 
 214         wxASSERT_MSG( !m_stream
, "broadcasting is for datagram sockets only" ); 
 216         EnableSocketOption(SO_BROADCAST
); 
 219     if ( m_initialRecvBufferSize 
>= 0 ) 
 220         SetSocketOption(SO_RCVBUF
, m_initialRecvBufferSize
); 
 221     if ( m_initialSendBufferSize 
>= 0 ) 
 222         SetSocketOption(SO_SNDBUF
, m_initialSendBufferSize
); 
 224     // we always put our sockets in unblocked mode and handle blocking 
 225     // ourselves in DoRead/Write() if wxSOCKET_WAITALL is specified 
 226     UnblockAndRegisterWithEventLoop(); 
 229 wxSocketError 
wxSocketImpl::UpdateLocalAddress() 
 231     WX_SOCKLEN_T lenAddr 
= sizeof(*m_local
->m_addr
); 
 232     if ( getsockname(m_fd
, m_local
->m_addr
, &lenAddr
) != 0 ) 
 235         m_error 
= wxSOCKET_IOERR
; 
 239     m_local
->m_len 
= lenAddr
; 
 241     return wxSOCKET_NOERROR
; 
 244 wxSocketError 
wxSocketImpl::CreateServer() 
 246     if ( !PreCreateCheck(m_local
) ) 
 252     // do create the socket 
 253     m_fd 
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0); 
 255     if ( m_fd 
== INVALID_SOCKET 
) 
 257         m_error 
= wxSOCKET_IOERR
; 
 258         return wxSOCKET_IOERR
; 
 263     // and then bind to and listen on it 
 265     // FIXME: should we test for m_dobind here? 
 266     if ( bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0 ) 
 267         m_error 
= wxSOCKET_IOERR
; 
 271         if ( listen(m_fd
, 5) != 0 ) 
 272             m_error 
= wxSOCKET_IOERR
; 
 281     // finally retrieve the address we effectively bound to 
 282     return UpdateLocalAddress(); 
 285 wxSocketError 
wxSocketImpl::CreateClient(bool wait
) 
 287     if ( !PreCreateCheck(m_peer
) ) 
 290     m_fd 
= socket(m_peer
->m_realfamily
, SOCK_STREAM
, 0); 
 292     if ( m_fd 
== INVALID_SOCKET 
) 
 294         m_error 
= wxSOCKET_IOERR
; 
 295         return wxSOCKET_IOERR
; 
 300     // If a local address has been set, then bind to it before calling connect 
 301     if ( m_local 
&& m_local
->m_addr 
) 
 303         if ( bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0 ) 
 306             m_error 
= wxSOCKET_IOERR
; 
 312     int rc 
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
); 
 313     if ( rc 
== SOCKET_ERROR 
) 
 315         wxSocketError err 
= GetLastError(); 
 316         if ( err 
== wxSOCKET_WOULDBLOCK 
) 
 318             m_establishing 
= true; 
 320             // block waiting for connection if we should (otherwise just return 
 321             // wxSOCKET_WOULDBLOCK to the caller) 
 324                 err 
= SelectWithTimeout(wxSOCKET_CONNECTION_FLAG
) 
 327                 m_establishing 
= false; 
 335         m_error 
= wxSOCKET_NOERROR
; 
 342 wxSocketError 
wxSocketImpl::CreateUDP() 
 344     if ( !PreCreateCheck(m_local
) ) 
 350     m_fd 
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0); 
 352     if ( m_fd 
== INVALID_SOCKET 
) 
 354         m_error 
= wxSOCKET_IOERR
; 
 355         return wxSOCKET_IOERR
; 
 362         if ( bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0 ) 
 365             m_error 
= wxSOCKET_IOERR
; 
 369         return UpdateLocalAddress(); 
 372     return wxSOCKET_NOERROR
; 
 375 wxSocketImpl 
*wxSocketImpl::Accept(wxSocketBase
& wxsocket
) 
 378     WX_SOCKLEN_T fromlen 
= sizeof(from
); 
 379     const SOCKET fd 
= accept(m_fd
, &from
, &fromlen
); 
 381     if ( fd 
== INVALID_SOCKET 
) 
 384     wxSocketImpl 
* const sock 
= Create(wxsocket
); 
 387     sock
->m_peer 
= GAddress_new(); 
 388     _GAddress_translate_from(sock
->m_peer
, &from
, fromlen
); 
 390     sock
->UnblockAndRegisterWithEventLoop(); 
 396 void wxSocketImpl::Close() 
 398     if ( m_fd 
!= INVALID_SOCKET 
) 
 401         m_fd 
= INVALID_SOCKET
; 
 406  *  Disallow further read/write operations on this socket, close 
 407  *  the fd and disable all callbacks. 
 409 void wxSocketImpl::Shutdown() 
 411     if ( m_fd 
!= INVALID_SOCKET 
) 
 413         shutdown(m_fd
, 1 /* SD_SEND */); 
 419  *  Sets the timeout for blocking calls. Time is expressed in 
 422 void wxSocketImpl::SetTimeout(unsigned long millis
) 
 424     SetTimeValFromMS(m_timeout
, millis
); 
 427 void wxSocketImpl::NotifyOnStateChange(wxSocketNotify event
) 
 429     m_wxsocket
->OnRequest(event
); 
 432 /* Address handling */ 
 435  *  Set or get the local or peer address for this socket. The 'set' 
 436  *  functions return wxSOCKET_NOERROR on success, an error code otherwise. 
 437  *  The 'get' functions return a pointer to a GAddress object on success, 
 438  *  or NULL otherwise, in which case they set the error code of the 
 439  *  corresponding socket. 
 442  *    wxSOCKET_INVSOCK - the socket is not valid. 
 443  *    wxSOCKET_INVADDR - the address is not valid. 
 445 wxSocketError 
wxSocketImpl::SetLocal(GAddress 
*address
) 
 447   /* the socket must be initialized, or it must be a server */ 
 448   if (m_fd 
!= INVALID_SOCKET 
&& !m_server
) 
 450     m_error 
= wxSOCKET_INVSOCK
; 
 451     return wxSOCKET_INVSOCK
; 
 455   if (address 
== NULL 
|| address
->m_family 
== wxSOCKET_NOFAMILY
) 
 457     m_error 
= wxSOCKET_INVADDR
; 
 458     return wxSOCKET_INVADDR
; 
 462     GAddress_destroy(m_local
); 
 464   m_local 
= GAddress_copy(address
); 
 466   return wxSOCKET_NOERROR
; 
 469 wxSocketError 
wxSocketImpl::SetPeer(GAddress 
*address
) 
 472   if (address 
== NULL 
|| address
->m_family 
== wxSOCKET_NOFAMILY
) 
 474     m_error 
= wxSOCKET_INVADDR
; 
 475     return wxSOCKET_INVADDR
; 
 479     GAddress_destroy(m_peer
); 
 481   m_peer 
= GAddress_copy(address
); 
 483   return wxSOCKET_NOERROR
; 
 486 GAddress 
*wxSocketImpl::GetLocal() 
 490   WX_SOCKLEN_T size 
= sizeof(addr
); 
 493   /* try to get it from the m_local var first */ 
 495     return GAddress_copy(m_local
); 
 497   /* else, if the socket is initialized, try getsockname */ 
 498   if (m_fd 
== INVALID_SOCKET
) 
 500     m_error 
= wxSOCKET_INVSOCK
; 
 504   if (getsockname(m_fd
, (sockaddr
*)&addr
, &size
) == SOCKET_ERROR
) 
 506     m_error 
= wxSOCKET_IOERR
; 
 510   /* got a valid address from getsockname, create a GAddress object */ 
 511   if ((address 
= GAddress_new()) == NULL
) 
 513      m_error 
= wxSOCKET_MEMERR
; 
 517   if ((err 
= _GAddress_translate_from(address
, (sockaddr
*)&addr
, size
)) != wxSOCKET_NOERROR
) 
 519      GAddress_destroy(address
); 
 527 GAddress 
*wxSocketImpl::GetPeer() 
 529   /* try to get it from the m_peer var */ 
 531     return GAddress_copy(m_peer
); 
 536 // ========================================================================== 
 538 // ========================================================================== 
 540 // -------------------------------------------------------------------------- 
 541 // Initialization and shutdown 
 542 // -------------------------------------------------------------------------- 
 544 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses 
 545 //           to m_countInit with a crit section 
 546 size_t wxSocketBase::m_countInit 
= 0; 
 548 bool wxSocketBase::IsInitialized() 
 550     return m_countInit 
> 0; 
 553 bool wxSocketBase::Initialize() 
 555     if ( !m_countInit
++ ) 
 557         wxSocketManager 
* const manager 
= wxSocketManager::Get(); 
 558         if ( !manager 
|| !manager
->OnInit() ) 
 569 void wxSocketBase::Shutdown() 
 571     // we should be initialized 
 572     wxASSERT_MSG( m_countInit 
> 0, _T("extra call to Shutdown()") ); 
 573     if ( --m_countInit 
== 0 ) 
 575         wxSocketManager 
* const manager 
= wxSocketManager::Get(); 
 576         wxCHECK_RET( manager
, "should have a socket manager" ); 
 582 // -------------------------------------------------------------------------- 
 584 // -------------------------------------------------------------------------- 
 586 void wxSocketBase::Init() 
 589     m_type         
= wxSOCKET_UNINIT
; 
 600     m_beingDeleted 
= false; 
 615     if ( !IsInitialized() ) 
 617         // this Initialize() will be undone by wxSocketModule::OnExit(), all 
 618         // the other calls to it should be matched by a call to Shutdown() 
 623 wxSocketBase::wxSocketBase() 
 628 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
) 
 637 wxSocketBase::~wxSocketBase() 
 639     // Just in case the app called Destroy() *and* then deleted the socket 
 640     // immediately: don't leave dangling pointers. 
 641     wxAppTraits 
*traits 
= wxTheApp 
? wxTheApp
->GetTraits() : NULL
; 
 643         traits
->RemoveFromPendingDelete(this); 
 645     // Shutdown and close the socket 
 649     // Destroy the implementation object 
 652     // Free the pushback buffer 
 657 bool wxSocketBase::Destroy() 
 659     // Delayed destruction: the socket will be deleted during the next idle 
 660     // loop iteration. This ensures that all pending events have been 
 662     m_beingDeleted 
= true; 
 664     // Shutdown and close the socket 
 667     // Suppress events from now on 
 670     // schedule this object for deletion 
 671     wxAppTraits 
*traits 
= wxTheApp 
? wxTheApp
->GetTraits() : NULL
; 
 674         // let the traits object decide what to do with us 
 675         traits
->ScheduleForDestroy(this); 
 677     else // no app or no traits 
 679         // in wxBase we might have no app object at all, don't leak memory 
 686 // ---------------------------------------------------------------------------- 
 688 // ---------------------------------------------------------------------------- 
 690 void wxSocketBase::SetError(wxSocketError error
) 
 692     m_impl
->m_error 
= error
; 
 695 wxSocketError 
wxSocketBase::LastError() const 
 697     return m_impl
->GetError(); 
 700 // -------------------------------------------------------------------------- 
 702 // -------------------------------------------------------------------------- 
 704 // The following IO operations update m_lcount: 
 705 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard} 
 706 bool wxSocketBase::Close() 
 708     // Interrupt pending waits 
 715     m_establishing 
= false; 
 719 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
) 
 724     m_lcount 
= DoRead(buffer
, nbytes
); 
 726     // Allow read events from now on 
 732 wxUint32 
wxSocketBase::DoRead(void* buffer_
, wxUint32 nbytes
) 
 734     // We use pointer arithmetic here which doesn't work with void pointers. 
 735     char *buffer 
= static_cast<char *>(buffer_
); 
 737     // Try the push back buffer first, even before checking whether the socket 
 738     // is valid to allow reading previously pushed back data from an already 
 740     wxUint32 total 
= GetPushback(buffer
, nbytes
, false); 
 744     // If it's indeed closed or if read everything, there is nothing more to do. 
 745     if ( !m_impl 
|| !nbytes 
) 
 748     wxCHECK_MSG( buffer
, 0, "NULL buffer" ); 
 751     // wxSOCKET_NOWAIT overrides all the other flags and means that we are 
 752     // polling the socket and don't block at all. 
 753     if ( m_flags 
& wxSOCKET_NOWAIT 
) 
 755         int ret 
= m_impl
->Read(buffer
, nbytes
); 
 758             if ( m_impl
->GetLastError() != wxSOCKET_WOULDBLOCK 
) 
 759                 SetError(wxSOCKET_IOERR
); 
 761         else // not an error, even if we didn't read anything 
 766     else // blocking socket 
 770             // Wait until socket becomes ready for reading 
 771             if ( !WaitForRead() ) 
 774             const int ret 
= m_impl
->Read(buffer
, nbytes
); 
 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 
 787                 SetError(wxSOCKET_IOERR
); 
 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 
 796             if ( !(m_flags 
& wxSOCKET_WAITALL
) ) 
 799             // Otherwise continue reading until we do read everything. 
 807         // it's an error to not read everything in wxSOCKET_WAITALL mode or to 
 808         // not read anything otherwise 
 809         if ( ((m_flags 
& wxSOCKET_WAITALL
) && nbytes
) || !total 
) 
 810             SetError(wxSOCKET_IOERR
); 
 816 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
) 
 820         unsigned char sig
[4]; 
 821         unsigned char len
[4]; 
 827     int old_flags 
= m_flags
; 
 828     SetFlags((m_flags 
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
); 
 831     if ( DoRead(&msg
, sizeof(msg
)) == sizeof(msg
) ) 
 833         wxUint32 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); 
 838         if ( sig 
== 0xfeeddead ) 
 840             wxUint32 len 
= (wxUint32
)msg
.len
[0]; 
 841             len 
|= (wxUint32
)(msg
.len
[1] << 8); 
 842             len 
|= (wxUint32
)(msg
.len
[2] << 16); 
 843             len 
|= (wxUint32
)(msg
.len
[3] << 24); 
 854             // Don't attempt to read if the msg was zero bytes long. 
 855             m_lcount 
= len 
? DoRead(buffer
, len
) : 0; 
 859                 char discard_buffer
[MAX_DISCARD_SIZE
]; 
 862                 // NOTE: discarded bytes don't add to m_lcount. 
 865                     discard_len 
= len2 
> MAX_DISCARD_SIZE
 
 868                     discard_len 
= DoRead(discard_buffer
, (wxUint32
)discard_len
); 
 869                     len2 
-= (wxUint32
)discard_len
; 
 871                 while ((discard_len 
> 0) && len2
); 
 874             if ( !len2 
&& DoRead(&msg
, sizeof(msg
)) == sizeof(msg
) ) 
 876                 sig 
= (wxUint32
)msg
.sig
[0]; 
 877                 sig 
|= (wxUint32
)(msg
.sig
[1] << 8); 
 878                 sig 
|= (wxUint32
)(msg
.sig
[2] << 16); 
 879                 sig 
|= (wxUint32
)(msg
.sig
[3] << 24); 
 881                 if ( sig 
== 0xdeadfeed ) 
 888         SetError(wxSOCKET_IOERR
); 
 896 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
) 
 901     m_lcount 
= DoRead(buffer
, nbytes
); 
 902     Pushback(buffer
, m_lcount
); 
 904     // Allow read events again 
 910 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
) 
 915     m_lcount 
= DoWrite(buffer
, nbytes
); 
 917     // Allow write events again 
 923 // This function is a mirror image of DoRead() except that it doesn't use the 
 924 // push back buffer, please see comments there 
 925 wxUint32 
wxSocketBase::DoWrite(const void *buffer_
, wxUint32 nbytes
) 
 927     const char *buffer 
= static_cast<const char *>(buffer_
); 
 929     // Return if there is nothing to read or the socket is (already?) closed. 
 930     if ( !m_impl 
|| !nbytes 
) 
 933     wxCHECK_MSG( buffer
, 0, "NULL buffer" ); 
 936     if ( m_flags 
& wxSOCKET_NOWAIT 
) 
 938         const int ret 
= m_impl
->Write(buffer
, nbytes
); 
 941             if ( m_impl
->GetLastError() != wxSOCKET_WOULDBLOCK 
) 
 942                 SetError(wxSOCKET_IOERR
); 
 949     else // blocking socket 
 953             if ( !WaitForWrite() ) 
 956             const int ret 
= m_impl
->Write(buffer
, nbytes
); 
 965                 SetError(wxSOCKET_IOERR
); 
 970             if ( !(m_flags 
& wxSOCKET_WAITALL
) ) 
 980         if ( ((m_flags 
& wxSOCKET_WAITALL
) && nbytes
) || !total 
) 
 981             SetError(wxSOCKET_IOERR
); 
 987 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
) 
 991         unsigned char sig
[4]; 
 992         unsigned char len
[4]; 
 998     const int old_flags 
= m_flags
; 
 999     SetFlags((m_flags 
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
); 
1001     msg
.sig
[0] = (unsigned char) 0xad; 
1002     msg
.sig
[1] = (unsigned char) 0xde; 
1003     msg
.sig
[2] = (unsigned char) 0xed; 
1004     msg
.sig
[3] = (unsigned char) 0xfe; 
1006     msg
.len
[0] = (unsigned char) (nbytes 
& 0xff); 
1007     msg
.len
[1] = (unsigned char) ((nbytes 
>> 8) & 0xff); 
1008     msg
.len
[2] = (unsigned char) ((nbytes 
>> 16) & 0xff); 
1009     msg
.len
[3] = (unsigned char) ((nbytes 
>> 24) & 0xff); 
1012     if ( DoWrite(&msg
, sizeof(msg
)) == sizeof(msg
) ) 
1014         m_lcount 
= DoWrite(buffer
, nbytes
); 
1015         if ( m_lcount 
== nbytes 
) 
1017             msg
.sig
[0] = (unsigned char) 0xed; 
1018             msg
.sig
[1] = (unsigned char) 0xfe; 
1019             msg
.sig
[2] = (unsigned char) 0xad; 
1020             msg
.sig
[3] = (unsigned char) 0xde; 
1024             msg
.len
[3] = (char) 0; 
1026             if ( DoWrite(&msg
, sizeof(msg
)) == sizeof(msg
)) 
1032         SetError(wxSOCKET_IOERR
); 
1035     SetFlags(old_flags
); 
1040 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
) 
1043         Pushback(buffer
, nbytes
); 
1045     SetError(wxSOCKET_NOERROR
); 
1051 wxSocketBase
& wxSocketBase::Discard() 
1053     char *buffer 
= new char[MAX_DISCARD_SIZE
]; 
1060     const int old_flags 
= m_flags
; 
1061     SetFlags(wxSOCKET_NOWAIT
); 
1065         ret 
= DoRead(buffer
, MAX_DISCARD_SIZE
); 
1068     while (ret 
== MAX_DISCARD_SIZE
); 
1072     SetError(wxSOCKET_NOERROR
); 
1074     // Allow read events again 
1077     SetFlags(old_flags
); 
1082 // -------------------------------------------------------------------------- 
1084 // -------------------------------------------------------------------------- 
1087     This function will check for the events specified in the flags parameter, 
1088     and it will return a mask indicating which operations can be performed. 
1090 wxSocketEventFlags 
wxSocketImpl::Select(wxSocketEventFlags flags
, 
1091                                         const timeval 
*timeout
) 
1093     if ( m_fd 
== INVALID_SOCKET 
) 
1094         return (wxSOCKET_LOST_FLAG 
& flags
); 
1100         tv
.tv_sec 
= tv
.tv_usec 
= 0; 
1102     // prepare the FD sets, passing NULL for the one(s) we don't use 
1104         readfds
, *preadfds 
= NULL
, 
1105         writefds
, *pwritefds 
= NULL
, 
1106         exceptfds
;                      // always want to know about errors 
1108     if ( flags 
& wxSOCKET_INPUT_FLAG 
) 
1110         preadfds 
= &readfds
; 
1111         wxFD_ZERO(preadfds
); 
1112         wxFD_SET(m_fd
, preadfds
); 
1115     // when using non-blocking connect() the socket becomes connected 
1116     // (successfully or not) when it becomes writable 
1117     if ( flags 
& (wxSOCKET_OUTPUT_FLAG 
| wxSOCKET_CONNECTION_FLAG
) ) 
1119         pwritefds 
= &writefds
; 
1120         wxFD_ZERO(pwritefds
); 
1121         wxFD_SET(m_fd
, pwritefds
); 
1124     wxFD_ZERO(&exceptfds
); 
1125     wxFD_SET(m_fd
, &exceptfds
); 
1127     const int rc 
= select(m_fd 
+ 1, preadfds
, pwritefds
, &exceptfds
, &tv
); 
1129     // check for errors first 
1130     if ( rc 
== -1 || wxFD_ISSET(m_fd
, &exceptfds
) ) 
1132         m_establishing 
= false; 
1134         return wxSOCKET_LOST_FLAG 
& flags
; 
1140     wxASSERT_MSG( rc 
== 1, "unexpected select() return value" ); 
1142     wxSocketEventFlags detected 
= 0; 
1143     if ( preadfds 
&& wxFD_ISSET(m_fd
, preadfds
) ) 
1144         detected 
|= wxSOCKET_INPUT_FLAG
; 
1146     if ( pwritefds 
&& wxFD_ISSET(m_fd
, pwritefds
) ) 
1148         // check for the case of non-blocking connect() 
1149         if ( m_establishing 
&& !m_server 
) 
1152             SOCKOPTLEN_T len 
= sizeof(error
); 
1153             m_establishing 
= false; 
1154             getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
); 
1157                 detected 
= wxSOCKET_LOST_FLAG
; 
1159                 detected 
|= wxSOCKET_CONNECTION_FLAG
; 
1161         else // not called to get non-blocking connect() status 
1163             detected 
|= wxSOCKET_OUTPUT_FLAG
; 
1167     return detected 
& flags
; 
1171 wxSocketBase::DoWait(long seconds
, long milliseconds
, wxSocketEventFlags flags
) 
1173     wxCHECK_MSG( m_impl
, false, "can't wait on invalid socket" ); 
1175     // This can be set to true from Interrupt() to exit this function a.s.a.p. 
1176     m_interrupt 
= false; 
1179     // Use either the provided timeout or the default timeout value associated 
1180     // with this socket. 
1182     // TODO: allow waiting forever, see #9443 
1183     const long timeout 
= seconds 
== -1 ? m_timeout 
* 1000 
1184                                        : seconds 
* 1000 + milliseconds
; 
1185     const wxMilliClock_t timeEnd 
= wxGetLocalTimeMillis() + timeout
; 
1187     // Get the active event loop which we'll use for the message dispatching 
1188     // when running in the main thread unless this was explicitly disabled by 
1189     // setting wxSOCKET_BLOCK flag 
1190     wxEventLoopBase 
*eventLoop
; 
1191     if ( !(m_flags 
& wxSOCKET_BLOCK
) && wxIsMainThread() ) 
1193         eventLoop 
= wxEventLoop::GetActive(); 
1195     else // in worker thread 
1197         // We never dispatch messages from threads other than the main one. 
1201     // Wait until we receive the event we're waiting for or the timeout expires 
1202     // (but note that we always execute the loop at least once, even if timeout 
1203     // is 0 as this is used for polling) 
1204     bool gotEvent 
= false; 
1205     for ( bool firstTime 
= true; !m_interrupt 
; firstTime 
= false ) 
1207         long timeLeft 
= wxMilliClockToLong(timeEnd 
- wxGetLocalTimeMillis()); 
1216         // This function is only called if wxSOCKET_BLOCK flag was not used and 
1217         // so we should dispatch the events if there is an event loop capable 
1219         wxSocketEventFlags events
; 
1222             // reset them before starting to wait 
1225             eventLoop
->DispatchTimeout(timeLeft
); 
1227             events 
= m_eventsgot
; 
1229         else // no event loop or waiting in another thread 
1231             // as explained below, we should always check for wxSOCKET_LOST_FLAG 
1233             SetTimeValFromMS(tv
, timeLeft
); 
1234             events 
= m_impl
->Select(flags 
| wxSOCKET_LOST_FLAG
, &tv
); 
1237         // always check for wxSOCKET_LOST_FLAG, even if flags doesn't include 
1238         // it, as continuing to wait for anything else after getting it is 
1240         if ( events 
& wxSOCKET_LOST_FLAG 
) 
1242             m_connected 
= false; 
1243             m_establishing 
= false; 
1244             if ( flags 
& wxSOCKET_LOST_FLAG 
) 
1249         // otherwise mask out the bits we're not interested in 
1252         // Incoming connection (server) or connection established (client)? 
1253         if ( events 
& wxSOCKET_CONNECTION_FLAG 
) 
1256             m_establishing 
= false; 
1261         // Data available or output buffer ready? 
1262         if ( (events 
& wxSOCKET_INPUT_FLAG
) || (events 
& wxSOCKET_OUTPUT_FLAG
) ) 
1272 bool wxSocketBase::Wait(long seconds
, long milliseconds
) 
1274     return DoWait(seconds
, milliseconds
, 
1275             wxSOCKET_INPUT_FLAG 
| 
1276             wxSOCKET_OUTPUT_FLAG 
| 
1277             wxSOCKET_CONNECTION_FLAG 
| 
1282 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
) 
1284     // Check pushback buffer before entering DoWait 
1288     // Note that wxSOCKET_INPUT_LOST has to be explicitly passed to DoWait 
1289     // because of the semantics of WaitForRead: a return value of true means 
1290     // that a Read call will return immediately, not that there is 
1291     // actually data to read. 
1292     return DoWait(seconds
, milliseconds
, wxSOCKET_INPUT_FLAG 
| wxSOCKET_LOST_FLAG
); 
1296 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
) 
1298     return DoWait(seconds
, milliseconds
, wxSOCKET_OUTPUT_FLAG 
| wxSOCKET_LOST_FLAG
); 
1301 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
) 
1303     return DoWait(seconds
, milliseconds
, wxSOCKET_LOST_FLAG
); 
1306 // -------------------------------------------------------------------------- 
1308 // -------------------------------------------------------------------------- 
1311 // Get local or peer address 
1314 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const 
1321     peer 
= m_impl
->GetPeer(); 
1323     // copying a null address would just trigger an assert anyway 
1328     addr_man
.SetAddress(peer
); 
1329     GAddress_destroy(peer
); 
1334 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const 
1341     local 
= m_impl
->GetLocal(); 
1342     addr_man
.SetAddress(local
); 
1343     GAddress_destroy(local
); 
1349 // Save and restore socket state 
1352 void wxSocketBase::SaveState() 
1354     wxSocketState 
*state
; 
1356     state 
= new wxSocketState(); 
1358     state
->m_flags      
= m_flags
; 
1359     state
->m_notify     
= m_notify
; 
1360     state
->m_eventmask  
= m_eventmask
; 
1361     state
->m_clientData 
= m_clientData
; 
1363     m_states
.Append(state
); 
1366 void wxSocketBase::RestoreState() 
1368     wxList::compatibility_iterator node
; 
1369     wxSocketState 
*state
; 
1371     node 
= m_states
.GetLast(); 
1375     state 
= (wxSocketState 
*)node
->GetData(); 
1377     m_flags      
= state
->m_flags
; 
1378     m_notify     
= state
->m_notify
; 
1379     m_eventmask  
= state
->m_eventmask
; 
1380     m_clientData 
= state
->m_clientData
; 
1382     m_states
.Erase(node
); 
1387 // Timeout and flags 
1390 void wxSocketBase::SetTimeout(long seconds
) 
1392     m_timeout 
= seconds
; 
1395         m_impl
->SetTimeout(m_timeout 
* 1000); 
1398 void wxSocketBase::SetFlags(wxSocketFlags flags
) 
1400     // Do some sanity checking on the flags used: not all values can be used 
1402     wxASSERT_MSG( !(flags 
& wxSOCKET_NOWAIT
) || 
1403                   !(flags 
& (wxSOCKET_WAITALL 
| wxSOCKET_BLOCK
)), 
1404                   "Using wxSOCKET_WAITALL or wxSOCKET_BLOCK with " 
1405                   "wxSOCKET_NOWAIT doesn't make sense" ); 
1411 // -------------------------------------------------------------------------- 
1413 // -------------------------------------------------------------------------- 
1415 void wxSocketBase::OnRequest(wxSocketNotify notification
) 
1417     wxSocketEventFlags flag 
= 0; 
1418     switch ( notification 
) 
1420         case wxSOCKET_INPUT
: 
1421             flag 
= wxSOCKET_INPUT_FLAG
; 
1424         case wxSOCKET_OUTPUT
: 
1425             flag 
= wxSOCKET_OUTPUT_FLAG
; 
1428         case wxSOCKET_CONNECTION
: 
1429             flag 
= wxSOCKET_CONNECTION_FLAG
; 
1433             flag 
= wxSOCKET_LOST_FLAG
; 
1437             wxFAIL_MSG( "unknown wxSocket notification" ); 
1440     // if we lost the connection the socket is now closed 
1441     if ( notification 
== wxSOCKET_LOST 
) 
1444     // remember the events which were generated for this socket, we're going to 
1445     // use this in DoWait() 
1446     m_eventsgot 
|= flag
; 
1448     // send the wx event if enabled and we're interested in it 
1449     if ( m_notify 
&& (m_eventmask 
& flag
) && m_handler 
) 
1451         // If we are in the middle of a R/W operation, do not propagate events 
1452         // to users. Also, filter 'late' events which are no longer valid. 
1453         if ( notification 
== wxSOCKET_INPUT 
) 
1455             if ( m_reading 
|| !m_impl
->Select(wxSOCKET_INPUT_FLAG
) ) 
1458         else if ( notification 
== wxSOCKET_OUTPUT 
) 
1460             if ( m_writing 
|| !m_impl
->Select(wxSOCKET_OUTPUT_FLAG
) ) 
1464         wxSocketEvent 
event(m_id
); 
1465         event
.m_event      
= notification
; 
1466         event
.m_clientData 
= m_clientData
; 
1467         event
.SetEventObject(this); 
1469         m_handler
->AddPendingEvent(event
); 
1473 void wxSocketBase::Notify(bool notify
) 
1478 void wxSocketBase::SetNotify(wxSocketEventFlags flags
) 
1480     m_eventmask 
= flags
; 
1483 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
) 
1485     m_handler 
= &handler
; 
1489 // -------------------------------------------------------------------------- 
1491 // -------------------------------------------------------------------------- 
1493 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
) 
1497     if (m_unread 
== NULL
) 
1498         m_unread 
= malloc(size
); 
1503         tmp 
= malloc(m_unrd_size 
+ size
); 
1504         memcpy((char *)tmp 
+ size
, m_unread
, m_unrd_size
); 
1510     m_unrd_size 
+= size
; 
1512     memcpy(m_unread
, buffer
, size
); 
1515 wxUint32 
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
) 
1517     wxCHECK_MSG( buffer
, 0, "NULL buffer" ); 
1522     if (size 
> (m_unrd_size
-m_unrd_cur
)) 
1523         size 
= m_unrd_size
-m_unrd_cur
; 
1525     memcpy(buffer
, (char *)m_unread 
+ m_unrd_cur
, size
); 
1530         if (m_unrd_size 
== m_unrd_cur
) 
1543 // ========================================================================== 
1545 // ========================================================================== 
1547 // -------------------------------------------------------------------------- 
1549 // -------------------------------------------------------------------------- 
1551 wxSocketServer::wxSocketServer(const wxSockAddress
& addr_man
, 
1552                                wxSocketFlags flags
) 
1553               : wxSocketBase(flags
, wxSOCKET_SERVER
) 
1555     wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") ); 
1557     m_impl 
= wxSocketImpl::Create(*this); 
1561         wxLogTrace( wxTRACE_Socket
, _T("*** Failed to create m_impl") ); 
1565     // Setup the socket as server 
1566     m_impl
->SetLocal(addr_man
.GetAddress()); 
1568     if (GetFlags() & wxSOCKET_REUSEADDR
) { 
1569         m_impl
->SetReusable(); 
1571     if (GetFlags() & wxSOCKET_BROADCAST
) { 
1572         m_impl
->SetBroadcast(); 
1574     if (GetFlags() & wxSOCKET_NOBIND
) { 
1575         m_impl
->DontDoBind(); 
1578     if (m_impl
->CreateServer() != wxSOCKET_NOERROR
) 
1583         wxLogTrace( wxTRACE_Socket
, _T("*** CreateServer() failed") ); 
1587     wxLogTrace( wxTRACE_Socket
, _T("wxSocketServer on fd %d"), m_impl
->m_fd 
); 
1590 // -------------------------------------------------------------------------- 
1592 // -------------------------------------------------------------------------- 
1594 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
) 
1596     if ( !m_impl 
|| (m_impl
->m_fd 
== INVALID_SOCKET
) || !m_impl
->IsServer() ) 
1598         wxFAIL_MSG( "can only be called for a valid server socket" ); 
1600         SetError(wxSOCKET_INVSOCK
); 
1607         // wait until we get a connection 
1608         if ( !m_impl
->SelectWithTimeout(wxSOCKET_INPUT_FLAG
) ) 
1610             SetError(wxSOCKET_TIMEDOUT
); 
1616     sock
.m_impl 
= m_impl
->Accept(sock
); 
1620         SetError(m_impl
->GetLastError()); 
1625     sock
.m_type 
= wxSOCKET_BASE
; 
1626     sock
.m_connected 
= true; 
1631 wxSocketBase 
*wxSocketServer::Accept(bool wait
) 
1633     wxSocketBase
* sock 
= new wxSocketBase(); 
1635     sock
->SetFlags(m_flags
); 
1637     if (!AcceptWith(*sock
, wait
)) 
1646 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
) 
1648     return DoWait(seconds
, milliseconds
, wxSOCKET_CONNECTION_FLAG
); 
1651 bool wxSocketBase::GetOption(int level
, int optname
, void *optval
, int *optlen
) 
1653     wxASSERT_MSG( m_impl
, _T("Socket not initialised") ); 
1655     SOCKOPTLEN_T lenreal 
= *optlen
; 
1656     if ( getsockopt(m_impl
->m_fd
, level
, optname
, 
1657                     static_cast<char *>(optval
), &lenreal
) != 0 ) 
1666 wxSocketBase::SetOption(int level
, int optname
, const void *optval
, int optlen
) 
1668     wxASSERT_MSG( m_impl
, _T("Socket not initialised") ); 
1670     return setsockopt(m_impl
->m_fd
, level
, optname
, 
1671                       static_cast<const char *>(optval
), optlen
) == 0; 
1674 bool wxSocketBase::SetLocal(const wxIPV4address
& local
) 
1676     GAddress
* la 
= local
.GetAddress(); 
1678     // If the address is valid, save it for use when we call Connect 
1679     if (la 
&& la
->m_addr
) 
1681         m_localAddress 
= local
; 
1689 // ========================================================================== 
1691 // ========================================================================== 
1693 // -------------------------------------------------------------------------- 
1695 // -------------------------------------------------------------------------- 
1697 wxSocketClient::wxSocketClient(wxSocketFlags flags
) 
1698               : wxSocketBase(flags
, wxSOCKET_CLIENT
) 
1700     m_initialRecvBufferSize 
= 
1701     m_initialSendBufferSize 
= -1; 
1704 wxSocketClient::~wxSocketClient() 
1708 // -------------------------------------------------------------------------- 
1710 // -------------------------------------------------------------------------- 
1712 bool wxSocketClient::DoConnect(const wxSockAddress
& remote
, 
1713                                const wxSockAddress
* local
, 
1718         // Shutdown and destroy the old socket 
1723     m_connected 
= false; 
1724     m_establishing 
= false; 
1726     // Create and set up the new one 
1727     m_impl 
= wxSocketImpl::Create(*this); 
1731     // Reuse makes sense for clients too, if we are trying to rebind to the same port 
1732     if (GetFlags() & wxSOCKET_REUSEADDR
) 
1733         m_impl
->SetReusable(); 
1734     if (GetFlags() & wxSOCKET_BROADCAST
) 
1735         m_impl
->SetBroadcast(); 
1736     if (GetFlags() & wxSOCKET_NOBIND
) 
1737         m_impl
->DontDoBind(); 
1739     // Bind to the local IP address and port, when provided or if one had been 
1741     if ( !local 
&& m_localAddress
.GetAddress() ) 
1742         local 
= &m_localAddress
; 
1745         m_impl
->SetLocal(local
->GetAddress()); 
1747     m_impl
->SetInitialSocketBuffers(m_initialRecvBufferSize
, m_initialSendBufferSize
); 
1749     m_impl
->SetPeer(remote
.GetAddress()); 
1751     // Finally do create the socket and connect to the peer 
1752     const wxSocketError err 
= m_impl
->CreateClient(wait
); 
1754     if ( err 
!= wxSOCKET_NOERROR 
) 
1756         if ( err 
== wxSOCKET_WOULDBLOCK 
) 
1758             wxASSERT_MSG( !wait
, "shouldn't get this for blocking connect" ); 
1760             m_establishing 
= true; 
1770 bool wxSocketClient::Connect(const wxSockAddress
& remote
, bool wait
) 
1772     return DoConnect(remote
, NULL
, wait
); 
1775 bool wxSocketClient::Connect(const wxSockAddress
& remote
, 
1776                              const wxSockAddress
& local
, 
1779     return DoConnect(remote
, &local
, wait
); 
1782 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
) 
1786         // this happens if the initial attempt to connect succeeded without 
1791     wxCHECK_MSG( m_establishing 
&& m_impl
, false, 
1792                  "No connection establishment attempt in progress" ); 
1794     // we must specify wxSOCKET_LOST_FLAG here explicitly because we must return 
1795     // true if the connection establishment process is finished, whether it is 
1796     // over because we successfully connected or because we were not able to 
1798     return DoWait(seconds
, milliseconds
, 
1799         wxSOCKET_CONNECTION_FLAG 
| wxSOCKET_LOST_FLAG
); 
1802 // ========================================================================== 
1804 // ========================================================================== 
1806 wxDatagramSocket::wxDatagramSocket( const wxSockAddress
& addr
, 
1807                                     wxSocketFlags flags 
) 
1808                 : wxSocketBase( flags
, wxSOCKET_DATAGRAM 
) 
1810     // Create the socket 
1811     m_impl 
= wxSocketImpl::Create(*this); 
1816     // Setup the socket as non connection oriented 
1817     m_impl
->SetLocal(addr
.GetAddress()); 
1818     if (flags 
& wxSOCKET_REUSEADDR
) 
1820         m_impl
->SetReusable(); 
1822     if (GetFlags() & wxSOCKET_BROADCAST
) 
1824         m_impl
->SetBroadcast(); 
1826     if (GetFlags() & wxSOCKET_NOBIND
) 
1828         m_impl
->DontDoBind(); 
1831     if ( m_impl
->CreateUDP() != wxSOCKET_NOERROR 
) 
1838     // Initialize all stuff 
1839     m_connected 
= false; 
1840     m_establishing 
= false; 
1843 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
, 
1852 wxDatagramSocket
& wxDatagramSocket::SendTo( const wxSockAddress
& addr
, 
1856     wxASSERT_MSG( m_impl
, _T("Socket not initialised") ); 
1858     m_impl
->SetPeer(addr
.GetAddress()); 
1863 // ========================================================================== 
1865 // ========================================================================== 
1867 class wxSocketModule 
: public wxModule
 
1870     virtual bool OnInit() 
1872         // wxSocketBase will call Initialize() itself only if sockets are 
1873         // really used, don't do it from here 
1877     virtual void OnExit() 
1879         if ( wxSocketBase::IsInitialized() ) 
1880             wxSocketBase::Shutdown(); 
1884     DECLARE_DYNAMIC_CLASS(wxSocketModule
) 
1887 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
) 
1889 #endif // wxUSE_SOCKETS