mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
add memberMu.
This commit is contained in:
parent
f11c1c5e54
commit
0d419787bc
@ -14,27 +14,32 @@ import (
|
|||||||
|
|
||||||
func New(capture types.CaptureManager, config *config.Session) *SessionManagerCtx {
|
func New(capture types.CaptureManager, config *config.Session) *SessionManagerCtx {
|
||||||
return &SessionManagerCtx{
|
return &SessionManagerCtx{
|
||||||
logger: log.With().Str("module", "session").Logger(),
|
logger: log.With().Str("module", "session").Logger(),
|
||||||
host: nil,
|
host: nil,
|
||||||
hostMu: sync.Mutex{},
|
hostMu: sync.Mutex{},
|
||||||
capture: capture,
|
capture: capture,
|
||||||
config: config,
|
config: config,
|
||||||
members: make(map[string]*SessionCtx),
|
members: make(map[string]*SessionCtx),
|
||||||
emmiter: events.New(),
|
membersMu: sync.Mutex{},
|
||||||
|
emmiter: events.New(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionManagerCtx struct {
|
type SessionManagerCtx struct {
|
||||||
logger zerolog.Logger
|
logger zerolog.Logger
|
||||||
host types.Session
|
host types.Session
|
||||||
hostMu sync.Mutex
|
hostMu sync.Mutex
|
||||||
capture types.CaptureManager
|
capture types.CaptureManager
|
||||||
config *config.Session
|
config *config.Session
|
||||||
members map[string]*SessionCtx
|
members map[string]*SessionCtx
|
||||||
emmiter events.EventEmmiter
|
membersMu sync.Mutex
|
||||||
|
emmiter events.EventEmmiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) New(id string, admin bool) types.Session {
|
func (manager *SessionManagerCtx) New(id string, admin bool) types.Session {
|
||||||
|
manager.membersMu.Lock()
|
||||||
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
session := &SessionCtx{
|
session := &SessionCtx{
|
||||||
id: id,
|
id: id,
|
||||||
admin: admin,
|
admin: admin,
|
||||||
@ -48,16 +53,17 @@ func (manager *SessionManagerCtx) New(id string, admin bool) types.Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
||||||
|
manager.membersMu.Lock()
|
||||||
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
session, ok := manager.members[id]
|
session, ok := manager.members[id]
|
||||||
return session, ok
|
return session, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Has(id string) bool {
|
|
||||||
_, ok := manager.members[id]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Destroy(id string) error {
|
func (manager *SessionManagerCtx) Destroy(id string) error {
|
||||||
|
manager.membersMu.Lock()
|
||||||
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
session, ok := manager.members[id]
|
session, ok := manager.members[id]
|
||||||
if ok {
|
if ok {
|
||||||
delete(manager.members, id)
|
delete(manager.members, id)
|
||||||
@ -105,6 +111,9 @@ func (manager *SessionManagerCtx) ClearHost() {
|
|||||||
// members list
|
// members list
|
||||||
// ---
|
// ---
|
||||||
func (manager *SessionManagerCtx) Admins() []types.Session {
|
func (manager *SessionManagerCtx) Admins() []types.Session {
|
||||||
|
manager.membersMu.Lock()
|
||||||
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
var sessions []types.Session
|
var sessions []types.Session
|
||||||
for _, session := range manager.members {
|
for _, session := range manager.members {
|
||||||
if !session.connected || !session.admin {
|
if !session.connected || !session.admin {
|
||||||
@ -118,6 +127,9 @@ func (manager *SessionManagerCtx) Admins() []types.Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Members() []types.Session {
|
func (manager *SessionManagerCtx) Members() []types.Session {
|
||||||
|
manager.membersMu.Lock()
|
||||||
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
var sessions []types.Session
|
var sessions []types.Session
|
||||||
for _, session := range manager.members {
|
for _, session := range manager.members {
|
||||||
if !session.connected {
|
if !session.connected {
|
||||||
@ -131,6 +143,9 @@ func (manager *SessionManagerCtx) Members() []types.Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) error {
|
func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) error {
|
||||||
|
manager.membersMu.Lock()
|
||||||
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
for id, session := range manager.members {
|
for id, session := range manager.members {
|
||||||
if !session.connected {
|
if !session.connected {
|
||||||
continue
|
continue
|
||||||
@ -146,6 +161,7 @@ func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{})
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +23,6 @@ type Session interface {
|
|||||||
type SessionManager interface {
|
type SessionManager interface {
|
||||||
New(id string, admin bool) Session
|
New(id string, admin bool) Session
|
||||||
Get(id string) (Session, bool)
|
Get(id string) (Session, bool)
|
||||||
Has(id string) bool
|
|
||||||
Destroy(id string) error
|
Destroy(id string) error
|
||||||
|
|
||||||
HasHost() bool
|
HasHost() bool
|
||||||
|
Loading…
Reference in New Issue
Block a user