[Haskell-cafe] `Expect'-like lazy reading/Parsec matching on TCP sockets

Bulat Ziganshin bulat.ziganshin at gmail.com
Tue Apr 3 18:34:22 EDT 2007

Hello Scott,

Wednesday, April 4, 2007, 1:54:27 AM, you wrote:

> Match the Parsec parser against the input as soon as a match
> is available, but fail if the match is unavailable after a timeout
> value if no further data is available on the socket.

one possible solution: use Streams library and establish a stream
transformer that adds an error call on timeout. something like this:

data StreamWithTimeout s = StreamWithTimeout s Timeout

instance Stream s => Stream (StreamWithTimeout s) where
  vGetChar (StreamWithTimeout s t) = do
     timeout t (vGetChar s)
               (error "Timed out!")

then you can use standard vGetContents lazy string reading in order to
get expected behaviour

or, even simple, you can make your own variant of hGetContents which
adds a timeout checks before each next call to hGetChar or hGetBuf

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list