| type receiver interface { |
| // stream is the interface common to TunnelForwardStream and TunnelServiceForwardStream. |
| // Forward forwards data read from net.Conn to a TunnelForwardStream or a TunnelServiceForwardStream. |
| func Forward(conn net.Conn, stream stream) error { |
| // Both conn2stream and stream2conn will write to the channel exactly |
| // Forward reads from the channel exactly once. |
| // A buffered channel is used to prevent the other write to the channel |
| done := make(chan error, 1) |
| go conn2stream(conn, stream, done) |
| go stream2conn(stream, conn, done) |
| func conn2stream(r io.Reader, s sender, done chan error) { |
| if err := s.Send(buf[:n]); err != nil { |
| func stream2conn(r receiver, w io.Writer, done chan error) { |
| if n, err := w.Write(buf); n != len(buf) || err != nil { |
| done <- fmt.Errorf("conn.Write returned (%d, %v) want (%d, nil)", n, err, len(buf)) |