def bounce(in1, out1, in2, out2, timeout=nil) buf1 = '' buf2 = '' loop do rd = [] wr = [] rd << in1 if buf1.length < 1024*16 rd << in2 if buf2.length < 1024*16 wr << out1 if buf1.length > 0 wr << out2 if buf2.length > 0 rd, wr = IO.select(rd, wr, nil, (Integer(timeout) rescue nil)) break unless rd case rd[0] when in1 # str = in1.readpartial 1 rescue nil str = in1.read 1 rescue nil break unless str buf1 << str when in2 # str = in2.readpartial 1 rescue nil str = in2.read 1 rescue nil break unless str buf2 << str end case wr[0] when out1 len = out1.write buf1 buf1[0...len] = '' when out2 len = out2.write buf2 buf2[0...len] = '' end end end