Alex
Alex

Reputation: 1631

haskell network io hgetline

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))
       )

Upvotes: 2

Views: 410

Answers (1)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

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

Upvotes: 1

Related Questions