我想读取句柄上的所有数据,然后阻塞等待更多数据。当流中有 '\n' 字符时,listen1 停止。listen2 有效,可以通过模仿 hGetNonBlocking 的代码使其完全通用。做这个的最好方式是什么?
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))
)