swarm_stream.go 4.33 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 7
	"fmt"
	"io"
	"sync"
	"sync/atomic"
Jeromy's avatar
Jeromy committed
8 9
	"time"

10 11 12
	"github.com/libp2p/go-libp2p-core/mux"
	"github.com/libp2p/go-libp2p-core/network"
	"github.com/libp2p/go-libp2p-core/protocol"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
)

Steven Allen's avatar
Steven Allen committed
15
type streamState int
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16

Steven Allen's avatar
Steven Allen committed
17 18 19 20 21 22 23 24
const (
	streamOpen streamState = iota
	streamCloseRead
	streamCloseWrite
	streamCloseBoth
	streamReset
)

25
// Validate Stream conforms to the go-libp2p-net Stream interface
26
var _ network.Stream = &Stream{}
27

Steven Allen's avatar
Steven Allen committed
28 29 30
// Stream is the stream type used by swarm. In general, you won't use this type
// directly.
type Stream struct {
31 32
	id uint32

33
	stream mux.MuxedStream
Steven Allen's avatar
Steven Allen committed
34 35 36 37 38 39 40 41 42 43
	conn   *Conn

	state struct {
		sync.Mutex
		v streamState
	}

	notifyLk sync.Mutex

	protocol atomic.Value
44

45
	stat network.Stat
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47
}

48 49 50 51 52
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
53 54 55 56 57 58 59 60 61
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
62 63
}

64 65
// 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
66
	return s.conn
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67 68 69
}

// Read reads bytes from a stream.
Steven Allen's avatar
Steven Allen committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
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())
	}
	// If we observe an EOF, this stream is now closed for reading.
	// If we're already closed for writing, this stream is now fully closed.
	if err == io.EOF {
		s.state.Lock()
		switch s.state.v {
		case streamCloseWrite:
			s.state.v = streamCloseBoth
			s.remove()
		case streamOpen:
			s.state.v = streamCloseRead
		}
		s.state.Unlock()
	}
	return n, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91 92 93
}

// Write writes bytes to a stream, flushing for each call.
Steven Allen's avatar
Steven Allen committed
94 95 96 97 98 99 100 101
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
102 103 104 105 106
}

// Close closes the stream, indicating this side is finished
// with the stream.
func (s *Stream) Close() error {
Steven Allen's avatar
Steven Allen committed
107 108 109 110 111 112 113 114 115 116 117 118
	err := s.stream.Close()

	s.state.Lock()
	switch s.state.v {
	case streamCloseRead:
		s.state.v = streamCloseBoth
		s.remove()
	case streamOpen:
		s.state.v = streamCloseWrite
	}
	s.state.Unlock()
	return err
119 120
}

Steven Allen's avatar
Steven Allen committed
121 122
// Reset resets the stream, closing both ends.
func (s *Stream) Reset() error {
Steven Allen's avatar
Steven Allen committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	err := s.stream.Reset()
	s.state.Lock()
	switch s.state.v {
	case streamOpen, streamCloseRead, streamCloseWrite:
		s.state.v = streamReset
		s.remove()
	}
	s.state.Unlock()
	return err
}

func (s *Stream) remove() {
	s.conn.removeStream(s)

	// 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()

143
		s.conn.swarm.notifyAll(func(f network.Notifiee) {
Steven Allen's avatar
Steven Allen committed
144 145 146 147
			f.ClosedStream(s.conn.swarm, s)
		})
		s.conn.swarm.refs.Done()
	}()
Steven Allen's avatar
Steven Allen committed
148 149
}

Steven Allen's avatar
Steven Allen committed
150
// Protocol returns the protocol negotiated on this stream (if set).
151
func (s *Stream) Protocol() protocol.ID {
Steven Allen's avatar
Steven Allen committed
152 153 154
	// Ignore type error. It means that the protocol is unset.
	p, _ := s.protocol.Load().(protocol.ID)
	return p
155 156
}

Steven Allen's avatar
Steven Allen committed
157 158 159 160 161
// 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.
162
func (s *Stream) SetProtocol(p protocol.ID) {
Steven Allen's avatar
Steven Allen committed
163
	s.protocol.Store(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164
}
Jeromy's avatar
Jeromy committed
165

Steven Allen's avatar
Steven Allen committed
166
// SetDeadline sets the read and write deadlines for this stream.
Jeromy's avatar
Jeromy committed
167
func (s *Stream) SetDeadline(t time.Time) error {
Steven Allen's avatar
Steven Allen committed
168
	return s.stream.SetDeadline(t)
Jeromy's avatar
Jeromy committed
169 170
}

Steven Allen's avatar
Steven Allen committed
171
// SetReadDeadline sets the read deadline for this stream.
Jeromy's avatar
Jeromy committed
172
func (s *Stream) SetReadDeadline(t time.Time) error {
Steven Allen's avatar
Steven Allen committed
173
	return s.stream.SetReadDeadline(t)
Jeromy's avatar
Jeromy committed
174 175
}

Steven Allen's avatar
Steven Allen committed
176
// SetWriteDeadline sets the write deadline for this stream.
Jeromy's avatar
Jeromy committed
177
func (s *Stream) SetWriteDeadline(t time.Time) error {
Steven Allen's avatar
Steven Allen committed
178
	return s.stream.SetWriteDeadline(t)
Jeromy's avatar
Jeromy committed
179
}
180 181

// Stat returns metadata information for this stream.
182
func (s *Stream) Stat() network.Stat {
183 184
	return s.stat
}