-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession_manager.go
106 lines (89 loc) · 2.41 KB
/
session_manager.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
/*
* Copyright 2019 the go-netty project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"sync"
"github.com/go-netty/go-netty"
)
type Manager interface {
// middleware
netty.ActiveHandler
netty.InactiveHandler
// size of active channels.
Size() int
// find channel context with id.
Context(id int64) netty.HandlerContext
// foreach active channels.
ForEach(func(netty.HandlerContext) bool)
// broadcast message.
Broadcast(message netty.Message)
// broadcast message filter.
BroadcastIf(message netty.Message, fn func(netty.HandlerContext) bool)
}
func NewManager() Manager {
return &sessionManager{
_sessions: make(map[int64]netty.HandlerContext, 64),
}
}
type sessionManager struct {
_sessions map[int64]netty.HandlerContext
_mutex sync.RWMutex
}
func (s *sessionManager) Size() int {
s._mutex.RLock()
size := len(s._sessions)
s._mutex.RUnlock()
return size
}
func (s *sessionManager) Context(id int64) netty.HandlerContext {
s._mutex.RLock()
ctx, _ := s._sessions[id]
s._mutex.RUnlock()
return ctx
}
func (s *sessionManager) ForEach(fn func(netty.HandlerContext) bool) {
s._mutex.RLock()
defer s._mutex.RUnlock()
for _, ctx := range s._sessions {
fn(ctx)
}
}
func (s *sessionManager) Broadcast(message netty.Message) {
s.ForEach(func(ctx netty.HandlerContext) bool {
ctx.Write(message)
return true
})
}
func (s *sessionManager) BroadcastIf(message netty.Message, fn func(netty.HandlerContext) bool) {
s.ForEach(func(ctx netty.HandlerContext) bool {
if fn(ctx) {
ctx.Write(message)
}
return true
})
}
func (s *sessionManager) HandleActive(ctx netty.ActiveContext) {
s._mutex.Lock()
s._sessions[ctx.Channel().ID()] = ctx
s._mutex.Unlock()
ctx.HandleActive()
}
func (s *sessionManager) HandleInactive(ctx netty.InactiveContext, ex netty.Exception) {
s._mutex.Lock()
delete(s._sessions, ctx.Channel().ID())
s._mutex.Unlock()
ctx.HandleInactive(ex)
}