[Haskell-cafe] Waiting on input with `hWaitForInput' or `threadWaitRead'

Jason Dusek jason.dusek at gmail.com
Tue Oct 18 19:21:20 CEST 2011

2011/10/18 Gregory Collins <greg at gregorycollins.net>:
> On Tue, Oct 18, 2011 at 3:18 AM, Jason Dusek <jason.dusek at gmail.com> wrote:
> > The lazy bridging code, `lazyBridge', blocks (unsurprisingly)
> > and does not allow packets to go back and forth. I think I need
> > explicit selects/waits here to get the back and forth traffic.
> > Maybe there is a some way to leverage GHC's internal async I/O
> > but I'm not sure how to do it.
> Maybe: forkIO two threads, one for the read end, one for the write
> end? I would use a loop over lazy I/O, also.

This does work, thanks; the new version of lazyBridge is:

  lazyBridge                  ::  Handle -> Handle -> IO ()
  lazyBridge a b               =  do forkIO (flush a b)
                                     forkIO (flush b a)
                                     return ()
    flush a b                  =  LazyB.hGetContents a >>= LazyB.hPut b

  -- http://hpaste.org/52814

I am kind of surprised that this works at all, actually.

The strict version has this problem where it lets each socket
takes turns sending and receiving, if you try to send and it's
not your turn, it waits for the other one to send before sending
your data. The lazy version just sends bytes as they become
available, the desired behaviour.

I guess if I wanted to instrument the proxying, to keep a tally
of how much traffic there was (to GC little used connections,
for example), I would need to move up to enumerators?

Jason Dusek
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

More information about the Haskell-Cafe mailing list