/
serverSessionMgr.go
51 lines (42 loc) · 951 Bytes
/
serverSessionMgr.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
package Kcpnet
import (
"sync"
)
type SessionMgr struct {
sessionMap sync.Map
}
var (
GServer2ServerSession *SessionMgr
GClient2ServerSession *SessionMgr
)
func (this *SessionMgr) AddSession(key interface{}, sess TcpSession) {
this.sessionMap.Store(key, sess)
}
func (this *SessionMgr) GetSession(key interface{}) (sess TcpSession) {
v, exist := this.sessionMap.Load(key)
if exist {
sess = v.(TcpSession)
}
return
}
func (this *SessionMgr) RemoveSession(key interface{}) (exist bool) {
_, exist = this.sessionMap.Load(key)
if exist {
this.sessionMap.Delete(key)
}
return
}
func (this *SessionMgr) GetSessionByIdentify(key interface{}) (sess TcpSession) {
val, exist := this.sessionMap.Load(key)
if exist {
sess = val.(TcpSession)
}
return
}
func (this *SessionMgr) GetAllSession() (sess sync.Map) {
return this.sessionMap
}
func init() {
GServer2ServerSession = &SessionMgr{}
GClient2ServerSession = &SessionMgr{}
}