hide space implementations from interfaces

This commit is contained in:
Darien Raymond committed 2015-12-11 11:01:20 +00:00
1 parent 46ab9c45cc
commit dd81fc6f6a
23 files changed
+162 -119

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
package controller
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/internal"
)
// A SpaceController is supposed to be used by a shell to create Spaces. It should not be used
// directly by proxies.
type SpaceController struct {
packetDispatcher internal.PacketDispatcherWithContext
dnsCache internal.DnsCacheWithContext
}
func New() *SpaceController {
return new(SpaceController)
}
func (this *SpaceController) Bind(object interface{}) {
if packetDispatcher, ok := object.(internal.PacketDispatcherWithContext); ok {
this.packetDispatcher = packetDispatcher
}
if dnsCache, ok := object.(internal.DnsCacheWithContext); ok {
this.dnsCache = dnsCache
}
}
func (this *SpaceController) ForContext(tag string) app.Space {
return internal.NewSpace(tag, this.packetDispatcher, this.dnsCache)
}
+1 -18
View File
@@ -4,25 +4,8 @@ import (
"net"
)
// A DnsCache is an internal cache of DNS resolutions.
type DnsCache interface {
Get(domain string) net.IP
Add(domain string, ip net.IP)
}
type DnsCacheWithContext interface {
Get(context Context, domain string) net.IP
Add(contaxt Context, domain string, ip net.IP)
}
type contextedDnsCache struct {
context Context
dnsCache DnsCacheWithContext
}
func (this *contextedDnsCache) Get(domain string) net.IP {
return this.dnsCache.Get(this.context, domain)
}
func (this *contextedDnsCache) Add(domain string, ip net.IP) {
this.dnsCache.Add(this.context, domain, ip)
}
+9
View File
@@ -0,0 +1,9 @@
package internal
type contextImpl struct {
callerTag string
}
func (this *contextImpl) CallerTag() string {
return this.callerTag
}
+25
View File
@@ -0,0 +1,25 @@
package internal
import (
"net"
"github.com/v2ray/v2ray-core/app"
)
type DnsCacheWithContext interface {
Get(context app.Context, domain string) net.IP
Add(contaxt app.Context, domain string, ip net.IP)
}
type contextedDnsCache struct {
context app.Context
dnsCache DnsCacheWithContext
}
func (this *contextedDnsCache) Get(domain string) net.IP {
return this.dnsCache.Get(this.context, domain)
}
func (this *contextedDnsCache) Add(domain string, ip net.IP) {
this.dnsCache.Add(this.context, domain, ip)
}
+20
View File
@@ -0,0 +1,20 @@
package internal
import (
"github.com/v2ray/v2ray-core/app"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/ray"
)
type PacketDispatcherWithContext interface {
DispatchToOutbound(context app.Context, packet v2net.Packet) ray.InboundRay
}
type contextedPacketDispatcher struct {
context app.Context
packetDispatcher PacketDispatcherWithContext
}
func (this *contextedPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
return this.packetDispatcher.DispatchToOutbound(this.context, packet)
}
+45
View File
@@ -0,0 +1,45 @@
package internal
import (
"github.com/v2ray/v2ray-core/app"
)
type Space struct {
packetDispatcher PacketDispatcherWithContext
dnsCache DnsCacheWithContext
tag string
}
func NewSpace(tag string, packetDispatcher PacketDispatcherWithContext, dnsCache DnsCacheWithContext) *Space {
return &Space{
tag: tag,
packetDispatcher: packetDispatcher,
dnsCache: dnsCache,
}
}
func (this *Space) HasPacketDispatcher() bool {
return this.packetDispatcher != nil
}
func (this *Space) PacketDispatcher() app.PacketDispatcher {
return &contextedPacketDispatcher{
packetDispatcher: this.packetDispatcher,
context: &contextImpl{
callerTag: this.tag,
},
}
}
func (this *Space) HasDnsCache() bool {
return this.dnsCache != nil
}
func (this *Space) DnsCache() app.DnsCache {
return &contextedDnsCache{
dnsCache: this.dnsCache,
context: &contextImpl{
callerTag: this.tag,
},
}
}
-13
View File
@@ -9,16 +9,3 @@ import (
type PacketDispatcher interface {
DispatchToOutbound(packet v2net.Packet) ray.InboundRay
}
type PacketDispatcherWithContext interface {
DispatchToOutbound(context Context, packet v2net.Packet) ray.InboundRay
}
type contextedPacketDispatcher struct {
context Context
packetDispatcher PacketDispatcherWithContext
}
func (this *contextedPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
return this.packetDispatcher.DispatchToOutbound(this.context, packet)
}
+8 -66
View File
@@ -1,74 +1,16 @@
package app
// Context of a function call from proxy to app.
type Context interface {
CallerTag() string
}
type contextImpl struct {
callerTag string
}
// A Space contains all apps that may be available in a V2Ray runtime.
// Caller must check the availability of an app by calling HasXXX before getting its instance.
type Space interface {
HasPacketDispatcher() bool
PacketDispatcher() PacketDispatcher
func (this *contextImpl) CallerTag() string {
return this.callerTag
}
type SpaceController struct {
packetDispatcher PacketDispatcherWithContext
dnsCache DnsCacheWithContext
}
func NewSpaceController() *SpaceController {
return new(SpaceController)
}
func (this *SpaceController) Bind(object interface{}) {
if packetDispatcher, ok := object.(PacketDispatcherWithContext); ok {
this.packetDispatcher = packetDispatcher
}
if dnsCache, ok := object.(DnsCacheWithContext); ok {
this.dnsCache = dnsCache
}
}
func (this *SpaceController) ForContext(tag string) *Space {
return newSpace(this, &contextImpl{callerTag: tag})
}
type Space struct {
packetDispatcher PacketDispatcher
dnsCache DnsCache
}
func newSpace(controller *SpaceController, context Context) *Space {
space := new(Space)
if controller.packetDispatcher != nil {
space.packetDispatcher = &contextedPacketDispatcher{
context: context,
packetDispatcher: controller.packetDispatcher,
}
}
if controller.dnsCache != nil {
space.dnsCache = &contextedDnsCache{
context: context,
dnsCache: controller.dnsCache,
}
}
return space
}
func (this *Space) HasPacketDispatcher() bool {
return this.packetDispatcher != nil
}
func (this *Space) PacketDispatcher() PacketDispatcher {
return this.packetDispatcher
}
func (this *Space) HasDnsCache() bool {
return this.dnsCache != nil
}
func (this *Space) DnsCache() DnsCache {
return this.dnsCache
HasDnsCache() bool
DnsCache() DnsCache
}