Dear sbcl-help,
Sorry to keep flogging this horse, but I'm still a bit confused about
mixing byte and character data on streams. I understand that there are
a number of different streams implementations running around, including
the so-called simple and gray streams, but I'm not quite sure when and
how these are invoked.
My particular problem is that I would like to write binary data to an
araneida stream. The stream gets created in
araneida:listener-accept-stream which calls
sb-bsd-sockets:socket-make-stream with element-type :base-char. This is
fine, except that at some point I would like to write binary data to
this stream. My understanding is that I can't do a write-byte to a
character stream, as this is not allowed by CLHS. I can live with that.
So, after looking at this for a while and some input from #lisp, I
tried making this a :element-type 'unsigned-byte stream. The problem is
that araneida reads/writes things like HTTP headers to this stream
using read/write-char. My understanding is that the spec does not
prohibit read/write-char from working on binary streams. But that the
implementation of fd-stream does prohibit this.
I patched fd-stream to use the 1-byte character read/write routines for
binary streams and this seems to work, and throws errors if one tries
something like (write-char (code-char 257))
This allowed araneida to do the following:
1. Text HTTP streams work as before, but over a (nominally) binary
stream.
2. araneida's static file handler can now serve up binary data, if one
comments out the check for steram compatibility
3. write-byte to the araneida stream allows for binary data to be sent
over an http stream, which meant I could dynamically generate jpegs and
serve them out over a stream, without hitting the file system, which is
how I get started in this whole mess.
If this is the best way forward, I'm happy to keep going down this path
and would love to see my patch incorporated into the sbcl tree. If I'm
missing something, and this patch is superfluous, I'd appreciate
someone pointing out the error of my eyes. If I'm on the right path,
but need to keep digging, I'm happy to do that too.
So, in summary, I've got the following three questions:
1. What does the LANG=C or whatever it is actually let you do? Does it
let you write-byte to a character stream?
2. Does the workaround allow you to read/write-character to a binary
stream?
3. Does the patch to allow fd-stream to read and write chars to binary
streams make sense?
I hate to keep bringing this up and if I'm just fundamentally missing
something, please let me know, but otherwise I'd like to see a
resolution to this that doesn't involve me having patched up copies of
sbcl and araneida all over the place...
Thanks again,
Cyrus
|