forked from funny/link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
executable file
·202 lines (168 loc) · 4.17 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
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
package link
import (
"errors"
"sync"
"sync/atomic"
)
var SessionClosedError = errors.New("Session Closed")
var SessionBlockedError = errors.New("Session Blocked")
var globalSessionId uint64
type Session[S, R any] struct {
id uint64
codec Codec[S, R]
manager *Manager[S, R]
sendChan chan S
recvMutex sync.Mutex
sendMutex sync.RWMutex
closeFlag int32
closeChan chan int
closeMutex sync.Mutex
firstCloseCallback *closeCallback
lastCloseCallback *closeCallback
State interface{}
}
func NewSession[S, R any](codec Codec[S, R], sendChanSize int) *Session[S, R] {
return newSession(nil, codec, sendChanSize)
}
func newSession[S, R any](manager *Manager[S, R], codec Codec[S, R], sendChanSize int) *Session[S, R] {
session := &Session[S, R]{
codec: codec,
manager: manager,
closeChan: make(chan int),
id: atomic.AddUint64(&globalSessionId, 1),
}
if sendChanSize > 0 {
session.sendChan = make(chan S, sendChanSize)
go session.sendLoop()
}
return session
}
func (session *Session[S, R]) ID() uint64 {
return session.id
}
func (session *Session[S, R]) IsClosed() bool {
return atomic.LoadInt32(&session.closeFlag) == 1
}
func (session *Session[S, R]) Close() error {
if atomic.CompareAndSwapInt32(&session.closeFlag, 0, 1) {
close(session.closeChan)
if session.sendChan != nil {
session.sendMutex.Lock()
close(session.sendChan)
if clear, ok := session.codec.(ClearSendChan[S]); ok {
clear.ClearSendChan(session.sendChan)
}
session.sendMutex.Unlock()
}
err := session.codec.Close()
go func() {
session.invokeCloseCallbacks()
if session.manager != nil {
session.manager.delSession(session)
}
}()
return err
}
return SessionClosedError
}
func (session *Session[S, R]) Codec() Codec[S, R] {
return session.codec
}
func (session *Session[S, R]) Receive() (R, error) {
session.recvMutex.Lock()
defer session.recvMutex.Unlock()
msg, err := session.codec.Receive()
if err != nil {
session.Close()
}
return msg, err
}
func (session *Session[S, R]) sendLoop() {
defer session.Close()
for {
select {
case msg, ok := <-session.sendChan:
if !ok || session.codec.Send(msg) != nil {
return
}
case <-session.closeChan:
return
}
}
}
func (session *Session[S, R]) Send(msg S) error {
if session.sendChan == nil {
if session.IsClosed() {
return SessionClosedError
}
session.sendMutex.Lock()
defer session.sendMutex.Unlock()
err := session.codec.Send(msg)
if err != nil {
session.Close()
}
return err
}
session.sendMutex.RLock()
if session.IsClosed() {
session.sendMutex.RUnlock()
return SessionClosedError
}
select {
case session.sendChan <- msg:
session.sendMutex.RUnlock()
return nil
default:
session.sendMutex.RUnlock()
session.Close()
return SessionBlockedError
}
}
type closeCallback struct {
Handler interface{}
Key interface{}
Func func()
Next *closeCallback
}
func (session *Session[S, R]) AddCloseCallback(handler, key interface{}, callback func()) {
if session.IsClosed() {
return
}
session.closeMutex.Lock()
defer session.closeMutex.Unlock()
newItem := &closeCallback{handler, key, callback, nil}
if session.firstCloseCallback == nil {
session.firstCloseCallback = newItem
} else {
session.lastCloseCallback.Next = newItem
}
session.lastCloseCallback = newItem
}
func (session *Session[S, R]) RemoveCloseCallback(handler, key interface{}) {
if session.IsClosed() {
return
}
session.closeMutex.Lock()
defer session.closeMutex.Unlock()
var prev *closeCallback
for callback := session.firstCloseCallback; callback != nil; prev, callback = callback, callback.Next {
if callback.Handler == handler && callback.Key == key {
if session.firstCloseCallback == callback {
session.firstCloseCallback = callback.Next
} else {
prev.Next = callback.Next
}
if session.lastCloseCallback == callback {
session.lastCloseCallback = prev
}
return
}
}
}
func (session *Session[S, R]) invokeCloseCallbacks() {
session.closeMutex.Lock()
defer session.closeMutex.Unlock()
for callback := session.firstCloseCallback; callback != nil; callback = callback.Next {
callback.Func()
}
}