Subject: Re: read-sequence from socket
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 04 Nov 2008 20:45:04 -0600
Newsgroups: comp.lang.lisp
Message-ID: <x46dnSe5f8AtlYzUnZ2dnUVZ_q7inZ2d@speakeasy.net>
David Reitter  <david.reitter@gmail.com> wrote:
+---------------
| I'm trying to read into a buffer from a socket using `read-sequence'.
| 
| I'd like `read-sequence' to stop when there is no more data, and
| the :input-timeout argument to `make-socket' in the current CCL allows
| me to specify a timeout, upon which an error is signalled.  However,
| I'm unclear how I would then find out how many bytes were read -
| that's the normal return value of `read-sequence'.
+---------------

You're going to find that doing this with READ-SEQUENCE is likely
to interact badly with the underlying operating system, since if
the timeout exception occurs in the middle of a read the "amount
already read" is not well-defined for many [most?] OSs.

What I would do instead in this case is write your own version
of READ-SEQUENCE which uses whatever underlying "select()/poll()"
calls [or equiv.] intermingled with native "read()"s (which will
be non-blocking if you only issue one when the "select()/poll()"
says there's something there [though if you're really paranoid
you might want to do an FIONREAD "ioctl()" or equiv. to be sure]),
so that the timeouts *only* ever occur during the "select()/poll()"
calls. In that case, you'll have a deterministic count of "amount
already read".


-Rob

p.s. Even with the above, setting a static timeout value which
can properly handle unexpected delays [e.g. temporary routing
loops or route flapping] which can persist for several minutes
is considered by networking experts to be... "interesting", at
the very least. You have been warned.  ;-}

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607