accept handles non-blocking correctly

This commit is contained in:
Alex Shinn 2011-11-21 20:42:49 +09:00
parent 71922c284a
commit bb53a03fb8
3 changed files with 53 additions and 3 deletions

47
lib/chibi/accept.c Normal file
View file

@ -0,0 +1,47 @@
/* chibi-ffi should probably be able to detect these patterns automatically, */
/* but for now we manually check two special cases - accept should check for */
/* EWOULDBLOCK and block on the socket, and listen should automatically make */
/* sockets non-blocking. */
sexp sexp_accept (sexp ctx, sexp self, sexp arg0, sexp arg1, sexp arg2) {
sexp f;
socklen_t tmp;
int res;
if (! sexp_exact_integerp(arg0))
return sexp_type_exception(ctx, self, SEXP_FIXNUM, arg0);
if (! (sexp_pointerp(arg1) && (sexp_pointer_tag(arg1) == sexp_type_tag(sexp_sockaddr_type_t))))
return sexp_type_exception(ctx, self, sexp_type_tag(sexp_sockaddr_type_t), arg1);
if (! sexp_exact_integerp(arg2))
return sexp_type_exception(ctx, self, SEXP_FIXNUM, arg2);
tmp = sexp_sint_value(arg2);
res = accept(sexp_sint_value(arg0), (struct sockaddr*)sexp_cpointer_value(arg1), &tmp);
#if SEXP_USE_GREEN_THREADS
if (res < 0 && errno == EWOULDBLOCK) {
f = sexp_global(ctx, SEXP_G_THREADS_BLOCKER);
if (sexp_opcodep(f)) {
((sexp_proc2)sexp_opcode_func(f))(ctx, f, 1, arg0);
return sexp_global(ctx, SEXP_G_IO_BLOCK_ERROR);
}
}
#endif
return sexp_make_integer(ctx, res);
}
/* If we're listening on a socket from Scheme, we most likely want it */
/* to be non-blocking. */
sexp sexp_listen (sexp ctx, sexp self, sexp arg0, sexp arg1) {
int fd, res;
if (! sexp_exact_integerp(arg0))
return sexp_type_exception(ctx, self, SEXP_FIXNUM, arg0);
if (! sexp_exact_integerp(arg1))
return sexp_type_exception(ctx, self, SEXP_FIXNUM, arg1);
fd = sexp_sint_value(arg0);
res = listen(fd, sexp_sint_value(arg1));
#if SEXP_USE_GREEN_THREADS
if (res >= 0)
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
#endif
return sexp_make_integer(ctx, res);
}

View file

@ -84,5 +84,4 @@
(close-file-descriptor sock)
(error "couldn't listen on socket for: " addrinfo))
(else
(set-file-descriptor-status! sock open/non-block)
sock))))

View file

@ -32,11 +32,13 @@
;;> Listen on a socket.
(define-c int listen (int int))
(define-c sexp (listen "sexp_listen")
((value ctx sexp) (value self sexp) sexp sexp))
;;> Accept a connection on a socket.
(define-c int accept (int sockaddr (pointer socklen_t)))
(define-c sexp (accept "sexp_accept")
((value ctx sexp) (value self sexp) sexp sexp sexp))
;;> Create an endpoint for communication.
@ -56,3 +58,5 @@
;;> The constants for the addrinfo struct.
;;/
(c-include "accept.c")