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-06-03 18:38:22 -04:00
ListeningPort v2net.Port
ListeningAddress v2net.Address
2016-01-31 11:01:28 -05:00
PacketDispatcher dispatcher.PacketDispatcher
ConnInput io.Reader
ConnOutput io.Writer
}
2016-06-03 18:38:22 -04:00
func (this *InboundConnectionHandler) Start() error {
return nil
}
2016-01-19 17:41:40 -05:00
func (this *InboundConnectionHandler) Port() v2net.Port {
2016-06-03 18:38:22 -04:00
return this.ListeningPort
2016-01-19 17:41:40 -05:00
}
func (this *InboundConnectionHandler) Close() {
}
2016-04-25 18:13:26 -04:00
func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
ray := this.PacketDispatcher.DispatchToOutbound(destination)
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
}