p2p: use host.SetStreamHandlerMatch for now
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Sep 3, 2018 at 11:41 UTC
0f9d28442159e4d4b6645708d1a0fc36f47e7a0e
3 files changed
+68
-35
p2p/listener.go
+38
-1
@@ -4,8 +4,11 @@ import (
4
"errors"
5
"sync"
6
7
+ net "gx/ipfs/QmPjvxTpVH8qJyQDnxnsxF9kv9jezKD1kozz1hs3fCGsNh/go-libp2p-net"
8
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
9
"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
10
+ p2phost "gx/ipfs/Qmb8T6YBBsjYsVGfrihQLfCJveczZnneSBqBKkYEBWDjge/go-libp2p-host"
11
+ peer "gx/ipfs/QmdVrMn1LhB4ybb8hMVaMLXnA8XRSewMnK6YqXKXoTcRvN/go-libp2p-peer"
12
)
13
14
// Listener listens for connections and proxies them to a target
@@ -28,12 +31,46 @@ type listenerKey struct {
31
32
// ListenerRegistry is a collection of local application proto listeners.
33
type ListenerRegistry struct {
31
- sync.Mutex
34
+ sync.RWMutex
35
36
Listeners map[listenerKey]Listener
37
starting map[listenerKey]struct{}
38
}
39
40
+func newListenerRegistry(id peer.ID, host p2phost.Host) *ListenerRegistry {
41
+ reg := &ListenerRegistry{
42
+ Listeners: map[listenerKey]Listener{},
43
+ starting: map[listenerKey]struct{}{},
44
+ }
45
+
46
+ addr, err := ma.NewMultiaddr(maPrefix + id.Pretty())
47
+ if err != nil {
48
+ panic(err)
49
+ }
50
+
51
+ host.SetStreamHandlerMatch("/x/", func(p string) bool {
52
+ reg.RLock()
53
+ defer reg.RUnlock()
54
+ for _, l := range reg.Listeners {
55
+ if l.ListenAddress().Equal(addr) && string(l.Protocol()) == p {
56
+ return true
57
+ }
58
+ }
59
+
60
+ return false
61
+ }, func(stream net.Stream) {
62
+ for _, l := range reg.Listeners {
63
+ if l.ListenAddress().Equal(addr) && l.Protocol() == stream.Protocol() {
64
+ l.(*remoteListener).handleStream(stream)
65
+ }
66
+ }
67
+
68
+ // panic?
69
+ })
70
+
71
+ return reg
72
+}
73
+
74
// Register registers listenerInfo into this registry and starts it
75
func (r *ListenerRegistry) Register(l Listener) error {
76
r.Lock()
p2p/p2p.go
+1
-4
@@ -26,10 +26,7 @@ func NewP2P(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore)
26
peerHost: peerHost,
27
peerstore: peerstore,
28
29
- Listeners: &ListenerRegistry{
30
- Listeners: map[listenerKey]Listener{},
31
- starting: map[listenerKey]struct{}{},
32
- },
29
+ Listeners: newListenerRegistry(identity, peerHost),
30
Streams: &StreamRegistry{
31
Streams: map[uint64]*Stream{},
32
},
p2p/remote.go
+29
-30
@@ -39,46 +39,45 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto protocol.ID, addr ma.Mu
39
}
40
41
func (l *remoteListener) start() error {
42
- // TODO: handle errors when https://github.com/libp2p/go-libp2p-host/issues/16 will be done
43
- l.p2p.peerHost.SetStreamHandler(l.proto, func(remote net.Stream) {
44
- local, err := manet.Dial(l.addr)
45
- if err != nil {
46
- remote.Reset()
47
- return
48
- }
42
+ return nil
43
+}
44
50
- peer := remote.Conn().RemotePeer()
45
+func (l *remoteListener) handleStream(remote net.Stream) {
46
+ local, err := manet.Dial(l.addr)
47
+ if err != nil {
48
+ remote.Reset()
49
+ return
50
+ }
51
52
- peerMa, err := ma.NewMultiaddr(maPrefix + peer.Pretty())
53
- if err != nil {
54
- remote.Reset()
55
- return
56
- }
52
+ peer := remote.Conn().RemotePeer()
53
58
- cmgr := l.p2p.peerHost.ConnManager()
59
- cmgr.TagPeer(peer, CMGR_TAG, 20)
54
+ peerMa, err := ma.NewMultiaddr(maPrefix + peer.Pretty())
55
+ if err != nil {
56
+ remote.Reset()
57
+ return
58
+ }
59
61
- stream := &Stream{
62
- Protocol: l.proto,
60
+ cmgr := l.p2p.peerHost.ConnManager()
61
+ cmgr.TagPeer(peer, CMGR_TAG, 20)
62
64
- OriginAddr: peerMa,
65
- TargetAddr: l.addr,
63
+ stream := &Stream{
64
+ Protocol: l.proto,
65
67
- Local: local,
68
- Remote: remote,
66
+ OriginAddr: peerMa,
67
+ TargetAddr: l.addr,
68
70
- Registry: l.p2p.Streams,
69
+ Local: local,
70
+ Remote: remote,
71
72
- cleanup: func() {
73
- cmgr.UntagPeer(peer, CMGR_TAG)
74
- },
75
- }
72
+ Registry: l.p2p.Streams,
73
77
- l.p2p.Streams.Register(stream)
78
- stream.startStreaming()
79
- })
74
+ cleanup: func() {
75
+ cmgr.UntagPeer(peer, CMGR_TAG)
76
+ },
77
+ }
78
81
- return nil
79
+ l.p2p.Streams.Register(stream)
80
+ stream.startStreaming()
81
}
82
83
func (l *remoteListener) Protocol() protocol.ID {