1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 17:27:50 -04:00
v2fly/proxy/testing/mocks/inboundhandler.go
2015-12-11 11:01:20 +00:00

56 lines
1.1 KiB
Go

package mocks
import (
"io"
"sync"
"github.com/v2ray/v2ray-core/app"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
)
type InboundConnectionHandler struct {
Port v2net.Port
space app.Space
ConnInput io.Reader
ConnOutput io.Writer
}
func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
this.Port = port
return nil
}
func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
input := ray.InboundInput()
output := ray.InboundOutput()
readFinish := &sync.Mutex{}
writeFinish := &sync.Mutex{}
readFinish.Lock()
writeFinish.Lock()
go func() {
v2net.ReaderToChan(input, this.ConnInput)
close(input)
readFinish.Unlock()
}()
go func() {
v2net.ChanToWriter(this.ConnOutput, output)
writeFinish.Unlock()
}()
readFinish.Lock()
writeFinish.Lock()
return nil
}
func (this *InboundConnectionHandler) Create(space app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) {
this.space = space
return this, nil
}