开发者

haskell network io hgetline

开发者 https://www.devze.com 2022-12-31 03:20 出处:网络
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 imitat

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号