+// Conditionally make the socket non-blocking for the lifetime of this object.
+class wxSocketUnblocker
+{
+public:
+ wxSocketUnblocker(wxSocketImpl *socket, bool unblock = true)
+ : m_impl(socket),
+ m_unblock(unblock)
+ {
+ if ( m_unblock )
+ m_impl->SetNonBlocking(true);
+ }
+
+ ~wxSocketUnblocker()
+ {
+ if ( m_unblock )
+ m_impl->SetNonBlocking(false);
+ }
+
+private:
+ wxSocketImpl * const m_impl;
+ bool m_unblock;
+
+ DECLARE_NO_COPY_CLASS(wxSocketUnblocker)
+};
+
+// ============================================================================
+// wxSocketManager
+// ============================================================================
+
+wxSocketManager *wxSocketManager::ms_manager = NULL;
+
+/* static */
+void wxSocketManager::Set(wxSocketManager *manager)
+{
+ wxASSERT_MSG( !ms_manager, "too late to set manager now" );
+
+ ms_manager = manager;
+}
+
+/* static */
+void wxSocketManager::Init()
+{
+ wxASSERT_MSG( !ms_manager, "shouldn't be initialized twice" );
+
+ /*
+ Details: Initialize() creates a hidden window as a sink for socket
+ events, such as 'read completed'. wxMSW has only one message loop
+ for the main thread. If Initialize is called in a secondary thread,
+ the socket window will be created for the secondary thread, but
+ since there is no message loop on this thread, it will never
+ receive events and all socket operations will time out.
+ BTW, the main thread must not be stopped using sleep or block
+ on a semaphore (a bad idea in any case) or socket operations
+ will time out.
+
+ On the Mac side, Initialize() stores a pointer to the CFRunLoop for
+ the main thread. Because secondary threads do not have run loops,
+ adding event notifications to the "Current" loop would have no
+ effect at all, events would never fire.
+ */
+ wxASSERT_MSG( wxIsMainThread(),
+ "sockets must be initialized from the main thread" );
+
+ wxAppConsole * const app = wxAppConsole::GetInstance();
+ wxCHECK_RET( app, "sockets can't be initialized without wxApp" );
+
+ ms_manager = app->GetTraits()->GetSocketManager();
+}
+
+// ==========================================================================
+// wxSocketImpl
+// ==========================================================================
+
+wxSocketImpl::wxSocketImpl(wxSocketBase& wxsocket)
+ : m_wxsocket(&wxsocket)
+{
+ m_fd = INVALID_SOCKET;
+ m_detected = 0;
+ m_local = NULL;
+ m_peer = NULL;
+ m_error = wxSOCKET_NOERROR;
+ m_server = false;
+ m_stream = true;
+ m_non_blocking = false;
+
+ SetTimeout(wxsocket.GetTimeout() * 1000);
+
+ m_establishing = false;
+ m_reusable = false;
+ m_broadcast = false;
+ m_dobind = true;
+ m_initialRecvBufferSize = -1;
+ m_initialSendBufferSize = -1;
+}
+
+wxSocketImpl::~wxSocketImpl()
+{
+ if (m_fd != INVALID_SOCKET)
+ Shutdown();
+
+ if (m_local)
+ GAddress_destroy(m_local);
+
+ if (m_peer)
+ GAddress_destroy(m_peer);
+}
+
+bool wxSocketImpl::PreCreateCheck(GAddress *addr)
+{
+ if ( m_fd != INVALID_SOCKET )
+ {
+ m_error = wxSOCKET_INVSOCK;
+ return false;
+ }
+
+ if ( !addr || !addr->m_addr )
+ {
+ m_error = wxSOCKET_INVADDR;
+ return false;
+ }
+
+ return true;
+}
+
+void wxSocketImpl::PostCreation()
+{
+ // FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option
+#ifdef SO_NOSIGPIPE
+ EnableSocketOption(SO_NOSIGPIPE);
+#endif
+
+ if ( m_reusable )
+ EnableSocketOption(SO_REUSEADDR);
+
+ if ( m_broadcast )
+ {
+ wxASSERT_MSG( !m_stream, "broadcasting is for datagram sockets only" );
+
+ EnableSocketOption(SO_BROADCAST);
+ }
+
+ if ( m_initialRecvBufferSize >= 0 )
+ SetSocketOption(SO_RCVBUF, m_initialRecvBufferSize);
+ if ( m_initialSendBufferSize >= 0 )
+ SetSocketOption(SO_SNDBUF, m_initialSendBufferSize);
+
+ // FIXME: shouldn't we check for m_non_blocking here? as it is now, all our
+ // sockets are non-blocking
+ UnblockAndRegisterWithEventLoop();
+}
+
+wxSocketError wxSocketImpl::UpdateLocalAddress()
+{
+ WX_SOCKLEN_T lenAddr;
+ if ( getsockname(m_fd, m_local->m_addr, &lenAddr) != 0 )
+ {
+ Close();
+ m_error = wxSOCKET_IOERR;
+ return m_error;
+ }
+
+ m_local->m_len = lenAddr;
+
+ return wxSOCKET_NOERROR;
+}
+
+wxSocketError wxSocketImpl::CreateServer()
+{
+ if ( !PreCreateCheck(m_local) )
+ return m_error;
+
+ m_server = true;
+ m_stream = true;
+
+ // do create the socket
+ m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0);
+
+ if ( m_fd == INVALID_SOCKET )
+ {
+ m_error = wxSOCKET_IOERR;
+ return wxSOCKET_IOERR;
+ }
+
+ PostCreation();
+
+ // and then bind to and listen on it
+ //
+ // FIXME: should we test for m_dobind here?
+ if ( bind(m_fd, m_local->m_addr, m_local->m_len) != 0 )
+ m_error = wxSOCKET_IOERR;
+
+ if ( IsOk() )
+ {
+ if ( listen(m_fd, 5) != 0 )
+ m_error = wxSOCKET_IOERR;
+ }
+
+ if ( !IsOk() )
+ {
+ Close();
+ return m_error;
+ }
+
+ // finally retrieve the address we effectively bound to
+ return UpdateLocalAddress();
+}
+
+wxSocketError wxSocketImpl::CreateClient()
+{
+ if ( !PreCreateCheck(m_peer) )
+ return m_error;
+
+ m_fd = socket(m_peer->m_realfamily, SOCK_STREAM, 0);
+
+ if ( m_fd == INVALID_SOCKET )
+ {
+ m_error = wxSOCKET_IOERR;
+ return wxSOCKET_IOERR;
+ }
+
+ PostCreation();
+
+ // If a local address has been set, then bind to it before calling connect
+ if ( m_local && m_local->m_addr )
+ {
+ if ( bind(m_fd, m_local->m_addr, m_local->m_len) != 0 )
+ {
+ Close();
+ m_error = wxSOCKET_IOERR;
+ return m_error;
+ }
+ }
+
+ // Connect to the peer and handle the EWOULDBLOCK return value in
+ // platform-specific code
+ return DoHandleConnect(connect(m_fd, m_peer->m_addr, m_peer->m_len));
+}
+
+
+wxSocketError wxSocketImpl::CreateUDP()
+{
+ if ( !PreCreateCheck(m_local) )
+ return m_error;
+
+ m_stream = false;
+ m_server = false;
+
+ m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0);
+
+ if ( m_fd == INVALID_SOCKET )
+ {
+ m_error = wxSOCKET_IOERR;
+ return wxSOCKET_IOERR;
+ }
+
+ PostCreation();
+
+ if ( m_dobind )
+ {
+ if ( bind(m_fd, m_local->m_addr, m_local->m_len) != 0 )
+ {
+ Close();
+ m_error = wxSOCKET_IOERR;
+ return m_error;
+ }
+
+ return UpdateLocalAddress();
+ }
+
+ return wxSOCKET_NOERROR;
+}
+
+
+void wxSocketImpl::Close()
+{
+ if ( m_fd != INVALID_SOCKET )
+ {
+ DoClose();
+ m_fd = INVALID_SOCKET;
+ }
+}
+
+/*
+ * Disallow further read/write operations on this socket, close
+ * the fd and disable all callbacks.
+ */
+void wxSocketImpl::Shutdown()
+{
+ if ( m_fd != INVALID_SOCKET )
+ {
+ shutdown(m_fd, 1 /* SD_SEND */);
+ Close();
+ }
+
+ m_detected = wxSOCKET_LOST_FLAG;
+}
+
+/*
+ * Sets the timeout for blocking calls. Time is expressed in
+ * milliseconds.
+ */
+void wxSocketImpl::SetTimeout(unsigned long millis)
+{
+ m_timeout.tv_sec = (millis / 1000);
+ m_timeout.tv_usec = (millis % 1000) * 1000;
+}
+
+void wxSocketImpl::NotifyOnStateChange(wxSocketNotify event)
+{
+ m_wxsocket->OnRequest(event);
+}
+
+/* Address handling */
+
+/*
+ * Set or get the local or peer address for this socket. The 'set'
+ * functions return wxSOCKET_NOERROR on success, an error code otherwise.
+ * The 'get' functions return a pointer to a GAddress object on success,
+ * or NULL otherwise, in which case they set the error code of the
+ * corresponding socket.
+ *
+ * Error codes:
+ * wxSOCKET_INVSOCK - the socket is not valid.
+ * wxSOCKET_INVADDR - the address is not valid.
+ */
+wxSocketError wxSocketImpl::SetLocal(GAddress *address)
+{
+ /* the socket must be initialized, or it must be a server */
+ if (m_fd != INVALID_SOCKET && !m_server)
+ {
+ m_error = wxSOCKET_INVSOCK;
+ return wxSOCKET_INVSOCK;
+ }
+
+ /* check address */
+ if (address == NULL || address->m_family == wxSOCKET_NOFAMILY)
+ {
+ m_error = wxSOCKET_INVADDR;
+ return wxSOCKET_INVADDR;
+ }
+
+ if (m_local)
+ GAddress_destroy(m_local);
+
+ m_local = GAddress_copy(address);
+
+ return wxSOCKET_NOERROR;
+}
+
+wxSocketError wxSocketImpl::SetPeer(GAddress *address)
+{
+ /* check address */
+ if (address == NULL || address->m_family == wxSOCKET_NOFAMILY)
+ {
+ m_error = wxSOCKET_INVADDR;
+ return wxSOCKET_INVADDR;
+ }
+
+ if (m_peer)
+ GAddress_destroy(m_peer);
+
+ m_peer = GAddress_copy(address);
+
+ return wxSOCKET_NOERROR;
+}
+
+GAddress *wxSocketImpl::GetLocal()
+{
+ GAddress *address;
+ wxSockAddr addr;
+ WX_SOCKLEN_T size = sizeof(addr);
+ wxSocketError err;
+
+ /* try to get it from the m_local var first */
+ if (m_local)
+ return GAddress_copy(m_local);
+
+ /* else, if the socket is initialized, try getsockname */
+ if (m_fd == INVALID_SOCKET)
+ {
+ m_error = wxSOCKET_INVSOCK;
+ return NULL;
+ }
+
+ if (getsockname(m_fd, (sockaddr*)&addr, &size) == SOCKET_ERROR)
+ {
+ m_error = wxSOCKET_IOERR;
+ return NULL;
+ }
+
+ /* got a valid address from getsockname, create a GAddress object */
+ if ((address = GAddress_new()) == NULL)
+ {
+ m_error = wxSOCKET_MEMERR;
+ return NULL;
+ }
+
+ if ((err = _GAddress_translate_from(address, (sockaddr*)&addr, size)) != wxSOCKET_NOERROR)
+ {
+ GAddress_destroy(address);
+ m_error = err;
+ return NULL;
+ }
+
+ return address;
+}
+
+GAddress *wxSocketImpl::GetPeer()
+{
+ /* try to get it from the m_peer var */
+ if (m_peer)
+ return GAddress_copy(m_peer);
+
+ return NULL;
+}
+