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()
liste开发者_如何学Gon2 sock chan = do
loop ( do
s <- B.hGet sock 1
s1 <- B.hGetNonBlocking sock 65000
atomically (writeTChan chan (B.append s s1))
)
Perhaps you are unaware or perhaps I misunderstand your goal, but network-bytestring does what you want already.
精彩评论