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

import (
Jeromy's avatar
Jeromy committed
4
	inet "github.com/libp2p/go-libp2p-net"
Jeromy's avatar
Jeromy committed
5
	protocol "github.com/libp2p/go-libp2p-protocol"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6

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

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

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

// 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 {
26
	return (*Conn)(s.Stream().Conn())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29 30
}

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

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

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

45
func (s *Stream) Protocol() protocol.ID {
46
	return (*ps.Stream)(s).Protocol()
47 48
}

49
func (s *Stream) SetProtocol(p protocol.ID) {
50
	(*ps.Stream)(s).SetProtocol(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
}