1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-23 20:20:21 -04:00
v2fly/proxy/testing/mocks/inboundhandler.go

66 lines
1.3 KiB
Go
Raw Normal View History

package mocks
import (
"io"
"sync"
2016-01-31 11:01:28 -05:00
"github.com/v2ray/v2ray-core/app/dispatcher"
2016-01-29 08:39:55 -05:00
v2io "github.com/v2ray/v2ray-core/common/io"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type InboundConnectionHandler struct {
2016-01-31 11:01:28 -05:00
port v2net.Port
PacketDispatcher dispatcher.PacketDispatcher
ConnInput io.Reader
ConnOutput io.Writer
}
2015-12-02 15:44:01 -05:00
func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
2016-01-19 17:41:40 -05:00
this.port = port
return nil
}
2016-01-19 17:41:40 -05:00
func (this *InboundConnectionHandler) Port() v2net.Port {
return this.port
}
func (this *InboundConnectionHandler) Close() {
}
func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
2016-01-31 11:01:28 -05:00
ray := this.PacketDispatcher.DispatchToOutbound(packet)
input := ray.InboundInput()
output := ray.InboundOutput()
readFinish := &sync.Mutex{}
writeFinish := &sync.Mutex{}
readFinish.Lock()
writeFinish.Lock()
go func() {
v2reader := v2io.NewAdaptiveReader(this.ConnInput)
defer v2reader.Release()
v2io.Pipe(v2reader, input)
2016-04-18 12:44:10 -04:00
input.Close()
readFinish.Unlock()
}()
go func() {
v2writer := v2io.NewAdaptiveWriter(this.ConnOutput)
defer v2writer.Release()
v2io.Pipe(output, v2writer)
2016-04-18 12:44:10 -04:00
output.Release()
writeFinish.Unlock()
}()
readFinish.Lock()
writeFinish.Lock()
return nil
}