-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathconn.go
83 lines (71 loc) · 1.8 KB
/
conn.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
package upgrader
import (
"fmt"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/transport"
)
type transportConn struct {
network.MuxedConn
network.ConnMultiaddrs
network.ConnSecurity
transport transport.Transport
scope network.ConnManagementScope
stat network.ConnStats
muxer protocol.ID
security protocol.ID
usedEarlyMuxerNegotiation bool
}
var _ transport.CapableConn = &transportConn{}
func (t *transportConn) Transport() transport.Transport {
return t.transport
}
func (t *transportConn) String() string {
ts := ""
if s, ok := t.transport.(fmt.Stringer); ok {
ts = "[" + s.String() + "]"
}
return fmt.Sprintf(
"<stream.Conn%s %s (%s) <-> %s (%s)>",
ts,
t.LocalMultiaddr(),
t.LocalPeer(),
t.RemoteMultiaddr(),
t.RemotePeer(),
)
}
func (t *transportConn) Stat() network.ConnStats {
return t.stat
}
func (t *transportConn) Scope() network.ConnScope {
return t.scope
}
func (t *transportConn) Close() error {
defer t.scope.Done()
return t.MuxedConn.Close()
}
func (t *transportConn) ConnState() network.ConnectionState {
return network.ConnectionState{
StreamMultiplexer: t.muxer,
Security: t.security,
Transport: "tcp",
UsedEarlyMuxerNegotiation: t.usedEarlyMuxerNegotiation,
}
}
func (t *transportConn) CloseWithError(errCode network.ConnErrorCode) error {
if ce, ok := t.MuxedConn.(interface {
CloseWithErrorChan(errCode network.ConnErrorCode) chan error
}); ok {
err := t.scope.ReserveMemory(10_000, network.ReservationPriorityMedium)
if err != nil {
return t.Close()
}
errCh := ce.CloseWithErrorChan(errCode)
go func() {
defer t.scope.Done()
<-errCh
}()
return nil
}
return t.Close()
}