]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/socket.cpp
wxLogStderr sends output to debugger too under Windows
[wxWidgets.git] / src / common / socket.cpp
index e9a222b142ad3735b4a7388d97a494f1b4e09cda..4f3b8085e5cda925c57a111978f9128c9fe0fe21 100644 (file)
@@ -24,6 +24,8 @@ typedef int socklen_t ;
 #pragma hdrstop
 #endif
 
+#if wxUSE_SOCKETS
+
 /////////////////////////////////////////////////////////////////////////////
 // wxWindows headers
 /////////////////////////////////////////////////////////////////////////////
@@ -363,8 +365,10 @@ wxSocketBase& wxSocketBase::Read(char* buffer, size_t nbytes)
 
   // If we have got the whole needed buffer or if we don't want to
   // wait then it returns immediately.
-  if (!nbytes || (m_lcount && !(m_flags & WAITALL)) )
+  if (!nbytes || (count && !(m_flags & WAITALL)) ) {
+    m_lcount = count;
     return *this;
+  }
 
   m_lcount = 0;
   WantBuffer(buffer, nbytes, EVT_READ);
@@ -681,6 +685,20 @@ bool wxSocketBase::WaitForLost(long seconds, long microseconds)
 // --------- wxSocketBase callback management -------------------
 // --------------------------------------------------------------
 
+#ifdef __WXGTK__
+void wxPrereadSocket(wxSocketBase *sock)
+{
+  char tmp_buf[1024];
+  int got = 0;
+
+  do {
+    got = recv(sock->m_fd, tmp_buf, 1024, 0);
+    if (got > 0)
+      sock->CreatePushbackAfter(tmp_buf, got);
+  } while (got > 0);
+}
+#endif
+
 #if defined(__WXMOTIF__) || defined(__WXXT__) || defined(__WXGTK__)
 #if defined(__WXMOTIF__) || defined(__WXXT__)
 static void wx_socket_read(XtPointer client, int *fid,
@@ -708,6 +726,15 @@ static void wx_socket_read(gpointer client, gint fd,
   {
     if (!(sock->NeededReq() & wxSocketBase::REQ_READ))
     {
+#ifdef __WXGTK__
+      // We can't exit from the GDK main loop because it doesn't accept
+      // destroying input event while we are in a event dispatch.
+      // So we will preread socket and we put the data in the pushback.
+      wxPrereadSocket(sock);
+      // Then we set the socket as BLOCKING
+      int flag = 0;
+      ioctl(fd, FIONBIO, &flag);
+#endif
       return;
     }
 
@@ -1119,7 +1146,10 @@ void wxSocketBase::CreatePushbackAfter(const char *buffer, size_t size)
 {
   char *curr_pos;
 
-  m_unread = (char *) realloc(m_unread, m_unrd_size+size);
+  if (m_unread != NULL)
+    m_unread = (char *) realloc(m_unread, m_unrd_size+size);
+  else
+    m_unread = (char *) malloc(size);
   curr_pos = m_unread + m_unrd_size;
 
   memcpy(curr_pos, buffer, size);
@@ -1894,3 +1924,6 @@ void wxMacProcessSocketEvents()
 
 #endif
   // __WXSTUBS__
+
+#endif
+  // wxUSE_SOCKETS