swarm_stream.go 1.63 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package swarm

import (
Jeromy's avatar
Jeromy committed
4 5
	"time"

Jeromy's avatar
Jeromy committed
6 7 8
	inet "github.com/libp2p/go-libp2p-net"
	protocol "github.com/libp2p/go-libp2p-protocol"
	ps "github.com/libp2p/go-peerstream"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9 10
)

11
// Stream is a wrapper around a ps.Stream that exposes a way to get
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
// our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
13
type Stream ps.Stream
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15 16

// Stream returns the underlying peerstream.Stream
func (s *Stream) Stream() *ps.Stream {
17
	return (*ps.Stream)(s)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19 20 21 22 23 24 25 26
}

// Conn returns the Conn associated with this Stream, as an inet.Conn
func (s *Stream) Conn() inet.Conn {
	return s.SwarmConn()
}

// SwarmConn returns the Conn associated with this Stream, as a *Conn
func (s *Stream) SwarmConn() *Conn {
27
	return (*Conn)(s.Stream().Conn())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28 29 30 31
}

// Read reads bytes from a stream.
func (s *Stream) Read(p []byte) (n int, err error) {
32
	return s.Stream().Read(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34 35 36
}

// Write writes bytes to a stream, flushing for each call.
func (s *Stream) Write(p []byte) (n int, err error) {
37
	return s.Stream().Write(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40 41 42
}

// Close closes the stream, indicating this side is finished
// with the stream.
func (s *Stream) Close() error {
43
	return s.Stream().Close()
44 45
}

Steven Allen's avatar
Steven Allen committed
46 47 48 49 50
// Reset resets the stream, closing both ends.
func (s *Stream) Reset() error {
	return s.Stream().Reset()
}

51
func (s *Stream) Protocol() protocol.ID {
52
	return (*ps.Stream)(s).Protocol()
53 54
}

55
func (s *Stream) SetProtocol(p protocol.ID) {
56
	(*ps.Stream)(s).SetProtocol(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
}
Jeromy's avatar
Jeromy committed
58 59 60 61 62 63 64 65 66 67 68 69

func (s *Stream) SetDeadline(t time.Time) error {
	return s.Stream().SetDeadline(t)
}

func (s *Stream) SetReadDeadline(t time.Time) error {
	return s.Stream().SetReadDeadline(t)
}

func (s *Stream) SetWriteDeadline(t time.Time) error {
	return s.Stream().SetWriteDeadline(t)
}