mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-06 18:28:00 -05:00
64 lines
1022 B
Go
64 lines
1022 B
Go
|
package core
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"google.golang.org/grpc"
|
||
|
)
|
||
|
|
||
|
// ServiceRegistryCallback is a callback function for registering services.
|
||
|
type ServiceRegistryCallback func(s *grpc.Server)
|
||
|
|
||
|
// Commander is a feature that accepts commands from external source.
|
||
|
type Commander interface {
|
||
|
Feature
|
||
|
|
||
|
// RegisterService registers a service into this Commander.
|
||
|
RegisterService(ServiceRegistryCallback)
|
||
|
}
|
||
|
|
||
|
type syncCommander struct {
|
||
|
sync.RWMutex
|
||
|
Commander
|
||
|
}
|
||
|
|
||
|
func (c *syncCommander) RegisterService(callback ServiceRegistryCallback) {
|
||
|
c.RLock()
|
||
|
defer c.RUnlock()
|
||
|
|
||
|
if c.Commander == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.Commander.RegisterService(callback)
|
||
|
}
|
||
|
|
||
|
func (c *syncCommander) Start() error {
|
||
|
c.RLock()
|
||
|
defer c.RUnlock()
|
||
|
|
||
|
if c.Commander == nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return c.Commander.Start()
|
||
|
}
|
||
|
|
||
|
func (c *syncCommander) Close() {
|
||
|
c.RLock()
|
||
|
defer c.RUnlock()
|
||
|
|
||
|
if c.Commander == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.Commander.Close()
|
||
|
}
|
||
|
|
||
|
func (c *syncCommander) Set(commander Commander) {
|
||
|
c.Lock()
|
||
|
defer c.Unlock()
|
||
|
|
||
|
c.Commander = commander
|
||
|
}
|