updates.
authorPekka Riikonen <priikone@silcnet.org>
Sun, 24 Jun 2001 13:31:26 +0000 (13:31 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 24 Jun 2001 13:31:26 +0000 (13:31 +0000)
configure.in.pre
lib/silcutil/win32/Makefile.am
lib/silcutil/win32/silcwin32schedule.c
lib/silcutil/win32/silcwin32sockconn.c [new file with mode: 0644]

index 293fa1e99a1b2ca27c5bdc092681f849730c4d49..dff0c03c78c92a724bb9d46c4131bfd20456f5a0 100644 (file)
@@ -563,10 +563,10 @@ esac ], CFLAGS="-O2 -g $CFLAGS"
 #
 # Other configure scripts
 #
-AC_CONFIG_SUBDIRS(irssi)
 AC_CONFIG_SUBDIRS(lib/dotconf)
 AC_CONFIG_SUBDIRS(lib/trq)
 AC_CONFIG_SUBDIRS(lib/silcmath/mpi)
+AC_CONFIG_SUBDIRS(irssi)
 #AC_CONFIG_SUBDIRS(lib/zlib)
 
 SILC_TOP_SRCDIR=`pwd`
index 9d95dc0f79edc578caa5c86b9211b9265088ab06..5e5d88979bfb9f244e17e7010e348c2c5d787c37 100644 (file)
@@ -22,7 +22,8 @@ noinst_LIBRARIES = libsilcwin32util.a
 
 libsilcwin32util_a_SOURCES =   \
        silcwin32net.c          \
-       silcwin32schedule.c
+       silcwin32schedule.c     \
+       silcwin32sockconn.c
 
 EXTRA_DIST = *.h
 
index df8ccf4d56b505c4a134e8d62f2ec988441e1c1d..6b38e5148d6c8407d75744fab0f20d1632259e24 100644 (file)
    o All Windows messages are dispatched from this function.
    o The Operating System has Winsock 2.
 
-   MSDN References:
+   References:
 
    o http://msdn.microsoft.com/library/default.asp?
      url=/library/en-us/winui/hh/winui/messques_77zk.asp 
    o http://msdn.microsoft.com/library/default.asp?
      url=/library/en-us/winsock/hh/winsock/apistart_9g1e.asp
+   o http://developer.novell.com/support/winsock/doc/toc.htm
 
 */
 
@@ -78,16 +79,24 @@ int silc_select(int n, fd_set *readfds, fd_set *writefds,
      and wait just for windows messages. */
   if (nhandles == 0 && timeout) {
     UINT timer = SetTimer(NULL, 0, timeo, NULL);
-    if (timer) {
+    curtime = GetTickCount();
+    while (timer)
       WaitMessage();
       KillTimer(NULL, timer);
 
       while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
+       if (msg.message == WM_TIMER)
+         return 0;
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
       }
 
-      return 0;
+      if (timeo != INFINITE) {
+       timeo -= GetTickCount() - curtime;
+       if (timeo < 0)
+         timeo = 0;
+       timer = SetTimer(NULL, 0, timeo, NULL);
+      }
     }
   }
 
diff --git a/lib/silcutil/win32/silcwin32sockconn.c b/lib/silcutil/win32/silcwin32sockconn.c
new file mode 100644 (file)
index 0000000..8ef70c8
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+
+  silcwin32sockconn.c
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 1997 - 2001 Pekka Riikonen
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+  
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+*/
+/* $Id$ */
+
+#include "silcincludes.h"
+
+/* Writes data from encrypted buffer to the socket connection. If the
+   data cannot be written at once, it will be written later with a timeout. 
+   The data is written from the data section of the buffer, not from head
+   or tail section. This automatically pulls the data section towards end
+   after writing the data. */
+
+int silc_socket_write(SilcSocketConnection sock)
+{
+  int ret = 0, err;
+  SOCKET fd = sock->sock;
+  SilcBuffer src = sock->outbuf;
+
+  SILC_LOG_DEBUG(("Writing data to socket %d", fd));
+
+  if (src->len > 0) {
+    ret = recv(fd, src->data, src->len,  0);
+    if (ret == SOCKET_ERROR) {
+      err = WSAGetLastError();
+      if (err == WSAEWOULDBLOCK) {
+       SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
+       return -2;
+      }
+      SILC_LOG_ERROR(("Cannot write to socket: %d", (int)fd));
+      return -1;
+    }
+
+    silc_buffer_pull(src, ret);
+  }
+
+  SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
+
+  return ret;
+}
+
+/* Reads data from the socket connection into the incoming data buffer.
+   It reads as much as possible from the socket connection. This returns
+   amount of bytes read or -1 on error or -2 on case where all of the
+   data could not be read at once. */
+
+int silc_socket_read(SilcSocketConnection sock)
+{
+  int len = 0, err;
+  unsigned char buf[SILC_SOCKET_READ_SIZE];
+  SOCKET fd = sock->sock;
+
+  SILC_LOG_DEBUG(("Reading data from socket %d", fd));
+
+  /* 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"));
+      return -2;
+    }
+    SILC_LOG_ERROR(("Cannot read from socket: %d", (int)fd));
+    return -1;
+  }
+
+  if (!len)
+    return 0;
+
+  /* Insert the data to the buffer. */
+
+  if (!sock->inbuf)
+    sock->inbuf = silc_buffer_alloc(SILC_SOCKET_BUF_SIZE);
+  
+  /* If the data does not fit to the buffer reallocate it */
+  if ((sock->inbuf->end - sock->inbuf->tail) < len)
+    sock->inbuf = silc_buffer_realloc(sock->inbuf, sock->inbuf->truelen + 
+                                     (len * 2));
+  silc_buffer_put_tail(sock->inbuf, buf, len);
+  silc_buffer_pull_tail(sock->inbuf, len);
+
+  SILC_LOG_DEBUG(("Read %d bytes", len));
+
+  return len;
+}