updates.
authorPekka Riikonen <priikone@silcnet.org>
Thu, 19 Jul 2001 12:42:08 +0000 (12:42 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Thu, 19 Jul 2001 12:42:08 +0000 (12:42 +0000)
lib/silcutil/silcschedule.c
lib/silcutil/win32/silcwin32sockconn.c

index fd53ffe237def1d36cb0e1761cbf3514a161a027..e34009b2be954d07f88b5cc54c86ed23d6f1e936 100644 (file)
@@ -277,7 +277,6 @@ static void silc_schedule_dispatch_nontimeout(SilcSchedule schedule)
     /* First check whether this fd has task in the fd queue */
     silc_mutex_lock(schedule->fd_queue->lock);
     task = silc_task_find(schedule->fd_queue, schedule->fd_list[i].fd);
-    silc_mutex_unlock(schedule->fd_queue->lock);
 
     /* If the task was found then execute its callbacks. If not then
        execute all generic tasks for that fd. */
@@ -285,7 +284,6 @@ static void silc_schedule_dispatch_nontimeout(SilcSchedule schedule)
       /* Validity of the task is checked always before and after
         execution beacuse the task might have been unregistered
         in the callback function, ie. it is not valid anymore. */
-      silc_mutex_lock(schedule->fd_queue->lock);
 
       /* Is the task ready for reading */
       if (task->valid && schedule->fd_list[i].revents & SILC_TASK_READ) {
@@ -312,6 +310,8 @@ static void silc_schedule_dispatch_nontimeout(SilcSchedule schedule)
     } else {
       /* Run generic tasks for this fd. */
 
+      silc_mutex_unlock(schedule->fd_queue->lock);
+
       silc_mutex_lock(schedule->generic_queue->lock);
       if (!schedule->generic_queue->task) {
        silc_mutex_unlock(schedule->generic_queue->lock);
index c78640308398c344bde074d1226a055db1d60693..44115731d1865fb38923db403bfde363122a2811 100644 (file)
@@ -41,11 +41,9 @@ int silc_socket_write(SilcSocketConnection sock)
       err = WSAGetLastError();
       if (err == WSAEWOULDBLOCK) {
        SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
-       silc_net_set_socket_nonblock(fd);
        return -2;
       }
       SILC_LOG_ERROR(("Cannot write to socket: %d", (int)fd));
-      silc_net_set_socket_nonblock(fd);
       return -1;
     }
 
@@ -54,7 +52,6 @@ int silc_socket_write(SilcSocketConnection sock)
 
   SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
 
-  silc_net_set_socket_nonblock(fd);
   return ret;
 }
 
@@ -68,25 +65,32 @@ int silc_socket_read(SilcSocketConnection sock)
   int len = 0, err;
   unsigned char buf[SILC_SOCKET_READ_SIZE];
   SOCKET fd = sock->sock;
+  int argp;
 
   SILC_LOG_DEBUG(("Reading data from socket %d", fd));
 
+  /* Check whether there is data available, without calling recv(). */
+  ioctlsocket(fd, FIONREAD, (unsigned long *)&argp);
+  if (argp == 0) {
+    /* Is this kludge or what? Without this thing this contraption
+       does not work at all!?. */
+    SleepEx(1, TRUE);
+    SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
+    return -2;
+  }
+
   /* Read the data from the socket. */
   len = recv(fd, buf, sizeof(buf), 0);
   if (len == SOCKET_ERROR) {
     err = WSAGetLastError();
     if (err == WSAEWOULDBLOCK || err == WSAEINTR) {
       SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
-      silc_net_set_socket_nonblock(fd);
       return -2;
     }
     SILC_LOG_ERROR(("Cannot read from socket: %d", (int)fd));
-    silc_net_set_socket_nonblock(fd);
     return -1;
   }
 
-  silc_net_set_socket_nonblock(fd);
-
   if (!len)
     return 0;