forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.go
54 lines (42 loc) · 1023 Bytes
/
listen.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
package client
import (
"net"
"github.com/libp2p/go-libp2p/core/transport"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
var _ manet.Listener = (*Listener)(nil)
type Listener Client
func (c *Client) Listener() *Listener {
return (*Listener)(c)
}
func (l *Listener) Accept() (manet.Conn, error) {
for {
select {
case evt := <-l.incoming:
err := evt.writeResponse()
if err != nil {
log.Debugf("error writing relay response: %s", err.Error())
evt.conn.stream.Reset()
continue
}
log.Debugf("accepted relay connection from %s through %s", evt.conn.remote.ID, evt.conn.RemoteMultiaddr())
evt.conn.tagHop()
return evt.conn, nil
case <-l.ctx.Done():
return nil, transport.ErrListenerClosed
}
}
}
func (l *Listener) Addr() net.Addr {
return &NetAddr{
Relay: "any",
Remote: "any",
}
}
func (l *Listener) Multiaddr() ma.Multiaddr {
return circuitAddr
}
func (l *Listener) Close() error {
return (*Client)(l).Close()
}