mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
commander interface
This commit is contained in:
parent
9cd0e90e99
commit
8923159aee
63
commander.go
Normal file
63
commander.go
Normal file
@ -0,0 +1,63 @@
|
||||
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
|
||||
}
|
10
v2ray.go
10
v2ray.go
@ -28,6 +28,7 @@ type Instance struct {
|
||||
ihm syncInboundHandlerManager
|
||||
ohm syncOutboundHandlerManager
|
||||
clock syncClock
|
||||
cmd syncCommander
|
||||
|
||||
features []Feature
|
||||
id uuid.UUID
|
||||
@ -130,6 +131,10 @@ func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error
|
||||
s.ihm.Set(instance.(InboundHandlerManager))
|
||||
case OutboundHandlerManager, *OutboundHandlerManager:
|
||||
s.ohm.Set(instance.(OutboundHandlerManager))
|
||||
case Clock, *Clock:
|
||||
s.clock.Set(instance.(Clock))
|
||||
case Commander, *Commander:
|
||||
s.cmd.Set(instance.(Commander))
|
||||
}
|
||||
s.features = append(s.features, instance)
|
||||
return nil
|
||||
@ -169,3 +174,8 @@ func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
|
||||
func (s *Instance) Clock() Clock {
|
||||
return &(s.clock)
|
||||
}
|
||||
|
||||
// Commander returns the Commander used by this Instance. The returned Commander is always functional.
|
||||
func (s *Instance) Commander() Commander {
|
||||
return &(s.cmd)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user