[Haskell-cafe] How to make asynchronous I/O composable and safe?
joeyadams3.14159 at gmail.com
Sat Jan 14 11:42:36 CET 2012
On Sat, Jan 14, 2012 at 1:29 AM, Bardur Arantsson <spam at scientician.net> wrote:
> So, the API becomes something like:
> runSocketServer :: ((Chan a, Chan b) -> IO ()) -> ... -> IO ()
> where the first parameter contains the "client logic" and "A" is the type of
> the messages from the client and "B" is the type of the messages which are
> sent back to the client.
Thanks, that's a good idea. Even if I only plan to receive in one
thread, placing the messages in a Chan or TChan helps separate my
application thread from the complexities of connection management.
Is there something on Hackage that will do this for me? Or will I
need to roll my own? Namely, convert a network connection to a pair
of channels, and close the connection automatically. Something like
-- | Spawn two threads, one which populates the first channel with messages
-- from the other host, and another which reads the second channel and sends
-- its messages to the other host.
-- Run the given computation, passing it these channels. When the
-- completes (or throws an exception), sending and receiving will
stop, and the
-- connection will be closed.
-- If either the receiving thread or sending thread encounter an exception,
-- sending and receiving will stop, and an asynchronous exception will be
-- thrown to your thread.
channelize :: IO msg_in -- ^ Receive callback
-> (msg_out -> IO () -- ^ Send callback
-> IO () -- ^ Close callback
-> (TChan msg_in -> TChan msg_out -> IO a)
-- ^ Inner computation
-> IO a
More information about the Haskell-Cafe