-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
175 lines (156 loc) · 3.19 KB
/
session.go
File metadata and controls
175 lines (156 loc) · 3.19 KB
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
package dagger
import (
"errors"
"net"
"sync"
"sync/atomic"
"time"
)
var (
// ErrConnClosed ..
ErrConnClosed = errors.New("connection closed")
// ErrWriteBlocked ..
ErrWriteBlocked = errors.New("write packet blocked")
)
// Session ...
type Session struct {
server *Server // server pointer
conn *net.TCPConn // raw conn
closeChan chan struct{} // close channel
closeFlag int32 // close flag, set to 1 when closed
closeOnce sync.Once // close session only once
sendChan chan Packet // packet send chan
recvChan chan Packet // packet receive chan
}
func newSession(conn *net.TCPConn, server *Server) *Session {
return &Session{
server: server,
conn: conn,
closeChan: make(chan struct{}),
sendChan: make(chan Packet, server.packetSendSize),
recvChan: make(chan Packet, server.packetRecvSize),
}
}
// RawConn is the conn instance getter
func (s *Session) RawConn() *net.TCPConn {
return s.conn
}
// IsClosed indicates whether session is closed
func (s *Session) IsClosed() bool {
return atomic.LoadInt32(&s.closeFlag) == 1
}
// Close closes the session
func (s *Session) Close() {
s.closeOnce.Do(func() {
atomic.StoreInt32(&s.closeFlag, 1)
close(s.closeChan)
close(s.sendChan)
close(s.recvChan)
s.conn.Close()
s.server.callback.OnClose(s)
})
}
// SendPacket sends a packet to the sendChan, the write loop will write the packet into the raw conn
// this method will never block
func (s *Session) SendPacket(p Packet, timeout time.Duration) (err error) {
if s.IsClosed() {
return ErrConnClosed
}
if timeout == 0 {
select {
case s.sendChan <- p:
return nil
default:
return ErrWriteBlocked
}
}
select {
case s.sendChan <- p:
return nil
case <-s.closeChan:
return ErrConnClosed
case <-time.After(timeout):
return ErrWriteBlocked
}
}
// Work is the session handler
func (s *Session) Work() {
if s.server.callback.OnConnect(s) == false {
return
}
goWork(s.handleLoop, s.server.waitGroup)
goWork(s.readLoop, s.server.waitGroup)
goWork(s.writeLoop, s.server.waitGroup)
}
// read packet from the conn and send to recvChan
func (s *Session) readLoop() {
defer func() {
recover()
s.Close()
}()
for {
select {
case <-s.server.exitChan:
return
case <-s.closeChan:
return
default:
}
p, err := s.server.protocol.ReadPacket(s.conn)
if err != nil {
return
}
s.recvChan <- p
}
}
// receive data from the sendChan, write to conn
func (s *Session) writeLoop() {
defer func() {
recover()
s.Close()
}()
for {
select {
case <-s.server.exitChan:
return
case <-s.closeChan:
return
case p := <-s.sendChan:
if s.IsClosed() {
return
}
if _, err := s.conn.Write(p.Serialize()); err != nil {
return
}
}
}
}
func (s *Session) handleLoop() {
defer func() {
recover()
s.Close()
}()
for {
select {
case <-s.server.exitChan:
return
case <-s.closeChan:
return
case p := <-s.recvChan:
if s.IsClosed() {
return
}
if !s.server.callback.OnMessage(s, p) { // handle message callback
return
}
}
}
}
// run function in a goroutine with waitgroup managed
func goWork(f func(), wg *sync.WaitGroup) {
wg.Add(1)
go func() {
f()
wg.Done()
}()
}