summary refs log tree commit diff stats
path: root/lib/pure/asyncdispatch.nim
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2014-07-12 22:51:06 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2014-07-12 22:51:06 +0100
commitcf5c8a204e76ff9ed5edb6ec0ebe3b97ee90f553 (patch)
tree90d9ce26be982b12b55e2dfaf021eda13fb8e995 /lib/pure/asyncdispatch.nim
parentc260b22fbca0e8a3be903c1c6db2027cfcf1c7f2 (diff)
downloadNim-cf5c8a204e76ff9ed5edb6ec0ebe3b97ee90f553.tar.gz
Many async optimisations.
* Selectors implementation will now attempt to immediately execute an IO
  operation instead of waiting for a ready notification.
* Removed recursion in asynchttpserver.
* Improved buffered implementation of recvLine in asyncnet.
* Optimised ``respond`` in asynchttpserver removing a possible "Delayed ACK"
  situation.
Diffstat (limited to 'lib/pure/asyncdispatch.nim')
-rw-r--r--lib/pure/asyncdispatch.nim30
1 files changed, 22 insertions, 8 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index 12329951c..14667a008 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -552,7 +552,18 @@ when defined(windows) or defined(nimdoc):
   initAll()
 else:
   import selectors
-  from posix import EINTR, EAGAIN, EINPROGRESS, EWOULDBLOCK, MSG_PEEK
+  when defined(windows):
+    import winlean
+    const
+      EINTR = WSAEINPROGRESS
+      EINPROGRESS = WSAEINPROGRESS
+      EWOULDBLOCK = WSAEWOULDBLOCK
+      EAGAIN = EINPROGRESS
+      MSG_NOSIGNAL = 0
+  else:
+    from posix import EINTR, EAGAIN, EINPROGRESS, EWOULDBLOCK, MSG_PEEK,
+                      MSG_NOSIGNAL
+  
   type
     TAsyncFD* = distinct cint
     TCallback = proc (sock: TAsyncFD): bool {.closure,gcsafe.}
@@ -693,12 +704,12 @@ else:
 
     proc cb(sock: TAsyncFD): bool =
       result = true
-      let res = recv(sock.TSocketHandle, addr readBuffer[0], size,
+      let res = recv(sock.TSocketHandle, addr readBuffer[0], size.cint,
                      flags.cint)
       #echo("recv cb res: ", res)
       if res < 0:
         let lastError = osLastError()
-        if lastError.int32 notin {EINTR, EWOULDBLOCK, EAGAIN}: 
+        if lastError.int32 notin {EINTR, EWOULDBLOCK, EAGAIN}:
           retFuture.fail(newException(EOS, osErrorMsg(lastError)))
         else:
           result = false # We still want this callback to be called.
@@ -708,8 +719,8 @@ else:
       else:
         readBuffer.setLen(res)
         retFuture.complete(readBuffer)
-  
-    addRead(socket, cb)
+    if not cb(socket):
+      addRead(socket, cb)
     return retFuture
 
   proc send*(socket: TAsyncFD, data: string): PFuture[void] =
@@ -721,7 +732,8 @@ else:
       result = true
       let netSize = data.len-written
       var d = data.cstring
-      let res = send(sock.TSocketHandle, addr d[written], netSize, 0.cint)
+      let res = send(sock.TSocketHandle, addr d[written], netSize.cint,
+                     MSG_NOSIGNAL)
       if res < 0:
         let lastError = osLastError()
         if lastError.int32 notin {EINTR, EWOULDBLOCK, EAGAIN}:
@@ -734,7 +746,8 @@ else:
           result = false # We still have data to send.
         else:
           retFuture.complete()
-    addWrite(socket, cb)
+    if not cb(socket):
+      addWrite(socket, cb)
     return retFuture
 
   proc acceptAddr*(socket: TAsyncFD): 
@@ -756,7 +769,8 @@ else:
       else:
         register(client.TAsyncFD)
         retFuture.complete(($inet_ntoa(sockAddress.sin_addr), client.TAsyncFD))
-    addRead(socket, cb)
+    if not cb(socket):
+      addRead(socket, cb)
     return retFuture
 
 proc accept*(socket: TAsyncFD): PFuture[TAsyncFD] =