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"
47 #include "wx/private/fd.h"
48 #include "wx/private/socket.h"
54 // we use MSG_NOSIGNAL to avoid getting SIGPIPE when sending data to a remote
55 // host which closed the connection if it is available, otherwise we rely on
56 // SO_NOSIGPIPE existency
58 // this should cover all the current Unix systems (Windows never sends any
59 // signals anyhow) but if we find one that has neither we should explicitly
60 // ignore SIGPIPE for it
61 // OpenVMS has neither MSG_NOSIGNAL nor SO_NOSIGPIPE. However the socket sample
62 // seems to work. Not sure if problems will show up on OpenVMS using sockets.
64 #define wxSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
65 #else // MSG_NOSIGNAL not available (BSD including OS X)
66 // next best possibility is to use SO_NOSIGPIPE socket option, this covers
67 // BSD systems (including OS X) -- but if we don't have it neither (AIX and
68 // old HP-UX do not), we have to fall back to the old way of simply
69 // disabling SIGPIPE temporarily, so define a class to do it in a safe way
70 #if defined(__UNIX__) && !defined(SO_NOSIGPIPE)
71 extern "C" { typedef void (*wxSigHandler_t
)(int); }
77 // ctor disables the given signal
79 : m_handler(signal(sig
, SIG_IGN
)),
84 // dtor restores the old handler
87 signal(m_sig
, m_handler
);
91 const wxSigHandler_t m_handler
;
94 wxDECLARE_NO_COPY_CLASS(IgnoreSignal
);
96 } // anonymous namespace
98 #define wxNEEDS_IGNORE_SIGPIPE
99 #endif // Unix without SO_NOSIGPIPE
101 #define wxSOCKET_MSG_NOSIGNAL 0
104 // DLL options compatibility check:
105 #include "wx/build.h"
106 WX_CHECK_BUILD_OPTIONS("wxNet")
108 // --------------------------------------------------------------------------
109 // macros and constants
110 // --------------------------------------------------------------------------
113 wxDEFINE_EVENT(wxEVT_SOCKET
, wxSocketEvent
);
116 #define MAX_DISCARD_SIZE (10 * 1024)
118 #define wxTRACE_Socket _T("wxSocket")
120 // --------------------------------------------------------------------------
122 // --------------------------------------------------------------------------
124 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
125 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
126 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
127 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
128 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
137 void SetTimeValFromMS(timeval
& tv
, unsigned long ms
)
139 tv
.tv_sec
= (ms
/ 1000);
140 tv
.tv_usec
= (ms
% 1000) * 1000;
143 } // anonymous namespace
145 // --------------------------------------------------------------------------
147 // --------------------------------------------------------------------------
149 class wxSocketState
: public wxObject
152 wxSocketFlags m_flags
;
153 wxSocketEventFlags m_eventmask
;
158 wxSocketState() : wxObject() {}
160 wxDECLARE_NO_COPY_CLASS(wxSocketState
);
163 // wxSocketWaitModeChanger: temporarily change the socket flags affecting its
165 class wxSocketWaitModeChanger
168 // temporarily set the flags to include the flag value which may be either
169 // wxSOCKET_NOWAIT or wxSOCKET_WAITALL
170 wxSocketWaitModeChanger(wxSocketBase
*socket
, int flag
)
172 m_oldflags(socket
->GetFlags())
175 wxASSERT_MSG( flag
== wxSOCKET_WAITALL
|| flag
== wxSOCKET_NOWAIT
,
178 // preserve wxSOCKET_BLOCK value when switching to wxSOCKET_WAITALL
179 // mode but not when switching to wxSOCKET_NOWAIT as the latter is
180 // incompatible with wxSOCKET_BLOCK
181 if ( flag
!= wxSOCKET_NOWAIT
)
182 flag
|= m_oldflags
& wxSOCKET_BLOCK
;
184 socket
->SetFlags(flag
);
187 ~wxSocketWaitModeChanger()
189 m_socket
->SetFlags(m_oldflags
);
193 wxSocketBase
* const m_socket
;
194 const int m_oldflags
;
196 wxDECLARE_NO_COPY_CLASS(wxSocketWaitModeChanger
);
199 // wxSocketRead/WriteGuard are instantiated before starting reading
200 // from/writing to the socket
201 class wxSocketReadGuard
204 wxSocketReadGuard(wxSocketBase
*socket
)
207 wxASSERT_MSG( !m_socket
->m_reading
, "read reentrancy?" );
209 m_socket
->m_reading
= true;
214 m_socket
->m_reading
= false;
216 m_socket
->m_impl
->ReenableEvents(wxSOCKET_INPUT_FLAG
);
220 wxSocketBase
* const m_socket
;
222 wxDECLARE_NO_COPY_CLASS(wxSocketReadGuard
);
225 class wxSocketWriteGuard
228 wxSocketWriteGuard(wxSocketBase
*socket
)
231 wxASSERT_MSG( !m_socket
->m_writing
, "write reentrancy?" );
233 m_socket
->m_writing
= true;
235 m_socket
->m_impl
->ReenableEvents(wxSOCKET_OUTPUT_FLAG
);
238 ~wxSocketWriteGuard()
240 m_socket
->m_writing
= false;
244 wxSocketBase
* const m_socket
;
246 wxDECLARE_NO_COPY_CLASS(wxSocketWriteGuard
);
249 // ============================================================================
251 // ============================================================================
253 wxSocketManager
*wxSocketManager::ms_manager
= NULL
;
256 void wxSocketManager::Set(wxSocketManager
*manager
)
258 wxASSERT_MSG( !ms_manager
, "too late to set manager now" );
260 ms_manager
= manager
;
264 void wxSocketManager::Init()
266 wxASSERT_MSG( !ms_manager
, "shouldn't be initialized twice" );
269 Details: Initialize() creates a hidden window as a sink for socket
270 events, such as 'read completed'. wxMSW has only one message loop
271 for the main thread. If Initialize is called in a secondary thread,
272 the socket window will be created for the secondary thread, but
273 since there is no message loop on this thread, it will never
274 receive events and all socket operations will time out.
275 BTW, the main thread must not be stopped using sleep or block
276 on a semaphore (a bad idea in any case) or socket operations
279 On the Mac side, Initialize() stores a pointer to the CFRunLoop for
280 the main thread. Because secondary threads do not have run loops,
281 adding event notifications to the "Current" loop would have no
282 effect at all, events would never fire.
284 wxASSERT_MSG( wxIsMainThread(),
285 "sockets must be initialized from the main thread" );
287 wxAppConsole
* const app
= wxAppConsole::GetInstance();
288 wxCHECK_RET( app
, "sockets can't be initialized without wxApp" );
290 ms_manager
= app
->GetTraits()->GetSocketManager();
293 // ==========================================================================
295 // ==========================================================================
297 wxSocketImpl::wxSocketImpl(wxSocketBase
& wxsocket
)
298 : m_wxsocket(&wxsocket
)
300 m_fd
= INVALID_SOCKET
;
301 m_error
= wxSOCKET_NOERROR
;
305 SetTimeout(wxsocket
.GetTimeout() * 1000);
307 m_establishing
= false;
311 m_initialRecvBufferSize
= -1;
312 m_initialSendBufferSize
= -1;
315 wxSocketImpl::~wxSocketImpl()
317 if ( m_fd
!= INVALID_SOCKET
)
321 bool wxSocketImpl::PreCreateCheck(const wxSockAddressImpl
& addr
)
323 if ( m_fd
!= INVALID_SOCKET
)
325 m_error
= wxSOCKET_INVSOCK
;
331 m_error
= wxSOCKET_INVADDR
;
338 void wxSocketImpl::PostCreation()
340 // FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option
342 EnableSocketOption(SO_NOSIGPIPE
);
346 EnableSocketOption(SO_REUSEADDR
);
350 wxASSERT_MSG( !m_stream
, "broadcasting is for datagram sockets only" );
352 EnableSocketOption(SO_BROADCAST
);
355 if ( m_initialRecvBufferSize
>= 0 )
356 SetSocketOption(SO_RCVBUF
, m_initialRecvBufferSize
);
357 if ( m_initialSendBufferSize
>= 0 )
358 SetSocketOption(SO_SNDBUF
, m_initialSendBufferSize
);
360 // we always put our sockets in unblocked mode and handle blocking
361 // ourselves in DoRead/Write() if wxSOCKET_WAITALL is specified
362 UnblockAndRegisterWithEventLoop();
365 wxSocketError
wxSocketImpl::UpdateLocalAddress()
367 if ( !m_local
.IsOk() )
369 // ensure that we have a valid object using the correct family: correct
370 // being the same one as our peer uses as we have no other way to
372 m_local
.Create(m_peer
.GetFamily());
375 WX_SOCKLEN_T lenAddr
= m_local
.GetLen();
376 if ( getsockname(m_fd
, m_local
.GetWritableAddr(), &lenAddr
) != 0 )
379 m_error
= wxSOCKET_IOERR
;
383 return wxSOCKET_NOERROR
;
386 wxSocketError
wxSocketImpl::CreateServer()
388 if ( !PreCreateCheck(m_local
) )
394 // do create the socket
395 m_fd
= socket(m_local
.GetFamily(), SOCK_STREAM
, 0);
397 if ( m_fd
== INVALID_SOCKET
)
399 m_error
= wxSOCKET_IOERR
;
400 return wxSOCKET_IOERR
;
405 // and then bind to and listen on it
407 // FIXME: should we test for m_dobind here?
408 if ( bind(m_fd
, m_local
.GetAddr(), m_local
.GetLen()) != 0 )
409 m_error
= wxSOCKET_IOERR
;
413 if ( listen(m_fd
, 5) != 0 )
414 m_error
= wxSOCKET_IOERR
;
423 // finally retrieve the address we effectively bound to
424 return UpdateLocalAddress();
427 wxSocketError
wxSocketImpl::CreateClient(bool wait
)
429 if ( !PreCreateCheck(m_peer
) )
432 m_fd
= socket(m_peer
.GetFamily(), SOCK_STREAM
, 0);
434 if ( m_fd
== INVALID_SOCKET
)
436 m_error
= wxSOCKET_IOERR
;
437 return wxSOCKET_IOERR
;
442 // If a local address has been set, then bind to it before calling connect
443 if ( m_local
.IsOk() )
445 if ( bind(m_fd
, m_local
.GetAddr(), m_local
.GetLen()) != 0 )
448 m_error
= wxSOCKET_IOERR
;
454 int rc
= connect(m_fd
, m_peer
.GetAddr(), m_peer
.GetLen());
455 if ( rc
== SOCKET_ERROR
)
457 wxSocketError err
= GetLastError();
458 if ( err
== wxSOCKET_WOULDBLOCK
)
460 m_establishing
= true;
462 // block waiting for connection if we should (otherwise just return
463 // wxSOCKET_WOULDBLOCK to the caller)
466 err
= SelectWithTimeout(wxSOCKET_CONNECTION_FLAG
)
469 m_establishing
= false;
477 m_error
= wxSOCKET_NOERROR
;
484 wxSocketError
wxSocketImpl::CreateUDP()
486 if ( !PreCreateCheck(m_local
) )
492 m_fd
= socket(m_local
.GetFamily(), SOCK_DGRAM
, 0);
494 if ( m_fd
== INVALID_SOCKET
)
496 m_error
= wxSOCKET_IOERR
;
497 return wxSOCKET_IOERR
;
504 if ( bind(m_fd
, m_local
.GetAddr(), m_local
.GetLen()) != 0 )
507 m_error
= wxSOCKET_IOERR
;
511 return UpdateLocalAddress();
514 return wxSOCKET_NOERROR
;
517 wxSocketImpl
*wxSocketImpl::Accept(wxSocketBase
& wxsocket
)
519 wxSockAddressStorage from
;
520 WX_SOCKLEN_T fromlen
= sizeof(from
);
521 const SOCKET fd
= accept(m_fd
, &from
.addr
, &fromlen
);
523 // accepting is similar to reading in the sense that it resets "ready for
524 // read" flag on the socket
525 ReenableEvents(wxSOCKET_INPUT_FLAG
);
527 if ( fd
== INVALID_SOCKET
)
530 wxSocketImpl
* const sock
= Create(wxsocket
);
532 sock
->m_peer
= wxSockAddressImpl(from
.addr
, fromlen
);
534 sock
->UnblockAndRegisterWithEventLoop();
540 void wxSocketImpl::Close()
542 if ( m_fd
!= INVALID_SOCKET
)
545 m_fd
= INVALID_SOCKET
;
549 void wxSocketImpl::Shutdown()
551 if ( m_fd
!= INVALID_SOCKET
)
553 shutdown(m_fd
, 1 /* SD_SEND */);
559 * Sets the timeout for blocking calls. Time is expressed in
562 void wxSocketImpl::SetTimeout(unsigned long millis
)
564 SetTimeValFromMS(m_timeout
, millis
);
567 void wxSocketImpl::NotifyOnStateChange(wxSocketNotify event
)
569 m_wxsocket
->OnRequest(event
);
572 /* Address handling */
573 wxSocketError
wxSocketImpl::SetLocal(const wxSockAddressImpl
& local
)
575 /* the socket must be initialized, or it must be a server */
576 if (m_fd
!= INVALID_SOCKET
&& !m_server
)
578 m_error
= wxSOCKET_INVSOCK
;
579 return wxSOCKET_INVSOCK
;
584 m_error
= wxSOCKET_INVADDR
;
585 return wxSOCKET_INVADDR
;
590 return wxSOCKET_NOERROR
;
593 wxSocketError
wxSocketImpl::SetPeer(const wxSockAddressImpl
& peer
)
597 m_error
= wxSOCKET_INVADDR
;
598 return wxSOCKET_INVADDR
;
603 return wxSOCKET_NOERROR
;
606 const wxSockAddressImpl
& wxSocketImpl::GetLocal()
608 if ( !m_local
.IsOk() )
609 UpdateLocalAddress();
614 // ----------------------------------------------------------------------------
616 // ----------------------------------------------------------------------------
618 // this macro wraps the given expression (normally a syscall) in a loop which
619 // ignores any interruptions, i.e. reevaluates it again if it failed and errno
622 #define DO_WHILE_EINTR( rc, syscall ) \
626 while ( rc == -1 && errno == EINTR )
628 #define DO_WHILE_EINTR( rc, syscall ) rc = (syscall)
631 int wxSocketImpl::RecvStream(void *buffer
, int size
)
634 DO_WHILE_EINTR( ret
, recv(m_fd
, static_cast<char *>(buffer
), size
, 0) );
638 // receiving 0 bytes for a TCP socket indicates that the connection was
639 // closed by peer so shut down our end as well (for UDP sockets empty
640 // datagrams are also possible)
641 m_establishing
= false;
642 NotifyOnStateChange(wxSOCKET_LOST
);
646 // do not return an error in this case however
652 int wxSocketImpl::SendStream(const void *buffer
, int size
)
654 #ifdef wxNEEDS_IGNORE_SIGPIPE
655 IgnoreSignal
ignore(SIGPIPE
);
659 DO_WHILE_EINTR( ret
, send(m_fd
, static_cast<const char *>(buffer
), size
,
660 wxSOCKET_MSG_NOSIGNAL
) );
665 int wxSocketImpl::RecvDgram(void *buffer
, int size
)
667 wxSockAddressStorage from
;
668 WX_SOCKLEN_T fromlen
= sizeof(from
);
671 DO_WHILE_EINTR( ret
, recvfrom(m_fd
, static_cast<char *>(buffer
), size
,
672 0, &from
.addr
, &fromlen
) );
674 if ( ret
== SOCKET_ERROR
)
677 m_peer
= wxSockAddressImpl(from
.addr
, fromlen
);
678 if ( !m_peer
.IsOk() )
684 int wxSocketImpl::SendDgram(const void *buffer
, int size
)
686 if ( !m_peer
.IsOk() )
688 m_error
= wxSOCKET_INVADDR
;
693 DO_WHILE_EINTR( ret
, sendto(m_fd
, static_cast<const char *>(buffer
), size
,
694 0, m_peer
.GetAddr(), m_peer
.GetLen()) );
699 int wxSocketImpl::Read(void *buffer
, int size
)
701 // server sockets can't be used for IO, only to accept new connections
702 if ( m_fd
== INVALID_SOCKET
|| m_server
)
704 m_error
= wxSOCKET_INVSOCK
;
708 int ret
= m_stream
? RecvStream(buffer
, size
)
709 : RecvDgram(buffer
, size
);
711 m_error
= ret
== SOCKET_ERROR
? GetLastError() : wxSOCKET_NOERROR
;
716 int wxSocketImpl::Write(const void *buffer
, int size
)
718 if ( m_fd
== INVALID_SOCKET
|| m_server
)
720 m_error
= wxSOCKET_INVSOCK
;
724 int ret
= m_stream
? SendStream(buffer
, size
)
725 : SendDgram(buffer
, size
);
727 m_error
= ret
== SOCKET_ERROR
? GetLastError() : wxSOCKET_NOERROR
;
732 // ==========================================================================
734 // ==========================================================================
736 // --------------------------------------------------------------------------
737 // Initialization and shutdown
738 // --------------------------------------------------------------------------
740 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
741 // to m_countInit with a crit section
742 size_t wxSocketBase::m_countInit
= 0;
744 bool wxSocketBase::IsInitialized()
746 return m_countInit
> 0;
749 bool wxSocketBase::Initialize()
751 if ( !m_countInit
++ )
753 wxSocketManager
* const manager
= wxSocketManager::Get();
754 if ( !manager
|| !manager
->OnInit() )
765 void wxSocketBase::Shutdown()
767 // we should be initialized
768 wxASSERT_MSG( m_countInit
> 0, _T("extra call to Shutdown()") );
769 if ( --m_countInit
== 0 )
771 wxSocketManager
* const manager
= wxSocketManager::Get();
772 wxCHECK_RET( manager
, "should have a socket manager" );
778 // --------------------------------------------------------------------------
780 // --------------------------------------------------------------------------
782 void wxSocketBase::Init()
785 m_type
= wxSOCKET_UNINIT
;
796 m_beingDeleted
= false;
811 if ( !IsInitialized() )
813 // this Initialize() will be undone by wxSocketModule::OnExit(), all
814 // the other calls to it should be matched by a call to Shutdown()
816 wxLogError("Cannot initialize wxSocketBase");
820 wxSocketBase::wxSocketBase()
825 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
834 wxSocketBase::~wxSocketBase()
836 // Just in case the app called Destroy() *and* then deleted the socket
837 // immediately: don't leave dangling pointers.
838 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
840 traits
->RemoveFromPendingDelete(this);
842 // Shutdown and close the socket
846 // Destroy the implementation object
849 // Free the pushback buffer
854 bool wxSocketBase::Destroy()
856 // Delayed destruction: the socket will be deleted during the next idle
857 // loop iteration. This ensures that all pending events have been
859 m_beingDeleted
= true;
861 // Shutdown and close the socket
864 // Suppress events from now on
867 // schedule this object for deletion
868 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
871 // let the traits object decide what to do with us
872 traits
->ScheduleForDestroy(this);
874 else // no app or no traits
876 // in wxBase we might have no app object at all, don't leak memory
883 // ----------------------------------------------------------------------------
885 // ----------------------------------------------------------------------------
887 void wxSocketBase::SetError(wxSocketError error
)
889 m_impl
->m_error
= error
;
892 wxSocketError
wxSocketBase::LastError() const
894 return m_impl
->GetError();
897 // --------------------------------------------------------------------------
899 // --------------------------------------------------------------------------
901 // The following IO operations update m_lcount:
902 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
903 bool wxSocketBase::Close()
905 // Interrupt pending waits
911 m_establishing
= false;
915 void wxSocketBase::ShutdownOutput()
921 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
923 wxSocketReadGuard
read(this);
925 m_lcount
= DoRead(buffer
, nbytes
);
930 wxUint32
wxSocketBase::DoRead(void* buffer_
, wxUint32 nbytes
)
932 wxCHECK_MSG( m_impl
, 0, "socket must be valid" );
934 // We use pointer arithmetic here which doesn't work with void pointers.
935 char *buffer
= static_cast<char *>(buffer_
);
936 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
938 // Try the push back buffer first, even before checking whether the socket
939 // is valid to allow reading previously pushed back data from an already
941 wxUint32 total
= GetPushback(buffer
, nbytes
, false);
947 // our socket is non-blocking so Read() will return immediately if
948 // there is nothing to read yet and it's more efficient to try it first
949 // before entering DoWait() which is going to start dispatching GUI
950 // events and, even more importantly, we must do this under Windows
951 // where we're not going to get notifications about socket being ready
952 // for reading before we read all the existing data from it
953 const int ret
= !m_impl
->m_stream
|| m_connected
954 ? m_impl
->Read(buffer
, nbytes
)
958 if ( m_impl
->GetLastError() == wxSOCKET_WOULDBLOCK
)
960 // if we don't want to wait, just return immediately
961 if ( m_flags
& wxSOCKET_NOWAIT
)
964 // otherwise wait until the socket becomes ready for reading or
965 // an error occurs on it
966 if ( !DoWaitWithTimeout(wxSOCKET_INPUT_FLAG
) )
968 // and exit if the timeout elapsed before it did
969 SetError(wxSOCKET_TIMEDOUT
);
978 SetError(wxSOCKET_IOERR
);
984 // for connection-oriented (e.g. TCP) sockets we can only read
985 // 0 bytes if the other end has been closed, and for connectionless
986 // ones (UDP) this flag doesn't make sense anyhow so we can set it
987 // to true too without doing any harm
990 // we're not going to read anything else and so if we haven't read
991 // anything (or not everything in wxSOCKET_WAITALL case) already,
993 if ( (m_flags
& wxSOCKET_WAITALL
) || !total
)
994 SetError(wxSOCKET_IOERR
);
1000 // if we are happy to read something and not the entire nbytes bytes,
1002 if ( !(m_flags
& wxSOCKET_WAITALL
) )
1012 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
1016 unsigned char sig
[4];
1017 unsigned char len
[4];
1020 wxSocketReadGuard
read(this);
1022 wxSocketWaitModeChanger
changeFlags(this, wxSOCKET_WAITALL
);
1025 if ( DoRead(&msg
, sizeof(msg
)) == sizeof(msg
) )
1027 wxUint32 sig
= (wxUint32
)msg
.sig
[0];
1028 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
1029 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
1030 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
1032 if ( sig
== 0xfeeddead )
1034 wxUint32 len
= (wxUint32
)msg
.len
[0];
1035 len
|= (wxUint32
)(msg
.len
[1] << 8);
1036 len
|= (wxUint32
)(msg
.len
[2] << 16);
1037 len
|= (wxUint32
)(msg
.len
[3] << 24);
1042 len2
= len
- nbytes
;
1048 // Don't attempt to read if the msg was zero bytes long.
1049 m_lcount
= len
? DoRead(buffer
, len
) : 0;
1053 char discard_buffer
[MAX_DISCARD_SIZE
];
1056 // NOTE: discarded bytes don't add to m_lcount.
1059 discard_len
= len2
> MAX_DISCARD_SIZE
1062 discard_len
= DoRead(discard_buffer
, (wxUint32
)discard_len
);
1063 len2
-= (wxUint32
)discard_len
;
1065 while ((discard_len
> 0) && len2
);
1068 if ( !len2
&& DoRead(&msg
, sizeof(msg
)) == sizeof(msg
) )
1070 sig
= (wxUint32
)msg
.sig
[0];
1071 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
1072 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
1073 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
1075 if ( sig
== 0xdeadfeed )
1082 SetError(wxSOCKET_IOERR
);
1087 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
1089 wxSocketReadGuard
read(this);
1091 m_lcount
= DoRead(buffer
, nbytes
);
1093 Pushback(buffer
, m_lcount
);
1098 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
1100 wxSocketWriteGuard
write(this);
1102 m_lcount
= DoWrite(buffer
, nbytes
);
1107 // This function is a mirror image of DoRead() except that it doesn't use the
1108 // push back buffer and doesn't treat 0 return value specially (normally this
1109 // shouldn't happen at all here), so please see comments there for explanations
1110 wxUint32
wxSocketBase::DoWrite(const void *buffer_
, wxUint32 nbytes
)
1112 wxCHECK_MSG( m_impl
, 0, "socket must be valid" );
1114 const char *buffer
= static_cast<const char *>(buffer_
);
1115 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
1120 if ( m_impl
->m_stream
&& !m_connected
)
1122 if ( (m_flags
& wxSOCKET_WAITALL
) || !total
)
1123 SetError(wxSOCKET_IOERR
);
1127 const int ret
= m_impl
->Write(buffer
, nbytes
);
1130 if ( m_impl
->GetLastError() == wxSOCKET_WOULDBLOCK
)
1132 if ( m_flags
& wxSOCKET_NOWAIT
)
1135 if ( !DoWaitWithTimeout(wxSOCKET_OUTPUT_FLAG
) )
1137 SetError(wxSOCKET_TIMEDOUT
);
1143 else // "real" error
1145 SetError(wxSOCKET_IOERR
);
1152 if ( !(m_flags
& wxSOCKET_WAITALL
) )
1162 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
1166 unsigned char sig
[4];
1167 unsigned char len
[4];
1170 wxSocketWriteGuard
write(this);
1172 wxSocketWaitModeChanger
changeFlags(this, wxSOCKET_WAITALL
);
1174 msg
.sig
[0] = (unsigned char) 0xad;
1175 msg
.sig
[1] = (unsigned char) 0xde;
1176 msg
.sig
[2] = (unsigned char) 0xed;
1177 msg
.sig
[3] = (unsigned char) 0xfe;
1179 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
1180 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
1181 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
1182 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
1185 if ( DoWrite(&msg
, sizeof(msg
)) == sizeof(msg
) )
1187 m_lcount
= DoWrite(buffer
, nbytes
);
1188 if ( m_lcount
== nbytes
)
1190 msg
.sig
[0] = (unsigned char) 0xed;
1191 msg
.sig
[1] = (unsigned char) 0xfe;
1192 msg
.sig
[2] = (unsigned char) 0xad;
1193 msg
.sig
[3] = (unsigned char) 0xde;
1197 msg
.len
[3] = (char) 0;
1199 if ( DoWrite(&msg
, sizeof(msg
)) == sizeof(msg
))
1205 SetError(wxSOCKET_IOERR
);
1210 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
1213 Pushback(buffer
, nbytes
);
1215 SetError(wxSOCKET_NOERROR
);
1221 wxSocketBase
& wxSocketBase::Discard()
1223 char *buffer
= new char[MAX_DISCARD_SIZE
];
1227 wxSocketReadGuard
read(this);
1229 wxSocketWaitModeChanger
changeFlags(this, wxSOCKET_NOWAIT
);
1233 ret
= DoRead(buffer
, MAX_DISCARD_SIZE
);
1236 while (ret
== MAX_DISCARD_SIZE
);
1240 SetError(wxSOCKET_NOERROR
);
1245 // --------------------------------------------------------------------------
1247 // --------------------------------------------------------------------------
1250 This function will check for the events specified in the flags parameter,
1251 and it will return a mask indicating which operations can be performed.
1253 wxSocketEventFlags
wxSocketImpl::Select(wxSocketEventFlags flags
,
1254 const timeval
*timeout
)
1256 if ( m_fd
== INVALID_SOCKET
)
1257 return (wxSOCKET_LOST_FLAG
& flags
);
1263 tv
.tv_sec
= tv
.tv_usec
= 0;
1265 // prepare the FD sets, passing NULL for the one(s) we don't use
1267 readfds
, *preadfds
= NULL
,
1268 writefds
, *pwritefds
= NULL
,
1269 exceptfds
; // always want to know about errors
1271 if ( flags
& wxSOCKET_INPUT_FLAG
)
1273 preadfds
= &readfds
;
1274 wxFD_ZERO(preadfds
);
1275 wxFD_SET(m_fd
, preadfds
);
1278 // when using non-blocking connect() the socket becomes connected
1279 // (successfully or not) when it becomes writable
1280 if ( flags
& (wxSOCKET_OUTPUT_FLAG
| wxSOCKET_CONNECTION_FLAG
) )
1282 pwritefds
= &writefds
;
1283 wxFD_ZERO(pwritefds
);
1284 wxFD_SET(m_fd
, pwritefds
);
1287 wxFD_ZERO(&exceptfds
);
1288 wxFD_SET(m_fd
, &exceptfds
);
1290 const int rc
= select(m_fd
+ 1, preadfds
, pwritefds
, &exceptfds
, &tv
);
1292 // check for errors first
1293 if ( rc
== -1 || wxFD_ISSET(m_fd
, &exceptfds
) )
1295 m_establishing
= false;
1297 return wxSOCKET_LOST_FLAG
& flags
;
1303 wxASSERT_MSG( rc
== 1, "unexpected select() return value" );
1305 wxSocketEventFlags detected
= 0;
1306 if ( preadfds
&& wxFD_ISSET(m_fd
, preadfds
) )
1307 detected
|= wxSOCKET_INPUT_FLAG
;
1309 if ( pwritefds
&& wxFD_ISSET(m_fd
, pwritefds
) )
1311 // check for the case of non-blocking connect()
1312 if ( m_establishing
&& !m_server
)
1315 SOCKOPTLEN_T len
= sizeof(error
);
1316 m_establishing
= false;
1317 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1320 detected
= wxSOCKET_LOST_FLAG
;
1322 detected
|= wxSOCKET_CONNECTION_FLAG
;
1324 else // not called to get non-blocking connect() status
1326 detected
|= wxSOCKET_OUTPUT_FLAG
;
1330 return detected
& flags
;
1334 wxSocketBase::DoWait(long seconds
, long milliseconds
, wxSocketEventFlags flags
)
1336 // Use either the provided timeout or the default timeout value associated
1337 // with this socket.
1339 // TODO: allow waiting forever, see #9443
1340 const long timeout
= seconds
== -1 ? m_timeout
* 1000
1341 : seconds
* 1000 + milliseconds
;
1343 return DoWait(timeout
, flags
);
1347 wxSocketBase::DoWait(long timeout
, wxSocketEventFlags flags
)
1349 wxCHECK_MSG( m_impl
, -1, "can't wait on invalid socket" );
1351 // we're never going to become ready if we're not connected (any more)
1352 if ( !m_connected
&& !m_establishing
)
1355 // This can be set to true from Interrupt() to exit this function a.s.a.p.
1356 m_interrupt
= false;
1359 const wxMilliClock_t timeEnd
= wxGetLocalTimeMillis() + timeout
;
1361 // Get the active event loop which we'll use for the message dispatching
1362 // when running in the main thread unless this was explicitly disabled by
1363 // setting wxSOCKET_BLOCK flag
1364 wxEventLoopBase
*eventLoop
;
1365 if ( !(m_flags
& wxSOCKET_BLOCK
) && wxIsMainThread() )
1367 eventLoop
= wxEventLoop::GetActive();
1369 else // in worker thread
1371 // We never dispatch messages from threads other than the main one.
1375 // Make sure the events we're interested in are enabled before waiting for
1376 // them: this is really necessary here as otherwise this could happen:
1377 // 1. DoRead(wxSOCKET_WAITALL) is called
1378 // 2. There is nothing to read so DoWait(wxSOCKET_INPUT_FLAG) is called
1379 // 3. Some, but not all data appears, wxSocketImplUnix::OnReadWaiting()
1380 // is called and wxSOCKET_INPUT_FLAG events are disabled in it
1381 // 4. Because of wxSOCKET_WAITALL we call DoWait() again but the events
1382 // are still disabled and we block forever
1384 // More elegant solution would be nice but for now simply re-enabling the
1385 // events here will do
1386 m_impl
->ReenableEvents(flags
& (wxSOCKET_INPUT_FLAG
| wxSOCKET_OUTPUT_FLAG
));
1389 // Wait until we receive the event we're waiting for or the timeout expires
1390 // (but note that we always execute the loop at least once, even if timeout
1391 // is 0 as this is used for polling)
1393 for ( bool firstTime
= true; !m_interrupt
; firstTime
= false )
1395 long timeLeft
= wxMilliClockToLong(timeEnd
- wxGetLocalTimeMillis());
1404 wxSocketEventFlags events
;
1407 // reset them before starting to wait
1410 eventLoop
->DispatchTimeout(timeLeft
);
1412 events
= m_eventsgot
;
1414 else // no event loop or waiting in another thread
1416 // as explained below, we should always check for wxSOCKET_LOST_FLAG
1418 SetTimeValFromMS(tv
, timeLeft
);
1419 events
= m_impl
->Select(flags
| wxSOCKET_LOST_FLAG
, &tv
);
1422 // always check for wxSOCKET_LOST_FLAG, even if flags doesn't include
1423 // it, as continuing to wait for anything else after getting it is
1425 if ( events
& wxSOCKET_LOST_FLAG
)
1427 m_connected
= false;
1428 m_establishing
= false;
1433 // otherwise mask out the bits we're not interested in
1436 // Incoming connection (server) or connection established (client)?
1437 if ( events
& wxSOCKET_CONNECTION_FLAG
)
1440 m_establishing
= false;
1445 // Data available or output buffer ready?
1446 if ( (events
& wxSOCKET_INPUT_FLAG
) || (events
& wxSOCKET_OUTPUT_FLAG
) )
1456 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
1458 return DoWait(seconds
, milliseconds
,
1459 wxSOCKET_INPUT_FLAG
|
1460 wxSOCKET_OUTPUT_FLAG
|
1461 wxSOCKET_CONNECTION_FLAG
) != 0;
1464 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
1466 // Check pushback buffer before entering DoWait
1470 // Check if the socket is not already ready for input, if it is, there is
1471 // no need to start waiting for it (worse, we'll actually never get a
1472 // notification about the socket becoming ready if it is already under
1474 if ( m_impl
->Select(wxSOCKET_INPUT_FLAG
) )
1477 return DoWait(seconds
, milliseconds
, wxSOCKET_INPUT_FLAG
) != 0;
1481 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
1483 if ( m_impl
->Select(wxSOCKET_OUTPUT_FLAG
) )
1486 return DoWait(seconds
, milliseconds
, wxSOCKET_OUTPUT_FLAG
) != 0;
1489 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
1491 return DoWait(seconds
, milliseconds
, wxSOCKET_LOST_FLAG
) == -1;
1494 // --------------------------------------------------------------------------
1496 // --------------------------------------------------------------------------
1499 // Get local or peer address
1502 bool wxSocketBase::GetPeer(wxSockAddress
& addr
) const
1504 wxCHECK_MSG( m_impl
, false, "invalid socket" );
1506 const wxSockAddressImpl
& peer
= m_impl
->GetPeer();
1510 addr
.SetAddress(peer
);
1515 bool wxSocketBase::GetLocal(wxSockAddress
& addr
) const
1517 wxCHECK_MSG( m_impl
, false, "invalid socket" );
1519 const wxSockAddressImpl
& local
= m_impl
->GetLocal();
1520 if ( !local
.IsOk() )
1523 addr
.SetAddress(local
);
1529 // Save and restore socket state
1532 void wxSocketBase::SaveState()
1534 wxSocketState
*state
;
1536 state
= new wxSocketState();
1538 state
->m_flags
= m_flags
;
1539 state
->m_notify
= m_notify
;
1540 state
->m_eventmask
= m_eventmask
;
1541 state
->m_clientData
= m_clientData
;
1543 m_states
.Append(state
);
1546 void wxSocketBase::RestoreState()
1548 wxList::compatibility_iterator node
;
1549 wxSocketState
*state
;
1551 node
= m_states
.GetLast();
1555 state
= (wxSocketState
*)node
->GetData();
1557 m_flags
= state
->m_flags
;
1558 m_notify
= state
->m_notify
;
1559 m_eventmask
= state
->m_eventmask
;
1560 m_clientData
= state
->m_clientData
;
1562 m_states
.Erase(node
);
1567 // Timeout and flags
1570 void wxSocketBase::SetTimeout(long seconds
)
1572 m_timeout
= seconds
;
1575 m_impl
->SetTimeout(m_timeout
* 1000);
1578 void wxSocketBase::SetFlags(wxSocketFlags flags
)
1580 // Do some sanity checking on the flags used: not all values can be used
1582 wxASSERT_MSG( !(flags
& wxSOCKET_NOWAIT
) ||
1583 !(flags
& (wxSOCKET_WAITALL
| wxSOCKET_BLOCK
)),
1584 "Using wxSOCKET_WAITALL or wxSOCKET_BLOCK with "
1585 "wxSOCKET_NOWAIT doesn't make sense" );
1591 // --------------------------------------------------------------------------
1593 // --------------------------------------------------------------------------
1595 void wxSocketBase::OnRequest(wxSocketNotify notification
)
1597 wxSocketEventFlags flag
= 0;
1598 switch ( notification
)
1600 case wxSOCKET_INPUT
:
1601 flag
= wxSOCKET_INPUT_FLAG
;
1604 case wxSOCKET_OUTPUT
:
1605 flag
= wxSOCKET_OUTPUT_FLAG
;
1608 case wxSOCKET_CONNECTION
:
1609 flag
= wxSOCKET_CONNECTION_FLAG
;
1613 flag
= wxSOCKET_LOST_FLAG
;
1617 wxFAIL_MSG( "unknown wxSocket notification" );
1620 // if we lost the connection the socket is now closed
1621 if ( notification
== wxSOCKET_LOST
)
1624 // remember the events which were generated for this socket, we're going to
1625 // use this in DoWait()
1626 m_eventsgot
|= flag
;
1628 // send the wx event if enabled and we're interested in it
1629 if ( m_notify
&& (m_eventmask
& flag
) && m_handler
)
1631 // don't generate the events when we're inside DoWait() called from our
1632 // own code as we are going to consume the data that has just become
1633 // available ourselves and the user code won't see it at all
1634 if ( (notification
== wxSOCKET_INPUT
&& m_reading
) ||
1635 (notification
== wxSOCKET_OUTPUT
&& m_writing
) )
1640 wxSocketEvent
event(m_id
);
1641 event
.m_event
= notification
;
1642 event
.m_clientData
= m_clientData
;
1643 event
.SetEventObject(this);
1645 m_handler
->AddPendingEvent(event
);
1649 void wxSocketBase::Notify(bool notify
)
1654 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
1656 m_eventmask
= flags
;
1659 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
1661 m_handler
= &handler
;
1665 // --------------------------------------------------------------------------
1667 // --------------------------------------------------------------------------
1669 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
1673 if (m_unread
== NULL
)
1674 m_unread
= malloc(size
);
1679 tmp
= malloc(m_unrd_size
+ size
);
1680 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1686 m_unrd_size
+= size
;
1688 memcpy(m_unread
, buffer
, size
);
1691 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1693 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
1698 if (size
> (m_unrd_size
-m_unrd_cur
))
1699 size
= m_unrd_size
-m_unrd_cur
;
1701 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1706 if (m_unrd_size
== m_unrd_cur
)
1719 // ==========================================================================
1721 // ==========================================================================
1723 // --------------------------------------------------------------------------
1725 // --------------------------------------------------------------------------
1727 wxSocketServer::wxSocketServer(const wxSockAddress
& addr
,
1728 wxSocketFlags flags
)
1729 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1731 wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") );
1733 m_impl
= wxSocketImpl::Create(*this);
1737 wxLogTrace( wxTRACE_Socket
, _T("*** Failed to create m_impl") );
1741 // Setup the socket as server
1742 m_impl
->SetLocal(addr
.GetAddress());
1744 if (GetFlags() & wxSOCKET_REUSEADDR
) {
1745 m_impl
->SetReusable();
1747 if (GetFlags() & wxSOCKET_BROADCAST
) {
1748 m_impl
->SetBroadcast();
1750 if (GetFlags() & wxSOCKET_NOBIND
) {
1751 m_impl
->DontDoBind();
1754 if (m_impl
->CreateServer() != wxSOCKET_NOERROR
)
1759 wxLogTrace( wxTRACE_Socket
, _T("*** CreateServer() failed") );
1763 wxLogTrace( wxTRACE_Socket
, _T("wxSocketServer on fd %d"), m_impl
->m_fd
);
1766 // --------------------------------------------------------------------------
1768 // --------------------------------------------------------------------------
1770 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1772 if ( !m_impl
|| (m_impl
->m_fd
== INVALID_SOCKET
) || !m_impl
->IsServer() )
1774 wxFAIL_MSG( "can only be called for a valid server socket" );
1776 SetError(wxSOCKET_INVSOCK
);
1783 // wait until we get a connection
1784 if ( !m_impl
->SelectWithTimeout(wxSOCKET_INPUT_FLAG
) )
1786 SetError(wxSOCKET_TIMEDOUT
);
1792 sock
.m_impl
= m_impl
->Accept(sock
);
1796 SetError(m_impl
->GetLastError());
1801 sock
.m_type
= wxSOCKET_BASE
;
1802 sock
.m_connected
= true;
1807 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1809 wxSocketBase
* sock
= new wxSocketBase();
1811 sock
->SetFlags(m_flags
);
1813 if (!AcceptWith(*sock
, wait
))
1822 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1824 return DoWait(seconds
, milliseconds
, wxSOCKET_CONNECTION_FLAG
) == 1;
1827 bool wxSocketBase::GetOption(int level
, int optname
, void *optval
, int *optlen
)
1829 wxASSERT_MSG( m_impl
, _T("Socket not initialised") );
1831 SOCKOPTLEN_T lenreal
= *optlen
;
1832 if ( getsockopt(m_impl
->m_fd
, level
, optname
,
1833 static_cast<char *>(optval
), &lenreal
) != 0 )
1842 wxSocketBase::SetOption(int level
, int optname
, const void *optval
, int optlen
)
1844 wxASSERT_MSG( m_impl
, _T("Socket not initialised") );
1846 return setsockopt(m_impl
->m_fd
, level
, optname
,
1847 static_cast<const char *>(optval
), optlen
) == 0;
1850 bool wxSocketBase::SetLocal(const wxIPV4address
& local
)
1852 m_localAddress
= local
;
1857 // ==========================================================================
1859 // ==========================================================================
1861 // --------------------------------------------------------------------------
1863 // --------------------------------------------------------------------------
1865 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1866 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1868 m_initialRecvBufferSize
=
1869 m_initialSendBufferSize
= -1;
1872 // --------------------------------------------------------------------------
1874 // --------------------------------------------------------------------------
1876 bool wxSocketClient::DoConnect(const wxSockAddress
& remote
,
1877 const wxSockAddress
* local
,
1882 // Shutdown and destroy the old socket
1887 m_connected
= false;
1888 m_establishing
= false;
1890 // Create and set up the new one
1891 m_impl
= wxSocketImpl::Create(*this);
1895 // Reuse makes sense for clients too, if we are trying to rebind to the same port
1896 if (GetFlags() & wxSOCKET_REUSEADDR
)
1897 m_impl
->SetReusable();
1898 if (GetFlags() & wxSOCKET_BROADCAST
)
1899 m_impl
->SetBroadcast();
1900 if (GetFlags() & wxSOCKET_NOBIND
)
1901 m_impl
->DontDoBind();
1903 // Bind to the local IP address and port, when provided or if one had been
1905 if ( !local
&& m_localAddress
.GetAddress().IsOk() )
1906 local
= &m_localAddress
;
1909 m_impl
->SetLocal(local
->GetAddress());
1911 m_impl
->SetInitialSocketBuffers(m_initialRecvBufferSize
, m_initialSendBufferSize
);
1913 m_impl
->SetPeer(remote
.GetAddress());
1915 // Finally do create the socket and connect to the peer
1916 const wxSocketError err
= m_impl
->CreateClient(wait
);
1918 if ( err
!= wxSOCKET_NOERROR
)
1920 if ( err
== wxSOCKET_WOULDBLOCK
)
1922 wxASSERT_MSG( !wait
, "shouldn't get this for blocking connect" );
1924 m_establishing
= true;
1934 bool wxSocketClient::Connect(const wxSockAddress
& remote
, bool wait
)
1936 return DoConnect(remote
, NULL
, wait
);
1939 bool wxSocketClient::Connect(const wxSockAddress
& remote
,
1940 const wxSockAddress
& local
,
1943 return DoConnect(remote
, &local
, wait
);
1946 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1950 // this happens if the initial attempt to connect succeeded without
1955 wxCHECK_MSG( m_establishing
&& m_impl
, false,
1956 "No connection establishment attempt in progress" );
1958 // notice that we return true even if DoWait() returned -1, i.e. if an
1959 // error occurred and connection was lost: this is intentional as we should
1960 // return false only if timeout expired without anything happening
1961 return DoWait(seconds
, milliseconds
, wxSOCKET_CONNECTION_FLAG
) != 0;
1964 // ==========================================================================
1966 // ==========================================================================
1968 wxDatagramSocket::wxDatagramSocket( const wxSockAddress
& addr
,
1969 wxSocketFlags flags
)
1970 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1972 // Create the socket
1973 m_impl
= wxSocketImpl::Create(*this);
1978 // Setup the socket as non connection oriented
1979 m_impl
->SetLocal(addr
.GetAddress());
1980 if (flags
& wxSOCKET_REUSEADDR
)
1982 m_impl
->SetReusable();
1984 if (GetFlags() & wxSOCKET_BROADCAST
)
1986 m_impl
->SetBroadcast();
1988 if (GetFlags() & wxSOCKET_NOBIND
)
1990 m_impl
->DontDoBind();
1993 if ( m_impl
->CreateUDP() != wxSOCKET_NOERROR
)
2000 // Initialize all stuff
2001 m_connected
= false;
2002 m_establishing
= false;
2005 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
2014 wxDatagramSocket
& wxDatagramSocket::SendTo( const wxSockAddress
& addr
,
2018 wxASSERT_MSG( m_impl
, _T("Socket not initialised") );
2020 m_impl
->SetPeer(addr
.GetAddress());
2025 // ==========================================================================
2027 // ==========================================================================
2029 class wxSocketModule
: public wxModule
2032 virtual bool OnInit()
2034 // wxSocketBase will call Initialize() itself only if sockets are
2035 // really used, don't do it from here
2039 virtual void OnExit()
2041 if ( wxSocketBase::IsInitialized() )
2042 wxSocketBase::Shutdown();
2046 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
2049 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)
2051 #if defined(wxUSE_SELECT_DISPATCHER) && wxUSE_SELECT_DISPATCHER
2052 // NOTE: we need to force linking against socketiohandler.cpp otherwise in
2053 // static builds of wxWidgets the ManagerSetter::ManagerSetter ctor
2054 // contained there wouldn't be ever called
2055 wxFORCE_LINK_MODULE( socketiohandler
)
2058 #endif // wxUSE_SOCKETS