-
Notifications
You must be signed in to change notification settings - Fork 17
/
tsession.go
223 lines (197 loc) · 5.07 KB
/
tsession.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
package turbo
import (
"bufio"
"bytes"
"net"
"time"
)
//turbo session
type TSession struct {
conn *net.TCPConn //tcp的session
remoteAddr string
br *bufio.Reader
bw *bufio.Writer
isClose bool
lasttime uint32
config *TConfig
onMessage IOHandler
}
func NewSession(conn *net.TCPConn, config *TConfig,
onMsg IOHandler) *TSession {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(config.IdleTime * 2)
//禁用nagle
conn.SetNoDelay(true)
conn.SetReadBuffer(config.ReadBufferSize)
conn.SetWriteBuffer(config.WriteBufferSize)
session := &TSession{
conn: conn,
br: bufio.NewReaderSize(conn, config.ReadBufferSize),
bw: bufio.NewWriterSize(conn, config.WriteBufferSize),
isClose: false,
remoteAddr: conn.RemoteAddr().String(),
onMessage: onMsg,
config: config}
//连接数计数
config.FlowStat.Connections.Incr(1)
//记录下当期那
config.FlowStat.Clients.Store(session.remoteAddr, nil)
return session
}
func (self *TSession) RemotingAddr() string {
return self.remoteAddr
}
func (self *TSession) Idle() bool {
//当前时间如果大于 最后一次发包时间+2倍的idletime 则认为空心啊
if uint32(time.Now().Unix())-
(self.lasttime+uint32(self.config.IdleTime/time.Second)) > 0 {
return true
}
return false
}
//读取
func (self *TSession) Open() {
go func() {
//缓存本次包的数据
for !self.isClose {
err := func() error {
defer func() {
if err := recover(); nil != err {
log.Errorf("TSession|Read|%s|recover|FAIL|%s", self.remoteAddr, err)
}
}()
//按照标准的turbo packet读取packet头部
buff, err := self.read0(self.br, PACKET_HEAD_LEN)
if nil != err {
self.Close()
return err
}
self.lasttime = uint32(time.Now().Unix())
br := bytes.NewReader(buff)
head, err := UnmarshalHeader(br)
if nil != err {
log.Errorf("TSession|UnmarshalHeader|%s|FAIL|CLOSE SESSION|%v",
self.remoteAddr, err)
self.onMessage(Packet{Header: head, Data: nil}, err)
return err
}
if head.BodyLen > MAX_PACKET_BYTES {
log.Errorf("TSession|UnmarshalHeader|%s|Too Large Packet|CLOSE SESSION|%v",
self.remoteAddr, head.BodyLen)
//构造一个error的响应包
self.onMessage(Packet{Header: head, Data: nil}, ERR_TOO_LARGE_PACKET)
return ERR_TOO_LARGE_PACKET
}
//读取body
body, err := self.read0(self.br, int(head.BodyLen))
if nil != err {
log.Errorf("TSession|ReadBody|%s|FAIL|CLOSE SESSION|%v|bodyLen:%d",
self.remoteAddr, err, head.BodyLen)
//构造一个error的读取包
self.onMessage(Packet{Header: head, Data: nil}, err)
return err
}
p := Packet{Header: head, Data: body}
//回调上层
self.onMessage(p, nil)
if nil != self.config.FlowStat {
self.config.FlowStat.ReadFlow.Incr(1)
self.config.FlowStat.ReadBytesFlow.Incr(PACKET_HEAD_LEN + head.BodyLen)
}
return nil
}()
if nil != err {
break
}
}
}()
}
//分段读取
func (self *TSession) read0(br *bufio.Reader, len int) ([]byte, error) {
//按照标准的turbo packet读取packet头部
buff := make([]byte, len)
idx := 0
for {
l, err := br.Read(buff[idx:])
if nil != err {
log.Errorf("TSession|Open|%s|FAIL|CLOSE SESSION|%s", self.remoteAddr, err)
return nil, err
}
idx += l
if idx >= len {
break
}
}
return buff, nil
}
//写数据
func (self *TSession) Write(tlv ...*Packet) error {
self.lasttime = uint32(time.Now().Unix())
batch := make([]byte, 0, len(tlv)*128)
for _, t := range tlv {
p := t.Marshal()
if nil == p || len(p) <= 0 {
log.Errorf("TSession|asyncWrite|MarshalPayload|FAIL|EMPTY PACKET")
if nil != t.OnComplete {
t.OnComplete(ERR_MARSHAL_PACKET)
}
//如果是同步写出
continue
}
//如果大小超过了最大值那么久写入失败
if t.Header.BodyLen > MAX_PACKET_BYTES {
if nil != t.OnComplete {
log.Errorf("TSession|asyncWrite|MarshalPayload|FAIL|MAX_PACKET_BYTES|%d/%d",
t.Header.BodyLen, MAX_PACKET_BYTES)
t.OnComplete(ERR_TOO_LARGE_PACKET)
}
continue
} else {
//成功写出去了
t.OnComplete(nil)
}
batch = append(batch, p...)
}
if len(batch) <= 0 {
return nil
}
l := 0
tmp := batch
for {
length, err := self.bw.Write(tmp)
if nil != err {
log.Errorf("TSession|asyncWrite|conn|%s|FAIL|%s|%d/%d", self.remoteAddr, err, length, len(tmp))
return err
}
l += length
//write finish
if l == len(batch) {
break
}
tmp = batch[l:]
}
// //flush
self.bw.Flush()
if nil != self.config.FlowStat {
self.config.FlowStat.WriteFlow.Incr(int32(len(tlv)))
self.config.FlowStat.WriteBytesFlow.Incr(int32(len(batch)))
}
return nil
}
//当前连接是否关闭
func (self *TSession) Closed() bool {
return self.isClose
}
func (self *TSession) Close() error {
if !self.isClose {
self.isClose = true
//flush
self.bw.Flush()
self.conn.Close()
self.config.FlowStat.Connections.Incr(-1)
//清理掉这个Clients
self.config.FlowStat.Clients.Delete(self.remoteAddr)
log.Infof("TSession|Close|%s...", self.remoteAddr)
}
return nil
}