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

import (
Steven Allen's avatar
Steven Allen committed
4 5 6
	"fmt"
	"sync"
	"sync/atomic"
Jeromy's avatar
Jeromy committed
7 8
	"time"

tavit ohanian's avatar
tavit ohanian committed
9 10 11
	"gitlab.dms3.io/p2p/go-p2p-core/mux"
	"gitlab.dms3.io/p2p/go-p2p-core/network"
	"gitlab.dms3.io/p2p/go-p2p-core/protocol"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
)

tavit ohanian's avatar
tavit ohanian committed
14
// Validate Stream conforms to the go-p2p-net Stream interface
15
var _ network.Stream = &Stream{}
16

Steven Allen's avatar
Steven Allen committed
17 18 19
// Stream is the stream type used by swarm. In general, you won't use this type
// directly.
type Stream struct {
Steven Allen's avatar
Steven Allen committed
20
	id uint64
21

22
	stream mux.MuxedStream
Steven Allen's avatar
Steven Allen committed
23 24
	conn   *Conn

25 26
	closeOnce sync.Once

Steven Allen's avatar
Steven Allen committed
27 28 29
	notifyLk sync.Mutex

	protocol atomic.Value
30

31
	stat network.Stat
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33
}

34 35 36 37 38
func (s *Stream) ID() string {
	// format: <first 10 chars of peer id>-<global conn ordinal>-<global stream ordinal>
	return fmt.Sprintf("%s-%d", s.conn.ID(), s.id)
}

Steven Allen's avatar
Steven Allen committed
39 40 41 42 43 44 45 46 47
func (s *Stream) String() string {
	return fmt.Sprintf(
		"<swarm.Stream[%s] %s (%s) <-> %s (%s)>",
		s.conn.conn.Transport(),
		s.conn.LocalMultiaddr(),
		s.conn.LocalPeer(),
		s.conn.RemoteMultiaddr(),
		s.conn.RemotePeer(),
	)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48 49
}

50 51
// Conn returns the Conn associated with this stream, as an network.Conn
func (s *Stream) Conn() network.Conn {
Steven Allen's avatar
Steven Allen committed
52
	return s.conn
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53 54 55
}

// Read reads bytes from a stream.
Steven Allen's avatar
Steven Allen committed
56 57 58 59 60 61 62 63
func (s *Stream) Read(p []byte) (int, error) {
	n, err := s.stream.Read(p)
	// TODO: push this down to a lower level for better accuracy.
	if s.conn.swarm.bwc != nil {
		s.conn.swarm.bwc.LogRecvMessage(int64(n))
		s.conn.swarm.bwc.LogRecvMessageStream(int64(n), s.Protocol(), s.Conn().RemotePeer())
	}
	return n, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65 66
}

// Write writes bytes to a stream, flushing for each call.
Steven Allen's avatar
Steven Allen committed
67 68 69 70 71 72 73 74
func (s *Stream) Write(p []byte) (int, error) {
	n, err := s.stream.Write(p)
	// TODO: push this down to a lower level for better accuracy.
	if s.conn.swarm.bwc != nil {
		s.conn.swarm.bwc.LogSentMessage(int64(n))
		s.conn.swarm.bwc.LogSentMessageStream(int64(n), s.Protocol(), s.Conn().RemotePeer())
	}
	return n, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75 76
}

77 78
// Close closes the stream, closing both ends and freeing all associated
// resources.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
func (s *Stream) Close() error {
Steven Allen's avatar
Steven Allen committed
80
	err := s.stream.Close()
81
	s.closeOnce.Do(s.remove)
Steven Allen's avatar
Steven Allen committed
82
	return err
83 84
}

85 86
// Reset resets the stream, signaling an error on both ends and freeing all
// associated resources.
Steven Allen's avatar
Steven Allen committed
87
func (s *Stream) Reset() error {
Steven Allen's avatar
Steven Allen committed
88
	err := s.stream.Reset()
89
	s.closeOnce.Do(s.remove)
Steven Allen's avatar
Steven Allen committed
90 91 92
	return err
}

93 94 95 96 97 98 99 100 101 102 103 104 105
// Close closes the stream for writing, flushing all data and sending an EOF.
// This function does not free resources, call Close or Reset when done with the
// stream.
func (s *Stream) CloseWrite() error {
	return s.stream.CloseWrite()
}

// Close closes the stream for reading. This function does not free resources,
// call Close or Reset when done with the stream.
func (s *Stream) CloseRead() error {
	return s.stream.CloseRead()
}

Steven Allen's avatar
Steven Allen committed
106
func (s *Stream) remove() {
107
	s.conn.removeStream(s)
Steven Allen's avatar
Steven Allen committed
108 109 110 111 112 113 114

	// We *must* do this in a goroutine. This can be called during a
	// an open notification and will block until that notification is done.
	go func() {
		s.notifyLk.Lock()
		defer s.notifyLk.Unlock()

115
		s.conn.swarm.notifyAll(func(f network.Notifiee) {
Steven Allen's avatar
Steven Allen committed
116 117 118 119
			f.ClosedStream(s.conn.swarm, s)
		})
		s.conn.swarm.refs.Done()
	}()
Steven Allen's avatar
Steven Allen committed
120 121
}

Steven Allen's avatar
Steven Allen committed
122
// Protocol returns the protocol negotiated on this stream (if set).
123
func (s *Stream) Protocol() protocol.ID {
Steven Allen's avatar
Steven Allen committed
124 125 126
	// Ignore type error. It means that the protocol is unset.
	p, _ := s.protocol.Load().(protocol.ID)
	return p
127 128
}

Steven Allen's avatar
Steven Allen committed
129 130 131 132 133
// SetProtocol sets the protocol for this stream.
//
// This doesn't actually *do* anything other than record the fact that we're
// speaking the given protocol over this stream. It's still up to the user to
// negotiate the protocol. This is usually done by the Host.
134
func (s *Stream) SetProtocol(p protocol.ID) {
Steven Allen's avatar
Steven Allen committed
135
	s.protocol.Store(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
136
}
Jeromy's avatar
Jeromy committed
137

Steven Allen's avatar
Steven Allen committed
138
// SetDeadline sets the read and write deadlines for this stream.
Jeromy's avatar
Jeromy committed
139
func (s *Stream) SetDeadline(t time.Time) error {
Steven Allen's avatar
Steven Allen committed
140
	return s.stream.SetDeadline(t)
Jeromy's avatar
Jeromy committed
141 142
}

Steven Allen's avatar
Steven Allen committed
143
// SetReadDeadline sets the read deadline for this stream.
Jeromy's avatar
Jeromy committed
144
func (s *Stream) SetReadDeadline(t time.Time) error {
Steven Allen's avatar
Steven Allen committed
145
	return s.stream.SetReadDeadline(t)
Jeromy's avatar
Jeromy committed
146 147
}

Steven Allen's avatar
Steven Allen committed
148
// SetWriteDeadline sets the write deadline for this stream.
Jeromy's avatar
Jeromy committed
149
func (s *Stream) SetWriteDeadline(t time.Time) error {
Steven Allen's avatar
Steven Allen committed
150
	return s.stream.SetWriteDeadline(t)
Jeromy's avatar
Jeromy committed
151
}
152 153

// Stat returns metadata information for this stream.
154
func (s *Stream) Stat() network.Stat {
155 156
	return s.stat
}