1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 11:51:02 -05:00
v2fly/proxy/testing/mocks/inboundhandler.go

59 lines
1.1 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() {
2016-01-29 08:39:55 -05:00
v2io.RawReaderToChan(input, this.ConnInput)
close(input)
readFinish.Unlock()
}()
go func() {
2016-02-01 06:22:29 -05:00
v2io.ChanToRawWriter(this.ConnOutput, output)
writeFinish.Unlock()
}()
readFinish.Lock()
writeFinish.Lock()
return nil
}