Tuesday, September 7, 2010

3 ways to use sb-bsd-sockets

Using SBCL, I needed to be able to shut down a server that was blocking on a sb-bsd-sockets:socket-accept call. The first thing that I did was make a special call on the freshly created socket:

(setf (sb-bsd-sockets:non-blocking-mode *server*) t)

After that, any call dealing with that socket will return immediately. I'm confused about how to put a socket into async mode in sbcl and how async is different than non-blocking.

You can read/write data using sb-bsd-sockets:inet-socket in 3 unique ways:

1. socket-send/socket-receive for raw octet writing and reading. Very painful to serialize anything.
2. socket-make-stream and all the ordinary stream handling routines in lisp. The calls seem to block even with non-blocking sockets.
3. socket-make-stream + fd-handlers. Allows you to specify callbacks for incoming input and to use the regular stream routines.

For my server, I need a single threaded event loop that can respond to events generated from the user thread and fire off events to the client(s). The client is a running swf or iPhone app that can respond to messages from the server.

Any SBCL socket pros, please respond with tips or hints!

Bonus note: For non-blocking client sockets on SBCL 1.0.39, I wrap the call to socket-connect in an ignore-errors block, because sockint::connect returns -1 with an errno of 36 (EAGAIN or equivalent). You can use the socket when socket-open-p returns T.

0 comments:

Post a Comment