From 783e63781f1ebdd7c3b3ebc592606c1049d00d78 Mon Sep 17 00:00:00 2001
From: Slava Pestov <slava@factorcode.org>
Date: Thu, 24 Jan 2008 19:21:10 -0400
Subject: [PATCH] I/O fixes

---
 extra/io/sockets/impl/impl.factor          |  9 +++++++--
 extra/io/sockets/sockets.factor            |  0
 extra/io/windows/nt/backend/backend.factor | 18 ++++++++++--------
 3 files changed, 17 insertions(+), 10 deletions(-)
 mode change 100644 => 100755 extra/io/sockets/sockets.factor

diff --git a/extra/io/sockets/impl/impl.factor b/extra/io/sockets/impl/impl.factor
index e8ab957482..ce4d5ad566 100755
--- a/extra/io/sockets/impl/impl.factor
+++ b/extra/io/sockets/impl/impl.factor
@@ -106,9 +106,14 @@ M: f parse-sockaddr nip ;
     [ ] unfold nip [ ] subset ;
 
 : prepare-resolve-host ( host serv passive? -- host' serv' flags )
+    #! If the port is a number, we resolve for 'http' then
+    #! change it later. This is a workaround for a FreeBSD
+    #! getaddrinfo() limitation -- on Windows, Linux and Mac,
+    #! we can convert a number to a string and pass that as the
+    #! service name, but on FreeBSD this gives us an unknown
+    #! service error.
     >r
-    >r string>char-alien r>
-    dup integer? [ port-override set f ] [ string>char-alien ] if
+    dup integer? [ port-override set "http" ] when
     r> AI_PASSIVE 0 ? ;
 
 M: object resolve-host ( host serv passive? -- seq )
diff --git a/extra/io/sockets/sockets.factor b/extra/io/sockets/sockets.factor
old mode 100644
new mode 100755
diff --git a/extra/io/windows/nt/backend/backend.factor b/extra/io/windows/nt/backend/backend.factor
index 0d1f2cec0b..82d609c371 100755
--- a/extra/io/windows/nt/backend/backend.factor
+++ b/extra/io/windows/nt/backend/backend.factor
@@ -116,25 +116,27 @@ M: windows-nt-io add-completion ( handle -- )
 : lookup-callback ( GetQueuedCompletion-args -- callback )
     io-hash get-global delete-at* drop ;
 
-: wait-for-io ( timeout -- continuation/f )
+: handle-overlapped ( timeout -- ? )
     wait-for-overlapped [
         GetLastError dup expected-io-error? [
-            2drop f
+            2drop t
         ] [
             dup eof? [
                 drop lookup-callback
                 dup io-callback-port t swap set-port-eof?
-                io-callback-continuation
             ] [
                 (win32-error-string) swap lookup-callback
                 [ io-callback-port set-port-error ] keep
-                io-callback-continuation
-            ] if
+            ] if io-callback-continuation schedule-thread f
         ] if
     ] [
-        lookup-callback io-callback-continuation
+        lookup-callback
+        io-callback-continuation schedule-thread f
     ] if ;
 
+: drain-overlapped ( timeout -- )
+    handle-overlapped [ 0 drain-overlapped ] unless ;
+
 : maybe-expire ( io-callbck -- )
     io-callback-port
     dup timeout? [
@@ -144,10 +146,10 @@ M: windows-nt-io add-completion ( handle -- )
     ] if ;
 
 : cancel-timeout ( -- )
-    io-hash get-global values [ maybe-expire ] each ;
+    io-hash get-global [ nip maybe-expire ] assoc-each ;
 
 M: windows-nt-io io-multiplex ( ms -- )
-    cancel-timeout wait-for-io [ schedule-thread ] when* ;
+    cancel-timeout drain-overlapped ;
 
 M: windows-nt-io init-io ( -- )
     <master-completion-port> master-completion-port set-global