guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 19/23: nio: add non-blocking connect


From: Andy Wingo
Subject: [Guile-commits] 19/23: nio: add non-blocking connect
Date: Thu, 24 Mar 2016 14:26:04 +0000

wingo pushed a commit to branch wip-ethreads
in repository guile.

commit a487ade8c8e43147486a7dc45f93b615b984a8ad
Author: Andy Wingo <address@hidden>
Date:   Fri Mar 30 17:11:22 2012 +0200

    nio: add non-blocking connect
    
    * libguile/nio.c (scm_nio_connect):
    * module/ice-9/nio.scm (nio-connect): Add non-blocking connect
      primitive.
---
 libguile/nio.c       |   32 ++++++++++++++++++++++++++++++++
 module/ice-9/nio.scm |   13 ++++++++++++-
 2 files changed, 44 insertions(+), 1 deletions(-)

diff --git a/libguile/nio.c b/libguile/nio.c
index bebb059..b8541b7 100644
--- a/libguile/nio.c
+++ b/libguile/nio.c
@@ -169,6 +169,37 @@ scm_nio_accept (SCM fd)
 }
 #undef FUNC_NAME
 
+/* Initiate a connection from a socket.
+
+   The second argument should be a socket address object as returned by
+   @code{make-socket-address}.
+
+   Returns @code{#t} if the connection succeeded immediately, and
+   @code{#f} otherwise.  */
+static SCM
+scm_nio_connect (SCM fd, SCM sockaddr)
+#define FUNC_NAME "nio-connect"
+{
+  int c_fd, rv, save_errno;
+  struct sockaddr *soka;
+  size_t size;
+
+  c_fd = scm_to_int (fd);
+  soka = scm_to_sockaddr (sockaddr, &size);
+  rv = connect (c_fd, soka, size);
+  save_errno = errno;
+
+  if (rv == -1 && save_errno != EINPROGRESS)
+    {
+      free (soka);
+      errno = save_errno;
+      SCM_SYSERROR;
+    }
+  free (soka);
+  return scm_from_bool (rv == 0);
+}
+#undef FUNC_NAME
+
 
 
 
@@ -179,6 +210,7 @@ scm_init_nio (void)
   scm_c_define_gsubr ("%nio-read", 4, 0, 0, scm_nio_read);
   scm_c_define_gsubr ("%nio-write", 4, 0, 0, scm_nio_write);
   scm_c_define_gsubr ("%nio-accept", 1, 0, 0, scm_nio_accept);
+  scm_c_define_gsubr ("%nio-connect", 2, 0, 0, scm_nio_connect);
 }
 
 void
diff --git a/module/ice-9/nio.scm b/module/ice-9/nio.scm
index b2f0e71..d69630d 100644
--- a/module/ice-9/nio.scm
+++ b/module/ice-9/nio.scm
@@ -20,7 +20,8 @@
 (define-module (ice-9 nio)
   #:export (nio-read
             nio-write
-            nio-accept))
+            nio-accept
+            nio-connect))
 
 (eval-when (eval load compile)
   (load-extension (string-append "libguile-" (effective-version))
@@ -44,3 +45,13 @@ read without blocking.  A return value of 0 indicates EOF."
 new client socket, unless the @code{accept} call would have blocked, in
 which case return @code{#f}."
   (%nio-accept fd))
+
+(define (nio-connect fd sockaddr)
+  "Initiate a connection from a socket whose file descriptor is
address@hidden  The second argument should be a socket address object as
+returned by @code{make-socket-address}.  Returns @code{#t} if the
+connection succeeded immediately, and @code{#f} otherwise.
+
+Once this socket becomes writable, it is ready.  If there was a
+connection error, it can be retrieved with getsockopt of SO_ERROR."
+  (%nio-connect fd sockaddr))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]