1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-08 11:17:44 -05:00
v2fly/proxy/testing/mocks/outboundhandler.go

56 lines
1.2 KiB
Go
Raw Normal View History

package mocks
import (
"io"
"sync"
2015-12-06 05:00:10 -05:00
"github.com/v2ray/v2ray-core/app"
2016-04-25 18:13:26 -04:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-01-29 08:39:55 -05:00
v2io "github.com/v2ray/v2ray-core/common/io"
v2net "github.com/v2ray/v2ray-core/common/net"
2016-01-02 17:32:18 -05:00
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport/ray"
)
type OutboundConnectionHandler struct {
Destination v2net.Destination
ConnInput io.Reader
ConnOutput io.Writer
}
2016-04-25 18:13:26 -04:00
func (this *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
input := ray.OutboundInput()
output := ray.OutboundOutput()
2016-04-25 18:13:26 -04:00
this.Destination = destination
this.ConnOutput.Write(payload.Value)
payload.Release()
2016-04-25 18:13:26 -04:00
writeFinish := &sync.Mutex{}
2016-04-25 18:13:26 -04:00
writeFinish.Lock()
2016-04-25 18:13:26 -04:00
go func() {
v2writer := v2io.NewAdaptiveWriter(this.ConnOutput)
defer v2writer.Release()
2016-04-25 18:13:26 -04:00
v2io.Pipe(input, v2writer)
writeFinish.Unlock()
input.Release()
}()
2016-04-25 18:13:26 -04:00
writeFinish.Lock()
v2reader := v2io.NewAdaptiveReader(this.ConnInput)
defer v2reader.Release()
v2io.Pipe(v2reader, output)
2016-04-18 12:44:10 -04:00
output.Close()
return nil
}
2016-06-03 18:38:22 -04:00
func (this *OutboundConnectionHandler) Create(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return this, nil
}