-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
176 lines (143 loc) · 3.15 KB
/
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 sockit
import (
"context"
"errors"
"github.com/sirupsen/logrus"
"net"
"sync"
"sync/atomic"
"time"
)
type Session struct {
id int64
c Conn
mgr ConnManager
handler Handler
user User
dataLock *sync.RWMutex
data map[string]interface{} // 用户自定义数据
lastPackTs time.Time // 最后一个收到包的时间
reqLock *sync.RWMutex
requests map[int64]chan Packet
manuallyClosed bool
closed chan struct{}
}
var idGenerator int64
func NewSession(c Conn, mgr ConnManager, user User, handler Handler) *Session {
sess := &Session{
id: atomic.AddInt64(&idGenerator, 1),
c: c,
mgr: mgr,
user: user,
handler: handler,
dataLock: &sync.RWMutex{},
data: make(map[string]interface{}),
reqLock: &sync.RWMutex{},
requests: make(map[int64]chan Packet),
closed: make(chan struct{}),
lastPackTs: time.Now(),
}
go sess.readPacket()
return sess
}
func (s *Session) readPacket() {
for {
packet, err := s.c.ReadPacket()
if err != nil {
if !errors.Is(err, net.ErrClosed) {
logrus.WithFields(logrus.Fields{
"remoteAddr": s.c.RemoteAddr().String(),
"sessionId": s.Id(),
}).Errorln("read packet error:", err.Error())
}
s.mgr.RemoveSession(s.Id())
return
}
logrus.WithFields(logrus.Fields{
"remoteAddr": s.c.RemoteAddr().String(),
"sessionId": s.Id(),
}).Debug("receive a packet")
s.lastPackTs = time.Now()
s.reqLock.RLock()
ch, ok := s.requests[packet.Id()]
s.reqLock.RUnlock()
if ok {
ch <- packet
close(ch)
s.reqLock.Lock()
delete(s.requests, packet.Id())
s.reqLock.Unlock()
} else {
go s.handler.Handle(packet, s)
}
}
}
// Id returns current session id
func (s *Session) Id() int64 {
return s.id
}
func (s *Session) User() User {
return s.user
}
func (s *Session) Set(key string, value interface{}) {
s.dataLock.Lock()
defer s.dataLock.Unlock()
s.data[key] = value
}
func (s *Session) Get(key string) (interface{}, bool) {
s.dataLock.RLock()
defer s.dataLock.RUnlock()
val, ok := s.data[key]
return val, ok
}
func (s *Session) close() error {
select {
case <-s.closed:
return nil
default:
}
close(s.closed)
return s.c.Close()
}
func (s *Session) Close() error {
if err := s.mgr.RemoveSession(s.Id()); err != nil {
return err
}
return s.close()
}
func (s *Session) SendPacket(p Packet) error {
return s.c.SendPacket(p)
}
func (s *Session) SendRequest(p Packet) (<-chan Packet, error) {
ch := make(chan Packet, 1)
s.reqLock.Lock()
s.requests[p.Id()] = ch
s.reqLock.Unlock()
if err := s.SendPacket(p); err != nil {
return nil, err
}
return ch, nil
}
func (s *Session) SendRequestTimeout(p Packet, timeout time.Duration) (Packet, error) {
ch := make(chan Packet, 1)
s.reqLock.Lock()
s.requests[p.Id()] = ch
s.reqLock.Unlock()
if err := s.SendPacket(p); err != nil {
return nil, err
}
timer := time.NewTimer(timeout)
select {
case p := <-ch:
timer.Stop()
return p, nil
case <-timer.C:
return nil, context.DeadlineExceeded
}
}
func (s *Session) LocalAddr() net.Addr {
return s.c.LocalAddr()
}
func (s *Session) RemoteAddr() net.Addr {
return s.c.RemoteAddr()
}