tags:

views:

56

answers:

1

I want to read all the data on a handle, and then block waiting for more data. listen1 stops when there is a '\n' character in the stream. listen2 works and could be made completely general by imitating the code for hGetNonBlocking. What is the best way to do this?

import qualified Data.ByteString as B

loop = sequence_ . repeat

listen1 :: Handle -> TChan B.ByteString -> IO()
listen1 sock chan = do
  loop ( do
         s <- B.hGetLine sock
         atomically (writeTChan chan s)
       )

listen2 :: Handle -> TChan B.ByteString -> IO()
listen2 sock chan = do
  loop ( do
         s <- B.hGet sock 1
         s1 <- B.hGetNonBlocking sock 65000
         atomically (writeTChan chan (B.append s s1))
       )
A: 

Perhaps you are unaware or perhaps I misunderstand your goal, but network-bytestring does what you want already.

TomMD