| // Copyright 2015 The Vanadium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| type receiver interface { |
| // Forward forwards data read from net.Conn to a TunnelForwardClientStream or a |
| // TunnelForwardServerStream. |
| func Forward(conn net.Conn, s sender, r receiver) 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, s, done) |
| go stream2conn(r, 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)) |