k0001 / pipes-network

Use network sockets together with the Haskell pipes library.
http://hackage.haskell.org/package/pipes-network
Other
24 stars 8 forks source link

Exception-free timeout functions #31

Open nikita-volkov opened 10 years ago

nikita-volkov commented 10 years ago

I suggest to use MonadPlus instances instead of throwing IOError. E.g.:

fromSocketTimeout :: (MonadIO m, MonadPlus m) => Int -> Socket.Socket -> Int -> Producer' BS m ()
fromSocketTimeout wait sock nbytes = loop where
    loop = do
       mbs <- liftIO (timeout wait (Network.Socket.ByteString.recv sock nbytes))
       case mbs of
          Just bs
           | ByteString.null bs -> return ()
           | otherwise -> yield bs >> loop
          Nothing -> mzero