1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-26 20:16:55 -05:00
v2fly/testing/mocks/outboundhandler.go

48 lines
1007 B
Go
Raw Normal View History

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