v2ray-core/testing/mocks/outboundhandler.go

49 lines
1.0 KiB
Go
Raw Normal View History

2015-09-19 15:35:20 +02:00
package mocks
import (
"bytes"
"github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
2015-10-14 14:51:19 +02:00
"github.com/v2ray/v2ray-core/transport/ray"
2015-09-19 15:35:20 +02:00
)
type OutboundConnectionHandler struct {
Data2Send *bytes.Buffer
Data2Return []byte
2015-09-20 18:22:29 +02:00
Destination v2net.Destination
2015-09-19 15:35:20 +02:00
}
2015-10-14 14:51:19 +02:00
func (handler *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray ray.OutboundRay) error {
2015-09-19 15:35:20 +02:00
input := ray.OutboundInput()
output := ray.OutboundOutput()
2015-10-07 00:30:44 +02:00
handler.Destination = packet.Destination()
if packet.Chunk() != nil {
handler.Data2Send.Write(packet.Chunk().Value)
2015-10-07 00:30:44 +02:00
}
2015-09-19 15:35:20 +02:00
go func() {
for {
data, open := <-input
if !open {
break
}
handler.Data2Send.Write(data.Value)
data.Release()
2015-09-19 15:35:20 +02:00
}
response := alloc.NewBuffer()
response.Clear()
response.Append(handler.Data2Return)
output <- response
2015-09-19 15:35:20 +02:00
close(output)
}()
return nil
}
func (handler *OutboundConnectionHandler) Create(config interface{}) (connhandler.OutboundConnectionHandler, error) {
2015-09-19 15:35:20 +02:00
return handler, nil
}