]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/socket.cpp
fixed memory leak
[wxWidgets.git] / src / common / socket.cpp
index 4f265961933249a0e042ff6d502a94b0766bf758..8edfe18588d0f9d5ee91a334e6edc2ab924f3e78 100644 (file)
@@ -122,12 +122,26 @@ bool wxSocketBase::Initialize()
 {
     if ( !m_countInit++ )
     {
+        /*
+            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.
+        */
+        wxASSERT_MSG( wxThread::IsMain(),
+            wxT("Call wxSocketBase::Initialize() from the main thread first!"));
+
         wxAppTraits *traits = wxAppConsole::GetInstance() ?
                               wxAppConsole::GetInstance()->GetTraits() : NULL;
-        GSocketGUIFunctionsTable *functions = 
+        GSocketGUIFunctionsTable *functions =
             traits ? traits->GetSocketGUIFunctionsTable() : NULL;
         GSocket_SetGUIFunctions(functions);
-        
+
         if ( !GSocket_Init() )
         {
             m_countInit--;
@@ -680,7 +694,7 @@ bool wxSocketBase::_Wait(long seconds,
 
 #if !defined(wxUSE_GUI) || !wxUSE_GUI
   GSocket_SetTimeout(m_socket, timeout);
-#endif 
+#endif
 
   // Wait in an active polling loop.
   //
@@ -754,6 +768,7 @@ bool wxSocketBase::WaitForRead(long seconds, long milliseconds)
                                       GSOCK_LOST_FLAG);
 }
 
+
 bool wxSocketBase::WaitForWrite(long seconds, long milliseconds)
 {
   return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG);
@@ -839,7 +854,7 @@ void wxSocketBase::RestoreState()
   m_notify     = state->m_notify;
   m_eventmask  = state->m_eventmask;
   m_clientData = state->m_clientData;
-  
+
   m_states.Erase(node);
   delete state;
 }
@@ -1053,6 +1068,11 @@ wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
         // Setup the socket as server
 
     GSocket_SetLocal(m_socket, addr_man.GetAddress());
+    
+    if (GetFlags() & wxSOCKET_REUSEADDR) {
+        GSocket_SetReusable(m_socket);
+    }
+
     if (GSocket_SetServer(m_socket) != GSOCK_NOERROR)
     {
         GSocket_destroy(m_socket);
@@ -1126,6 +1146,27 @@ bool wxSocketServer::WaitForAccept(long seconds, long milliseconds)
   return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG);
 }
 
+bool wxSocketBase::GetOption(int level, int optname, void *optval, int *optlen)
+{
+    if (GSocket_GetSockOpt(m_socket, level, optname, optval, optlen)
+        != GSOCK_NOERROR)
+    {
+        return FALSE;
+    }
+    return TRUE;
+}
+
+bool wxSocketBase::SetOption(int level, int optname, const void *optval,
+                              int optlen)
+{
+    if (GSocket_SetSockOpt(m_socket, level, optname, optval, optlen)
+        != GSOCK_NOERROR)
+    {
+        return FALSE;
+    }
+    return TRUE;
+}
+
 // ==========================================================================
 // wxSocketClient
 // ==========================================================================
@@ -1221,8 +1262,10 @@ wxDatagramSocket::wxDatagramSocket( wxSockAddress& addr,
   m_socket = GSocket_new();
 
   if(!m_socket)
+  {
+    wxASSERT_MSG( 0, _T("datagram socket not new'd") );
     return;
-
+  }
   // Setup the socket as non connection oriented
   GSocket_SetLocal(m_socket, addr.GetAddress());
   if( GSocket_SetNonOriented(m_socket) != GSOCK_NOERROR )