/ stream.go
stream.go
 1  package libp2pwebrtcdirect
 2  
 3  import (
 4  	"io"
 5  	"time"
 6  
 7  	"github.com/pion/datachannel"
 8  )
 9  
10  // Stream is a bidirectional io pipe within a connection.
11  type Stream struct {
12  	channel datachannel.ReadWriteCloser
13  }
14  
15  func newStream(channel datachannel.ReadWriteCloser) *Stream {
16  	return &Stream{channel: channel}
17  }
18  
19  // Read implements the io.Reader.
20  func (s *Stream) Read(p []byte) (int, error) {
21  	i, err := s.channel.Read(p)
22  	if err != nil {
23  		// pions/datachannel retuns an error when the underlying transport
24  		// is closed. Here we turn this into EOF.
25  		return i, io.EOF
26  	}
27  	return i, nil
28  }
29  
30  // Write implements the io.Writer.
31  func (s *Stream) Write(p []byte) (int, error) {
32  	return s.channel.Write(p)
33  }
34  
35  // CloseRead closes the stream for writing. Reading will still work (that
36  // is, the remote side can still write).
37  func (s *Stream) CloseRead() error {
38  	// TODO: figure out close vs reset
39  	return nil
40  }
41  
42  // CloseWrite closes the stream for writing. Reading will still work (that
43  // is, the remote side can still write).
44  func (s *Stream) CloseWrite() error {
45  	// TODO: figure out close vs reset
46  	return nil
47  }
48  
49  // Close closes the stream for writing. Reading will still work (that
50  // is, the remote side can still write).
51  func (s *Stream) Close() error {
52  	// TODO: figure out close vs reset
53  	return nil
54  }
55  
56  // Reset closes both ends of the stream. Use this to tell the remote
57  // side to hang up and go away.
58  func (s *Stream) Reset() error {
59  	// TODO: figure out close vs reset
60  	return s.channel.Close()
61  }
62  
63  // SetDeadline is a stub
64  func (s *Stream) SetDeadline(t time.Time) error {
65  	return nil
66  }
67  
68  // SetReadDeadline is a stub
69  func (s *Stream) SetReadDeadline(t time.Time) error {
70  	return nil
71  }
72  
73  // SetWriteDeadline is a stub
74  func (s *Stream) SetWriteDeadline(t time.Time) error {
75  	return nil
76  }