-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtcp_connect.go
340 lines (309 loc) · 6.54 KB
/
tcp_connect.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package jaguar
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
"reflect"
"runtime/debug"
"time"
)
type TcpConn interface {
//Additional plug-in objects, available by relying on injection
Attach(plugin interface{})
//Additional plug-in objects, interface sits, which can be obtained by relying on injection
AttachImpl(impl string, plugin interface{})
//Actively close the connection
Close()
//Remote address connected
RemoteAddr() net.Addr
//Push
Push(pushHandle Encode) error
}
func init() {
routeMap = make(map[uint16]interface{})
}
var routeMap map[uint16]interface{}
// AddRequest - Join the request processor
func AddRequest(protocolId uint16, req ReqHandle) {
type execute interface {
Execute()
}
_, ok := req.(execute)
if !ok {
panic(fmt.Sprintf("id : %d", protocolId) + " No Execute implementation method")
}
routeMap[protocolId] = req
}
// newConn
func newConn(conn net.Conn, hook *Middleware) *tcpConn {
ts := new(tcpConn)
ts.Conn = conn
ts.writeBuffer = make(chan []byte, 4096)
ts.attach = NewJMap()
ts.AttachImpl("tcp_conn", ts)
ts.hook = hook
return ts
}
type tcpConn struct {
net.Conn
writeBuffer chan []byte
attach *JMap
attachImpl *JMap
hook *Middleware
}
// write
func (tc *tcpConn) write() {
for {
data := <-tc.writeBuffer
headLen := len(data)
if headLen == 0 {
return
}
headData := tc.packetLenToByte(headLen)
tc.SetWriteDeadline(time.Now().Add(time.Second * 15))
if _, err := tc.Write(headData); err != nil {
return
}
if _, err := tc.Write(data); err != nil {
return
}
}
}
// packetLenToInt
func (tc *tcpConn) packetLenToInt(head []byte) int {
buffer := bytes.NewBuffer(head)
switch len(head) {
case 1:
var x uint8
binary.Read(buffer, _opt.ByteOrder, &x)
return int(x)
case 2:
var x uint16
binary.Read(buffer, _opt.ByteOrder, &x)
return int(x)
case 4:
var x uint32
binary.Read(buffer, _opt.ByteOrder, &x)
return int(x)
case 8:
var x uint64
binary.Read(buffer, _opt.ByteOrder, &x)
return int(x)
}
tc.Conn.Close()
return 0
}
// packetSize
func (tc *tcpConn) packetLenToByte(plen int) []byte {
switch _opt.PacketHeaderLength {
case 1:
data, err := toBytes(uint8(plen))
if err == nil {
return data
}
case 2:
data, err := toBytes(uint16(plen))
if err == nil {
return data
}
case 4:
data, err := toBytes(uint32(plen))
if err == nil {
return data
}
case 8:
data, err := toBytes(uint64(plen))
if err == nil {
return data
}
}
e := errors.New("Opt.PacketHeaderLength error")
for _, f := range tc.hook.recover {
f(e, "")
}
tc.Conn.Close()
return []byte{}
}
// break
func (tc *tcpConn) readBreak() {
for _, f := range tc.hook.closed {
f()
}
}
//Close 关闭
func (tc *tcpConn) Close() {
tc.writeBuffer <- make([]byte, 0)
tc.Conn.Close()
}
func (tc *tcpConn) send(data []byte) {
dataLen := len(data)
if dataLen == 0 {
return
}
if dataLen > _opt.PacketMaxLength {
e := errors.New("Package length exceeds upper limit.")
for _, f := range tc.hook.recover {
f(e, "")
}
return
}
tc.writeBuffer <- data
}
type Encode interface {
Encode()
ProtocolId() uint16
}
func (tc *tcpConn) Push(pushHandle Encode) error {
protocolId := pushHandle.ProtocolId()
ph := newPushHandle(protocolId)
ph.di(pushHandle)
for _, f := range tc.hook.push {
f(protocolId, pushHandle)
}
pushHandle.Encode()
if ph.outBuffer == nil || ph.outBuffer.Len() == 0 {
return errors.New("PushHandle doesn't writeStream")
}
if ph.writeError == nil {
for _, f := range tc.hook.writer {
ph.outBuffer = f(protocolId, ph.outBuffer)
}
tc.send(ph.outBuffer.Bytes())
}
return ph.writeError
}
func (tc *tcpConn) respone(protocolId uint16, buffer *bytes.Buffer) {
for _, f := range tc.hook.writer {
buffer = f(protocolId, buffer)
}
sendData := buffer.Bytes()
tc.send(sendData)
}
// route
func (tc *tcpConn) route(buffer *bytes.Buffer) {
var id uint16 = 0
defer func() {
if perr := recover(); perr != nil {
e := errors.New(fmt.Sprint(perr))
stack := string(debug.Stack())
for _, f := range tc.hook.recover {
f(e, stack)
}
}
}()
binary.Read(buffer, binary.BigEndian, &id)
for _, f := range tc.hook.reader {
buffer = f(id, buffer)
}
req, ok := routeMap[id]
if !ok {
return
}
t := reflect.TypeOf(req)
if t == nil {
tc.Close()
return
}
newReq := reflect.New(t.Elem())
handle := newReqHandle(id, tc, buffer)
handle.di(newReq.Interface())
tc.di(newReq.Interface())
for _, f := range tc.hook.request {
f(id, newReq.Interface())
}
newReq.MethodByName("Execute").Call(nil)
}
func (tc *tcpConn) uintToBytes(number uint32) []byte {
buffer := make([]byte, 4)
binary.BigEndian.PutUint32(buffer, number)
return buffer
}
// Attach
func (tc *tcpConn) Attach(obj interface{}) {
if obj == nil {
panic("obj not nil")
}
objValue := reflect.ValueOf(obj)
if objValue.Kind() != reflect.Ptr {
panic("Use a pointer object, The " + fmt.Sprint(obj))
}
if tc.attach.Exist(obj) {
panic("Object has been registered, The " + fmt.Sprint(obj))
}
tc.attach.Set(reflect.TypeOf(obj).String(), obj)
}
// AttachImpl
func (tc *tcpConn) AttachImpl(impl string, obj interface{}) {
if obj == nil {
panic("obj not nil")
}
tc.attach.Set("inject%_%"+impl, obj)
}
func (tc *tcpConn) di(child interface{}) {
structFields(child, func(sf reflect.StructField, value reflect.Value) {
tag, inject := sf.Tag.Lookup("inject")
if !inject {
return
}
if tag != "" {
obj := tc.attach.Interface("inject%_%" + tag)
if obj != nil {
value.Set(reflect.ValueOf(obj))
}
return
}
obj := tc.attach.Interface(value.Type().String())
if obj != nil {
value.Set(reflect.ValueOf(obj))
}
})
}
func (tc *tcpConn) attachDi() {
for _, obj := range tc.attach._map {
tc.di(obj)
}
}
// read
func (tc *tcpConn) read() {
action := 1
bodyLen := 0
packet := new(bytes.Buffer)
for {
tc.SetReadDeadline(time.Now().Add(_opt.IdleCheckFrequency))
packet.Reset()
var data []byte
var trialLen int
if action == 1 {
trialLen = int(_opt.PacketHeaderLength)
} else {
trialLen = int(bodyLen)
}
for trialLen > 0 {
data = make([]byte, trialLen)
len, derr := tc.Conn.Read(data)
if len == 0 || derr != nil {
tc.Conn.Close()
tc.readBreak()
return
}
packet.Write(data[:len])
trialLen = trialLen - len
}
if action == 1 {
action = 2
bodyLen = tc.packetLenToInt(packet.Bytes())
if bodyLen > _opt.PacketMaxLength {
tc.Conn.Close()
tc.readBreak()
return
}
} else {
action = 1
packetHandle := new(bytes.Buffer)
packetHandle.Write(packet.Bytes())
go tc.route(packetHandle)
}
}
}