1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 02:38:10 -05:00
v2fly/commander.go
2018-01-24 15:05:46 +01:00

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
}