-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_session.go
176 lines (155 loc) · 3.04 KB
/
tcp_session.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
package jsn_net
import (
"io"
"net"
"sync"
"time"
)
type Pipe interface {
Post(in any) bool
Run()
Close()
}
type PipeProvider func() Pipe
type (
TcpCodec interface {
Read(io.Reader) (any, error)
Write(io.Writer, any)
}
)
type sessionConstructor struct {
providePipe PipeProvider
codec TcpCodec
readTimeout time.Duration
sendChanSize int
}
func NewTcpSession(sid uint64, conn *net.TCPConn, pipe Pipe, codec TcpCodec, readTimeout time.Duration, sendChanSize int) *TcpSession {
s := new(TcpSession)
s.sid = sid
s.conn = conn
s.pipe = pipe
s.codec = codec
s.sendChan = make(chan any, sendChanSize)
s.readTimeout = readTimeout
s.tag = LifeTag{}
s.goWg = sync.WaitGroup{}
return s
}
type TcpSession struct {
sid uint64
conn net.Conn
pipe Pipe
codec TcpCodec
sendChan chan any
readTimeout time.Duration
tag LifeTag
goWg sync.WaitGroup
}
func (s *TcpSession) Sid() uint64 {
return s.sid
}
func (s *TcpSession) Post(fn func()) bool {
if !s.tag.IsRunning() {
return false
}
s.pipe.Post(&TcpEventOutSide{
S: s,
Func: fn,
})
return true
}
// 主loop阻塞问题
// 同步处理问题
// tcp 写肯定需要保证不丢失
// todo
func (s *TcpSession) Send(msg any) bool {
// 如果选择同步写,就阻塞
// 如果异步,是否阻塞外部写?限流?
s.sendChan <- msg
return true
}
func (s *TcpSession) Close() {
if !s.tag.IsRunning() {
return
}
s.tag.SetRunning(false)
s.manualClose()
}
func (s *TcpSession) run() {
if s.tag.IsRunning() {
return
}
s.tag.SetRunning(true)
WaitGo(&s.goWg, s.pipe.Run)
WaitGo(&s.goWg, s.write)
s.pipe.Post(&TcpEventAdd{
S: s,
})
s.read()
s.goWg.Wait()
s.tag.SetRunning(false)
}
func (s *TcpSession) SetReadTimeoutOnce(timeout time.Duration) {
if err := s.conn.SetReadDeadline(time.Now().Add(timeout)); nil != err {
s.pipe.Post(&TcpEventClose{
S: s,
Err: err,
ManualClose: !s.tag.IsRunning(),
})
}
}
func (s *TcpSession) CancelLastTimeout() {
if err := s.conn.SetReadDeadline(time.Time{}); nil != err {
s.pipe.Post(&TcpEventClose{
S: s,
Err: err,
ManualClose: !s.tag.IsRunning(),
})
}
}
func (s *TcpSession) read() {
for {
if 0 != s.readTimeout {
if err := s.conn.SetReadDeadline(time.Now().Add(s.readTimeout)); nil != err {
s.pipe.Post(&TcpEventClose{
S: s,
Err: err,
ManualClose: !s.tag.IsRunning(),
})
break
}
}
packet, err := s.codec.Read(s.conn)
if nil != err {
s.pipe.Post(&TcpEventClose{
S: s,
Err: err,
ManualClose: !s.tag.IsRunning(),
})
break
}
s.pipe.Post(&TcpEventPacket{
S: s,
Packet: packet,
})
}
s.tag.SetRunning(false)
s.sendChan <- nil
}
func (s *TcpSession) write() {
for {
msg := <-s.sendChan
if nil == msg {
break
}
s.codec.Write(s.conn, msg)
}
if nil != s.conn {
_ = s.conn.Close()
}
s.pipe.Close()
}
func (s *TcpSession) manualClose() {
_ = s.conn.(*net.TCPConn).CloseRead()
_ = s.conn.SetReadDeadline(time.Now())
}