summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorEric Doughty-Papassideris <eric.doughty@gmail.com>2012-01-21 17:51:58 +0100
committerEric Doughty-Papassideris <eric.doughty@gmail.com>2012-01-21 17:51:58 +0100
commit42e74aaf8ebb3801b009ce735950f7d4fc946fcf (patch)
treea54fb733def4d1265d86e1795bf0d38dd6970db9 /lib
parent374a4cdb8ae2c3d7d4789e079bbf1cb542acb4eb (diff)
downloadNim-42e74aaf8ebb3801b009ce735950f7d4fc946fcf.tar.gz
Less likely to overload struct timeval
Diffstat (limited to 'lib')
-rwxr-xr-xlib/pure/sockets.nim28
1 files changed, 12 insertions, 16 deletions
diff --git a/lib/pure/sockets.nim b/lib/pure/sockets.nim
index 54dc8ccd2..c21c0c941 100755
--- a/lib/pure/sockets.nim
+++ b/lib/pure/sockets.nim
@@ -428,6 +428,11 @@ proc connectAsync*(socket: TSocket, name: string, port = TPort(0),
   if not success: OSError()
 
 
+proc timeValFromMilliseconds(timeout = 500): TTimeVal =
+  if timeout != -1:
+    var seconds = timeout div 1000
+    result.tv_sec = seconds
+    result.tv_usec = (timeout - seconds * 1000) * 1000
 #proc recvfrom*(s: TWinSocket, buf: cstring, len, flags: cint, 
 #               fromm: ptr TSockAddr, fromlen: ptr cint): cint 
 
@@ -460,9 +465,7 @@ proc select*(readfds, writefds, exceptfds: var seq[TSocket],
   ## You can determine whether a socket is ready by checking if it's still
   ## in one of the TSocket sequences.
 
-  var tv: TTimeVal
-  tv.tv_sec = 0
-  tv.tv_usec = timeout * 1000
+  var tv {.noInit.}: TTimeVal = timeValFromMilliseconds(timeout)
   
   var rd, wr, ex: TFdSet
   var m = 0
@@ -480,10 +483,8 @@ proc select*(readfds, writefds, exceptfds: var seq[TSocket],
   pruneSocketSet(exceptfds, (ex))
 
 proc select*(readfds, writefds: var seq[TSocket], 
-             timeout = 500): int = 
-  var tv: TTimeVal
-  tv.tv_sec = 0
-  tv.tv_usec = timeout * 1000
+             timeout = 500): int =
+  var tv {.noInit.}: TTimeVal = timeValFromMilliseconds(timeout)
   
   var rd, wr: TFdSet
   var m = 0
@@ -499,10 +500,8 @@ proc select*(readfds, writefds: var seq[TSocket],
   pruneSocketSet(writefds, (wr))
 
 proc selectWrite*(writefds: var seq[TSocket], 
-                  timeout = 500): int = 
-  var tv: TTimeVal
-  tv.tv_sec = 0
-  tv.tv_usec = timeout * 1000
+                  timeout = 500): int =
+  var tv {.noInit.}: TTimeVal = timeValFromMilliseconds(timeout)
   
   var wr: TFdSet
   var m = 0
@@ -515,11 +514,8 @@ proc selectWrite*(writefds: var seq[TSocket],
   
   pruneSocketSet(writefds, (wr))
 
-
-proc select*(readfds: var seq[TSocket], timeout = 500): int = 
-  var tv: TTimeVal
-  tv.tv_sec = 0
-  tv.tv_usec = timeout * 1000
+proc select*(readfds: var seq[TSocket], timeout = 500): int =
+  var tv {.noInit.}: TTimeVal = timeValFromMilliseconds(timeout)
   
   var rd: TFdSet
   var m = 0