2018-01-10 06:22:37 -05:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
2018-10-11 15:14:53 -04:00
|
|
|
"v2ray.com/core/features/inbound"
|
2018-10-11 14:43:37 -04:00
|
|
|
"v2ray.com/core/features/outbound"
|
2018-01-10 06:22:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type syncInboundHandlerManager struct {
|
|
|
|
sync.RWMutex
|
2018-10-11 15:14:53 -04:00
|
|
|
inbound.Manager
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
func (*syncInboundHandlerManager) Type() interface{} {
|
|
|
|
return inbound.ManagerType()
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
func (m *syncInboundHandlerManager) GetHandler(ctx context.Context, tag string) (inbound.Handler, error) {
|
2018-01-10 06:22:37 -05:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
if m.Manager == nil {
|
|
|
|
return nil, newError("inbound.Manager not set.").AtError()
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
return m.Manager.GetHandler(ctx, tag)
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
func (m *syncInboundHandlerManager) AddHandler(ctx context.Context, handler inbound.Handler) error {
|
2018-01-10 06:22:37 -05:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
if m.Manager == nil {
|
|
|
|
return newError("inbound.Manager not set.").AtError()
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
return m.Manager.AddHandler(ctx, handler)
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *syncInboundHandlerManager) Start() error {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
if m.Manager == nil {
|
|
|
|
return newError("inbound.Manager not set.").AtError()
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
return m.Manager.Start()
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (m *syncInboundHandlerManager) Close() error {
|
2018-01-10 06:22:37 -05:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
return common.Close(m.Manager)
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
func (m *syncInboundHandlerManager) Set(manager inbound.Manager) {
|
2018-02-20 15:22:41 -05:00
|
|
|
if manager == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
common.Close(m.Manager) // nolint: errcheck
|
|
|
|
m.Manager = manager
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type syncOutboundHandlerManager struct {
|
|
|
|
sync.RWMutex
|
2018-10-12 17:57:56 -04:00
|
|
|
outbound.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*syncOutboundHandlerManager) Type() interface{} {
|
|
|
|
return outbound.ManagerType()
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 14:43:37 -04:00
|
|
|
func (m *syncOutboundHandlerManager) GetHandler(tag string) outbound.Handler {
|
2018-01-10 06:22:37 -05:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
if m.Manager == nil {
|
2018-01-10 06:22:37 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
return m.Manager.GetHandler(tag)
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 14:43:37 -04:00
|
|
|
func (m *syncOutboundHandlerManager) GetDefaultHandler() outbound.Handler {
|
2018-01-10 06:22:37 -05:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
if m.Manager == nil {
|
2018-01-10 06:22:37 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
return m.Manager.GetDefaultHandler()
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 14:43:37 -04:00
|
|
|
func (m *syncOutboundHandlerManager) AddHandler(ctx context.Context, handler outbound.Handler) error {
|
2018-01-10 06:22:37 -05:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
if m.Manager == nil {
|
2018-01-10 06:22:37 -05:00
|
|
|
return newError("OutboundHandlerManager not set.").AtError()
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
return m.Manager.AddHandler(ctx, handler)
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *syncOutboundHandlerManager) Start() error {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
if m.Manager == nil {
|
2018-01-10 06:22:37 -05:00
|
|
|
return newError("OutboundHandlerManager not set.").AtError()
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
return m.Manager.Start()
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (m *syncOutboundHandlerManager) Close() error {
|
2018-01-10 06:22:37 -05:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
return common.Close(m.Manager)
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
func (m *syncOutboundHandlerManager) Set(manager outbound.Manager) {
|
2018-02-20 15:22:41 -05:00
|
|
|
if manager == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
common.Close(m.Manager) // nolint: errcheck
|
|
|
|
m.Manager = manager
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|