/
stream.go
120 lines (96 loc) · 2.25 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package p2p
import (
"io"
"sync"
ifconnmgr "github.com/libp2p/go-libp2p-core/connmgr"
net "github.com/libp2p/go-libp2p-core/network"
peer "github.com/libp2p/go-libp2p-core/peer"
protocol "github.com/libp2p/go-libp2p-core/protocol"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)
const cmgrTag = "stream-fwd"
// Stream holds information on active incoming and outgoing p2p streams.
type Stream struct {
id uint64
Protocol protocol.ID
OriginAddr ma.Multiaddr
TargetAddr ma.Multiaddr
peer peer.ID
Local manet.Conn
Remote net.Stream
Registry *StreamRegistry
}
// close stream endpoints and deregister it
func (s *Stream) close() {
s.Registry.Close(s)
}
// reset closes stream endpoints and deregisters it
func (s *Stream) reset() {
s.Registry.Reset(s)
}
func (s *Stream) startStreaming() {
go func() {
_, err := io.Copy(s.Local, s.Remote)
if err != nil {
s.reset()
} else {
s.close()
}
}()
go func() {
_, err := io.Copy(s.Remote, s.Local)
if err != nil {
s.reset()
} else {
s.close()
}
}()
}
// StreamRegistry is a collection of active incoming and outgoing proto app streams.
type StreamRegistry struct {
sync.Mutex
Streams map[uint64]*Stream
conns map[peer.ID]int
nextID uint64
ifconnmgr.ConnManager
}
// Register registers a stream to the registry
func (r *StreamRegistry) Register(streamInfo *Stream) {
r.Lock()
defer r.Unlock()
r.ConnManager.TagPeer(streamInfo.peer, cmgrTag, 20)
r.conns[streamInfo.peer]++
streamInfo.id = r.nextID
r.Streams[r.nextID] = streamInfo
r.nextID++
streamInfo.startStreaming()
}
// Deregister deregisters stream from the registry
func (r *StreamRegistry) Deregister(streamID uint64) {
r.Lock()
defer r.Unlock()
s, ok := r.Streams[streamID]
if !ok {
return
}
p := s.peer
r.conns[p]--
if r.conns[p] < 1 {
delete(r.conns, p)
r.ConnManager.UntagPeer(p, cmgrTag)
}
delete(r.Streams, streamID)
}
// Close stream endpoints and deregister it
func (r *StreamRegistry) Close(s *Stream) {
_ = s.Local.Close()
_ = s.Remote.Close()
s.Registry.Deregister(s.id)
}
// Reset closes stream endpoints and deregisters it
func (r *StreamRegistry) Reset(s *Stream) {
_ = s.Local.Close()
_ = s.Remote.Reset()
s.Registry.Deregister(s.id)
}