mirror of https://github.com/v2ray/v2ray-core
				
				
				
			remove commander from core feature
							parent
							
								
									c25a76a0cf
								
							
						
					
					
						commit
						f0849543ed
					
				|  | @ -31,12 +31,16 @@ func NewCommander(ctx context.Context, config *Config) (*Commander, error) { | ||||||
| 		ohm:    v.OutboundHandlerManager(), | 		ohm:    v.OutboundHandlerManager(), | ||||||
| 		v:      v, | 		v:      v, | ||||||
| 	} | 	} | ||||||
| 	if err := v.RegisterFeature((*core.Commander)(nil), c); err != nil { | 	if err := v.RegisterFeature((*Commander)(nil), c); err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	return c, nil | 	return c, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (c *Commander) Type() interface{} { | ||||||
|  | 	return (*Commander)(nil) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func (c *Commander) Start() error { | func (c *Commander) Start() error { | ||||||
| 	c.Lock() | 	c.Lock() | ||||||
| 	c.server = grpc.NewServer() | 	c.server = grpc.NewServer() | ||||||
|  |  | ||||||
							
								
								
									
										44
									
								
								commander.go
								
								
								
								
							
							
						
						
									
										44
									
								
								commander.go
								
								
								
								
							|  | @ -1,44 +0,0 @@ | ||||||
| package core |  | ||||||
| 
 |  | ||||||
| import ( |  | ||||||
| 	"sync" |  | ||||||
| ) |  | ||||||
| 
 |  | ||||||
| // Commander is a feature that accepts commands from external source.
 |  | ||||||
| type Commander interface { |  | ||||||
| 	Feature |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| type syncCommander struct { |  | ||||||
| 	sync.RWMutex |  | ||||||
| 	Commander |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func (c *syncCommander) Start() error { |  | ||||||
| 	c.RLock() |  | ||||||
| 	defer c.RUnlock() |  | ||||||
| 
 |  | ||||||
| 	if c.Commander == nil { |  | ||||||
| 		return nil |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return c.Commander.Start() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func (c *syncCommander) Close() error { |  | ||||||
| 	c.RLock() |  | ||||||
| 	defer c.RUnlock() |  | ||||||
| 
 |  | ||||||
| 	if c.Commander == nil { |  | ||||||
| 		return nil |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return c.Commander.Close() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func (c *syncCommander) Set(commander Commander) { |  | ||||||
| 	c.Lock() |  | ||||||
| 	defer c.Unlock() |  | ||||||
| 
 |  | ||||||
| 	c.Commander = commander |  | ||||||
| } |  | ||||||
							
								
								
									
										28
									
								
								v2ray.go
								
								
								
								
							
							
						
						
									
										28
									
								
								v2ray.go
								
								
								
								
							|  | @ -28,7 +28,6 @@ type Instance struct { | ||||||
| 	router        syncRouter | 	router        syncRouter | ||||||
| 	ihm           syncInboundHandlerManager | 	ihm           syncInboundHandlerManager | ||||||
| 	ohm           syncOutboundHandlerManager | 	ohm           syncOutboundHandlerManager | ||||||
| 	cmd           syncCommander |  | ||||||
| 
 | 
 | ||||||
| 	access   sync.Mutex | 	access   sync.Mutex | ||||||
| 	features []Feature | 	features []Feature | ||||||
|  | @ -105,7 +104,7 @@ func (s *Instance) Close() error { | ||||||
| 	defer s.access.Unlock() | 	defer s.access.Unlock() | ||||||
| 
 | 
 | ||||||
| 	s.running = false | 	s.running = false | ||||||
| 	for _, f := range s.features { | 	for _, f := range s.allFeatures() { | ||||||
| 		f.Close() | 		f.Close() | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -119,7 +118,7 @@ func (s *Instance) Start() error { | ||||||
| 	defer s.access.Unlock() | 	defer s.access.Unlock() | ||||||
| 
 | 
 | ||||||
| 	s.running = true | 	s.running = true | ||||||
| 	for _, f := range s.features { | 	for _, f := range s.allFeatures() { | ||||||
| 		if err := f.Start(); err != nil { | 		if err := f.Start(); err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
|  | @ -134,6 +133,8 @@ func (s *Instance) Start() error { | ||||||
| // If feature is one of the following types, the corressponding feature in this Instance
 | // If feature is one of the following types, the corressponding feature in this Instance
 | ||||||
| // will be replaced: DNSClient, PolicyManager, Router, Dispatcher, InboundHandlerManager, OutboundHandlerManager.
 | // will be replaced: DNSClient, PolicyManager, Router, Dispatcher, InboundHandlerManager, OutboundHandlerManager.
 | ||||||
| func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error { | func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error { | ||||||
|  | 	running := false | ||||||
|  | 
 | ||||||
| 	switch feature.(type) { | 	switch feature.(type) { | ||||||
| 	case DNSClient, *DNSClient: | 	case DNSClient, *DNSClient: | ||||||
| 		s.dnsClient.Set(instance.(DNSClient)) | 		s.dnsClient.Set(instance.(DNSClient)) | ||||||
|  | @ -147,19 +148,23 @@ func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error | ||||||
| 		s.ihm.Set(instance.(InboundHandlerManager)) | 		s.ihm.Set(instance.(InboundHandlerManager)) | ||||||
| 	case OutboundHandlerManager, *OutboundHandlerManager: | 	case OutboundHandlerManager, *OutboundHandlerManager: | ||||||
| 		s.ohm.Set(instance.(OutboundHandlerManager)) | 		s.ohm.Set(instance.(OutboundHandlerManager)) | ||||||
| 	case Commander, *Commander: | 	default: | ||||||
| 		s.cmd.Set(instance.(Commander)) | 		s.access.Lock() | ||||||
|  | 		s.features = append(s.features, instance) | ||||||
|  | 		running = s.running | ||||||
|  | 		s.access.Unlock() | ||||||
| 	} | 	} | ||||||
| 	s.access.Lock() |  | ||||||
| 	defer s.access.Unlock() |  | ||||||
| 
 | 
 | ||||||
| 	s.features = append(s.features, instance) | 	if running { | ||||||
| 	if s.running { |  | ||||||
| 		return instance.Start() | 		return instance.Start() | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (s *Instance) allFeatures() []Feature { | ||||||
|  | 	return append([]Feature{s.DNSClient(), s.PolicyManager(), s.Dispatcher(), s.Router(), s.InboundHandlerManager(), s.OutboundHandlerManager()}, s.features...) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // GetFeature returns a feature that was registered in this Instance. Nil if not found.
 | // GetFeature returns a feature that was registered in this Instance. Nil if not found.
 | ||||||
| // The returned Feature must implement common.HasType and whose type equals the given feature type.
 | // The returned Feature must implement common.HasType and whose type equals the given feature type.
 | ||||||
| func (s *Instance) GetFeature(featureType interface{}) Feature { | func (s *Instance) GetFeature(featureType interface{}) Feature { | ||||||
|  | @ -202,8 +207,3 @@ func (s *Instance) InboundHandlerManager() InboundHandlerManager { | ||||||
| func (s *Instance) OutboundHandlerManager() OutboundHandlerManager { | func (s *Instance) OutboundHandlerManager() OutboundHandlerManager { | ||||||
| 	return &(s.ohm) | 	return &(s.ohm) | ||||||
| } | } | ||||||
| 
 |  | ||||||
| // 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
	
	 Darien Raymond
						Darien Raymond