daemons-0.1.2: Daemons in Haskell made fun and easy

Safe HaskellNone




Socket pipes

socketReader :: MonadIO m => Socket -> Producer ByteString m ()Source

Stream data from the socket.

socketWriter :: MonadIO m => Socket -> Consumer ByteString m ()Source

Stream data to the socket.

Socket server/client

type Handler r = Producer ByteString IO () -> Consumer ByteString IO () -> IO rSource

A simple handler: takes an incoming stream of ByteStrings, an stream of ByteStrings, and ties them together somehow. Conceptually, the simplest handler would be identity:

 import Control.Monad
 import Control.Pipe
 import Data.ByteString.Char8

 handler reader writer = do
     let identity = forever $ do
         x <- await
         yield x
     runPipe (writer <+< identity <+< reader)

See the pipes tutorial for more examples of writing pipes.

Since ByteStrings are fairly boring by themseleves, have a look at Control.Pipe.Serialize which lets you deserialize/serialize pipes of ByteStrings easily.

runSocketServer :: MonadIO m => Socket -> Handler () -> m ()Source

Listen for connections on the given socket, and run Handler on each received connection. The socket should previously have been bound to a port or to a file. Each handler is run in its own thread. Even in case of an error, the handlers' sockets are closed.

runSocketClient :: MonadIO m => Socket -> Handler r -> m rSource

Run Handler on the given socket.