1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 09:26:21 -05:00
v2fly/network.go

144 lines
2.6 KiB
Go
Raw Normal View History

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