You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
770 B
42 lines
770 B
2 years ago
|
package p2p
|
||
|
|
||
|
import "sync"
|
||
|
|
||
|
type ConnectCallbacks struct {
|
||
|
cbs []ConnectCallback
|
||
|
mu sync.RWMutex
|
||
|
}
|
||
|
|
||
|
func (a *ConnectCallbacks) Add(cb ConnectCallback) {
|
||
|
a.mu.Lock()
|
||
|
defer a.mu.Unlock()
|
||
|
a.cbs = append(a.cbs, cb)
|
||
|
}
|
||
|
|
||
|
func (a *ConnectCallbacks) GetAll() []ConnectCallback {
|
||
|
a.mu.RLock()
|
||
|
defer a.mu.RUnlock()
|
||
|
out := make([]ConnectCallback, len(a.cbs))
|
||
|
copy(out, a.cbs)
|
||
|
return out
|
||
|
}
|
||
|
|
||
|
type DisconnectCallbacks struct {
|
||
|
cbs []DisconnectCallback
|
||
|
mu sync.RWMutex
|
||
|
}
|
||
|
|
||
|
func (a *DisconnectCallbacks) Add(cb DisconnectCallback) {
|
||
|
a.mu.Lock()
|
||
|
defer a.mu.Unlock()
|
||
|
a.cbs = append(a.cbs, cb)
|
||
|
}
|
||
|
|
||
|
func (a *DisconnectCallbacks) GetAll() []DisconnectCallback {
|
||
|
a.mu.RLock()
|
||
|
defer a.mu.RUnlock()
|
||
|
out := make([]DisconnectCallback, len(a.cbs))
|
||
|
copy(out, a.cbs)
|
||
|
return out
|
||
|
}
|