mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package peer
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type ConnectionListener struct {
|
|
OnConnected func(peer *Conn)
|
|
OnDisconnected func(peer *Conn)
|
|
}
|
|
|
|
type ConnectionDispatcher struct {
|
|
listeners map[*ConnectionListener]struct{}
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func NewConnectionDispatcher() *ConnectionDispatcher {
|
|
return &ConnectionDispatcher{
|
|
listeners: make(map[*ConnectionListener]struct{}),
|
|
}
|
|
}
|
|
|
|
func (e *ConnectionDispatcher) AddListener(listener *ConnectionListener) {
|
|
e.mu.Lock()
|
|
defer e.mu.Unlock()
|
|
e.listeners[listener] = struct{}{}
|
|
}
|
|
|
|
func (e *ConnectionDispatcher) RemoveListener(listener *ConnectionListener) {
|
|
e.mu.Lock()
|
|
defer e.mu.Unlock()
|
|
|
|
delete(e.listeners, listener)
|
|
}
|
|
|
|
func (e *ConnectionDispatcher) NotifyConnected(peer *Conn) {
|
|
e.mu.Lock()
|
|
defer e.mu.Unlock()
|
|
for listener, _ := range e.listeners {
|
|
listener.OnConnected(peer)
|
|
}
|
|
}
|
|
|
|
func (e *ConnectionDispatcher) NotifyDisconnected(peer *Conn) {
|
|
e.mu.Lock()
|
|
defer e.mu.Unlock()
|
|
for listener, _ := range e.listeners {
|
|
listener.OnDisconnected(peer)
|
|
}
|
|
}
|