我想读取句柄上的所有数据,然后阻止等待更多数据。 listen1在流中有'\ n'字符时停止。 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))
)