forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
105 lines (84 loc) · 1.95 KB
/
connection.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
package tcp
import (
"io"
"net"
"time"
"v2ray.com/core/transport/internet/internal"
)
type ConnectionManager interface {
Put(internal.ConnectionId, net.Conn)
}
type RawConnection struct {
net.TCPConn
}
func (v *RawConnection) Reusable() bool {
return false
}
func (v *RawConnection) SetReusable(b bool) {}
func (v *RawConnection) SysFd() (int, error) {
return internal.GetSysFd(&v.TCPConn)
}
type Connection struct {
id internal.ConnectionId
conn net.Conn
listener ConnectionManager
reusable bool
config *Config
}
func NewConnection(id internal.ConnectionId, conn net.Conn, manager ConnectionManager, config *Config) *Connection {
return &Connection{
id: id,
conn: conn,
listener: manager,
reusable: config.ConnectionReuse.IsEnabled(),
config: config,
}
}
func (v *Connection) Read(b []byte) (int, error) {
if v == nil || v.conn == nil {
return 0, io.EOF
}
return v.conn.Read(b)
}
func (v *Connection) Write(b []byte) (int, error) {
if v == nil || v.conn == nil {
return 0, io.ErrClosedPipe
}
return v.conn.Write(b)
}
func (v *Connection) Close() error {
if v == nil || v.conn == nil {
return io.ErrClosedPipe
}
if v.Reusable() {
v.listener.Put(v.id, v.conn)
return nil
}
err := v.conn.Close()
v.conn = nil
return err
}
func (v *Connection) LocalAddr() net.Addr {
return v.conn.LocalAddr()
}
func (v *Connection) RemoteAddr() net.Addr {
return v.conn.RemoteAddr()
}
func (v *Connection) SetDeadline(t time.Time) error {
return v.conn.SetDeadline(t)
}
func (v *Connection) SetReadDeadline(t time.Time) error {
return v.conn.SetReadDeadline(t)
}
func (v *Connection) SetWriteDeadline(t time.Time) error {
return v.conn.SetWriteDeadline(t)
}
func (v *Connection) SetReusable(reusable bool) {
v.reusable = reusable
}
func (v *Connection) Reusable() bool {
return v.config.ConnectionReuse.IsEnabled() && v.reusable
}
func (v *Connection) SysFd() (int, error) {
return internal.GetSysFd(v.conn)
}