-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcp_conn.go
174 lines (143 loc) · 3.45 KB
/
tcp_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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* tcp connection adapter
*/
package tok
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"time"
)
const (
tcpHeaderLen = 4
)
var (
// TCPMaxPackLen upper limit for single message
TCPMaxPackLen uint32 = 4 * 1024 * 1024
)
type tcpAdapter struct {
conn net.Conn
readTimeout time.Duration
}
func (p *tcpAdapter) Read() ([]byte, error) {
var deadline time.Time
if p.readTimeout > 0 {
deadline = time.Now().Add(p.readTimeout)
} else {
deadline = time.Time{}
}
if err := p.conn.SetReadDeadline(deadline); err != nil {
log.Println("[warning] setting read deadline: ", err)
return nil, err
}
// read header
b := make([]byte, tcpHeaderLen)
if _, err := io.ReadFull(p.conn, b); err != nil {
return nil, err
}
buf := bytes.NewBuffer(b)
var n uint32
if err := binary.Read(buf, binary.BigEndian, &n); err != nil {
return nil, err
}
if n > TCPMaxPackLen {
return nil, fmt.Errorf("pack length %dM can't greater than %dM", n/1024/1024, TCPMaxPackLen/1024/1024)
}
if p.readTimeout > 0 {
deadline = time.Now().Add(p.readTimeout)
} else {
deadline = time.Time{}
}
if err := p.conn.SetReadDeadline(deadline); err != nil {
log.Println("[warning] setting read deadline: ", err)
return nil, err
}
b = make([]byte, n)
_, err := io.ReadFull(p.conn, b)
return b, err
}
func (p *tcpAdapter) Write(b []byte) error {
// set write deadline
if err := p.conn.SetWriteDeadline(time.Now().Add(WriteTimeout)); err != nil {
log.Println("[warning] setting write deadline fail: ", err)
return err
}
n := uint32(len(b))
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, &n); err != nil {
return err
}
_, err := p.conn.Write(append(buf.Bytes(), b...))
return err
}
func (p *tcpAdapter) Close() {
p.conn.Close()
}
func (p *tcpAdapter) ShareConn(adapter conAdapter) bool {
tcpAdp, ok := adapter.(*tcpAdapter)
if !ok {
return false
}
return p.conn == tcpAdp.conn
}
// Listen create Tcp listener with hub.
// If config is not nil, a new hub will be created and replace the old one.
// addr is the tcp address to be listened on.
// auth function is used for user authorization
// return error if listen failed.
func Listen(hub *Hub, config *HubConfig, addr string, auth TCPAuthFunc) (*Hub, error) {
if config != nil {
hub = createHub(config.Actor, config.Q, config.Sso)
}
if hub == nil {
log.Fatal("hub is needed")
}
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
initAuth := func(conn net.Conn) {
// log.Println("raw tcp connection", conn.RemoteAddr())
if err := conn.SetReadDeadline(time.Now().Add(AuthTimeout)); err != nil {
log.Println("set auth deadline err: ", err)
conn.Close()
return
}
adapter := &tcpAdapter{conn: conn, readTimeout: AuthTimeout}
b, err := adapter.Read()
if err != nil {
// log.Println("tcp auth err ", err)
adapter.Close()
return
}
dv, err := auth(b)
if err != nil {
log.Printf("tcp auth err: %+v", err)
adapter.Close()
return
}
if ReadTimeout > 0 {
adapter.readTimeout = ReadTimeout
} else {
adapter.readTimeout = 0
}
initConnection(dv, adapter, hub)
}
go func() {
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error accepting", err)
continue
}
go initAuth(conn)
}
}()
return hub, nil
}
// TCPAuthFunc tcp auth function
// parameter is the first package content of connection. return Device interface
type TCPAuthFunc func([]byte) (*Device, error)