]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/socket.cpp
remove unused any more wx_socket_callback()
[wxWidgets.git] / src / common / socket.cpp
index 6de0020de0ec4b13c8382137aa74eceddc6de7fd..c167ebf14b348f942709d898ee95cac68efe78b7 100644 (file)
@@ -41,6 +41,7 @@
 #include "wx/stopwatch.h"
 #include "wx/thread.h"
 #include "wx/evtloop.h"
+#include "wx/private/fd.h"
 
 // DLL options compatibility check:
 #include "wx/build.h"
@@ -152,6 +153,97 @@ void GSocketManager::Init()
     ms_manager = app->GetTraits()->GetSocketManager();
 }
 
+// ==========================================================================
+// GSocketBase
+// ==========================================================================
+
+/* static */
+GSocket *GSocketBase::Create(wxSocketBase& wxsocket)
+{
+    GSocket * const newsocket = new GSocket(wxsocket);
+    if ( !GSocketManager::Get()->Init_Socket(newsocket) )
+    {
+        delete newsocket;
+        return NULL;
+    }
+
+    return newsocket;
+}
+
+GSocketBase::GSocketBase(wxSocketBase& wxsocket)
+    : m_wxsocket(&wxsocket)
+{
+    m_fd              = INVALID_SOCKET;
+    m_detected        = 0;
+    m_local           = NULL;
+    m_peer            = NULL;
+    m_error           = GSOCK_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;
+}
+
+GSocketBase::~GSocketBase()
+{
+    if (m_fd != INVALID_SOCKET)
+        Shutdown();
+
+    if (m_local)
+        GAddress_destroy(m_local);
+
+    if (m_peer)
+        GAddress_destroy(m_peer);
+
+    // cast is ok as all GSocketBase objects we have in our code are really
+    // GSockets
+    GSocketManager::Get()->Destroy_Socket(static_cast<GSocket *>(this));
+}
+
+/* GSocket_Shutdown:
+ *  Disallow further read/write operations on this socket, close
+ *  the fd and disable all callbacks.
+ */
+void GSocketBase::Shutdown()
+{
+    if ( m_fd != INVALID_SOCKET )
+    {
+        shutdown(m_fd, 1 /* SD_SEND */);
+        Close();
+    }
+
+    m_detected = GSOCK_LOST_FLAG;
+}
+
+/* GSocket_SetTimeout:
+ *  Sets the timeout for blocking calls. Time is expressed in
+ *  milliseconds.
+ */
+void GSocketBase::SetTimeout(unsigned long millis)
+{
+#ifdef __WXMSW__
+    m_timeout.tv_sec  = (millis / 1000);
+    m_timeout.tv_usec = (millis % 1000) * 1000;
+#else
+    m_timeout = millis;
+#endif
+}
+
+void GSocketBase::NotifyOnStateChange(GSocketEvent event)
+{
+    // GSocketEvent and wxSocketNotify enums have the same elements with the
+    // same values
+    m_wxsocket->OnRequest(static_cast<wxSocketNotify>(event));
+}
+
 // ==========================================================================
 // wxSocketBase
 // ==========================================================================
@@ -314,18 +406,7 @@ bool wxSocketBase::Close()
     InterruptWait();
 
     if (m_socket)
-    {
-        // Disable callbacks
-        m_socket->UnsetCallback(
-            GSOCK_INPUT_FLAG |
-            GSOCK_OUTPUT_FLAG |
-            GSOCK_LOST_FLAG |
-            GSOCK_CONNECTION_FLAG
-        );
-
-        // Shutdown the connection
         m_socket->Shutdown();
-    }
 
     m_connected = false;
     m_establishing = false;
@@ -1071,34 +1152,6 @@ void wxSocketBase::SetFlags(wxSocketFlags flags)
 // Event handling
 // --------------------------------------------------------------------------
 
-// A note on how events are processed, which is probably the most
-// difficult thing to get working right while keeping the same API
-// and functionality for all platforms.
-//
-// When GSocket detects an event, it calls wx_socket_callback, which in
-// turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
-// object. OnRequest does some housekeeping, and if the event is to be
-// propagated to the user, it creates a new wxSocketEvent object and
-// posts it. The event is not processed immediately, but delayed with
-// AddPendingEvent instead. This is necessary in order to decouple the
-// event processing from wx_socket_callback; otherwise, subsequent IO
-// calls made from the user event handler would fail, as gtk callbacks
-// are not reentrant.
-//
-// Note that, unlike events, user callbacks (now deprecated) are _not_
-// decoupled from wx_socket_callback and thus they suffer from a variety
-// of problems. Avoid them where possible and use events instead.
-
-extern "C"
-void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket),
-                                    GSocketEvent notification,
-                                    char *cdata)
-{
-    wxSocketBase *sckobj = (wxSocketBase *)cdata;
-
-    sckobj->OnRequest((wxSocketNotify) notification);
-}
-
 void wxSocketBase::OnRequest(wxSocketNotify notification)
 {
     switch(notification)
@@ -1243,7 +1296,7 @@ wxSocketServer::wxSocketServer(const wxSockAddress& addr_man,
 {
     wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") );
 
-    m_socket = GSocket_new();
+    m_socket = GSocket::Create(*this);
 
     if (!m_socket)
     {
@@ -1274,11 +1327,6 @@ wxSocketServer::wxSocketServer(const wxSockAddress& addr_man,
         return;
     }
 
-    m_socket->SetTimeout(m_timeout * 1000);
-    m_socket->SetCallback(GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
-                                  GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
-                                  wx_socket_callback, (char *)this);
-
     wxLogTrace( wxTRACE_Socket, _T("wxSocketServer on fd %d"), m_socket->m_fd );
 }
 
@@ -1295,20 +1343,14 @@ bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait)
     // When we are finished, we put the socket to blocking mode
     // again.
     wxSocketUnblocker unblock(m_socket, !wait);
-    GSocket * const child_socket = m_socket->WaitConnection();
+    sock.m_socket = m_socket->WaitConnection(sock);
 
-    if (!child_socket)
+    if ( !sock.m_socket )
         return false;
 
     sock.m_type = wxSOCKET_BASE;
-    sock.m_socket = child_socket;
     sock.m_connected = true;
 
-    sock.m_socket->SetTimeout(sock.m_timeout * 1000);
-    sock.m_socket->SetCallback(GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
-            GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
-            wx_socket_callback, (char *)&sock);
-
     return true;
 }
 
@@ -1406,23 +1448,13 @@ bool wxSocketClient::DoConnect(const wxSockAddress& addr_man,
         delete m_socket;
     }
 
-    m_socket = GSocket_new();
+    m_socket = GSocket::Create(*this);
     m_connected = false;
     m_establishing = false;
 
     if (!m_socket)
         return false;
 
-    m_socket->SetTimeout(m_timeout * 1000);
-    m_socket->SetCallback(
-        GSOCK_INPUT_FLAG |
-        GSOCK_OUTPUT_FLAG |
-        GSOCK_LOST_FLAG |
-        GSOCK_CONNECTION_FLAG,
-        wx_socket_callback,
-        (char *)this
-    );
-
     // If wait == false, then the call should be nonblocking. When we are
     // finished, we put the socket to blocking mode again.
     wxSocketUnblocker unblock(m_socket, !wait);
@@ -1521,13 +1553,11 @@ wxDatagramSocket::wxDatagramSocket( const wxSockAddress& addr,
                 : wxSocketBase( flags, wxSOCKET_DATAGRAM )
 {
     // Create the socket
-    m_socket = GSocket_new();
+    m_socket = GSocket::Create(*this);
 
     if (!m_socket)
-    {
-        wxFAIL_MSG( _T("datagram socket not new'd") );
         return;
-    }
+
     m_socket->Notify(m_notify);
     // Setup the socket as non connection oriented
     m_socket->SetLocal(addr.GetAddress());
@@ -1553,10 +1583,6 @@ wxDatagramSocket::wxDatagramSocket( const wxSockAddress& addr,
     // Initialize all stuff
     m_connected = false;
     m_establishing = false;
-    m_socket->SetTimeout( m_timeout );
-    m_socket->SetCallback( GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
-                           GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
-                           wx_socket_callback, (char*)this );
 }
 
 wxDatagramSocket& wxDatagramSocket::RecvFrom( wxSockAddress& addr,