1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-01 00:06:11 -04:00
v2fly/testing/mocks/outboundhandler.go

45 lines
934 B
Go
Raw Normal View History

2015-09-19 09:35:20 -04:00
package mocks
import (
"bytes"
"github.com/v2ray/v2ray-core"
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())
}
2015-09-19 09:35:20 -04:00
go func() {
for {
data, open := <-input
if !open {
break
}
handler.Data2Send.Write(data)
}
2015-10-03 05:34:01 -04:00
dataCopy := make([]byte, len(handler.Data2Return))
copy(dataCopy, handler.Data2Return)
output <- dataCopy
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
}