You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet/conn/manager.go

124 lines
2.2 KiB
Go

package conn
2 years ago
import (
"sync"
)
type ConnManager struct {
2 years ago
sync.RWMutex
// 分组
groups map[string]*Group
// 所有 Connection
conns map[int64]*Connection
2 years ago
}
func NewConnManager() *ConnManager {
return &ConnManager{
2 years ago
RWMutex: sync.RWMutex{},
groups: make(map[string]*Group),
conns: make(map[int64]*Connection),
2 years ago
}
}
// Store 保存连接,同时加入到指定分组,若给定分组名为空,则不进行分组操作
func (m *ConnManager) Store(groupName string, c *Connection) error {
2 years ago
m.Lock()
m.conns[c.Session().ID()] = c
2 years ago
m.Unlock()
group, ok := m.FindGroup(groupName)
if !ok {
group = m.NewGroup(groupName)
}
return group.Add(c)
2 years ago
}
func (m *ConnManager) Remove(c *Connection) error {
m.Lock()
defer m.Unlock()
delete(m.conns, c.Session().ID())
// 从所有group中移除
for _, group := range m.groups {
err := group.Leave(c)
if err != nil {
return err
}
}
return nil
}
func (m *ConnManager) RemoveFromGroup(groupName string, c *Connection) error {
m.Lock()
delete(m.conns, c.Session().ID())
m.Unlock()
group, ok := m.FindGroup(groupName)
if !ok {
return nil
}
return group.Leave(c)
}
2 years ago
// NewGroup 新增分组,若分组已存在,则返回现有分组
func (m *ConnManager) NewGroup(name string) *Group {
2 years ago
m.Lock()
defer m.Unlock()
group, ok := m.groups[name]
if ok {
return group
}
group = NewGroup(name)
m.groups[name] = group
return group
}
// FindGroup 查找分组
func (m *ConnManager) FindGroup(name string) (*Group, bool) {
2 years ago
m.RLock()
defer m.RUnlock()
g, ok := m.groups[name]
return g, ok
}
// FindConn 根据连接ID找到连接
func (m *ConnManager) FindConn(id int64) (*Connection, bool) {
2 years ago
m.RLock()
defer m.RUnlock()
e, ok := m.conns[id]
return e, ok
}
// FindConnByUID 根据连接绑定的UID找到连接
func (m *ConnManager) FindConnByUID(uid string) (*Connection, bool) {
2 years ago
m.RLock()
defer m.RUnlock()
for _, e := range m.conns {
if e.Session().UID() == uid {
return e, true
}
}
return nil, false
}
// PeekConn 循环所有连接
// fn 返回true跳过循环反之一直循环
func (m *ConnManager) PeekConn(fn func(id int64, c *Connection) bool) {
2 years ago
m.RLock()
defer m.RUnlock()
for id, c := range m.conns {
if fn(id, c) {
2 years ago
break
}
}
}