1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-25 17:05:23 +00:00
v2fly/app/dispatcher/testing/dispatcher.go

35 lines
919 B
Go
Raw Normal View History

2016-02-04 12:13:15 +00:00
package testing
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/ray"
)
type TestPacketDispatcher struct {
2016-02-04 12:27:35 +00:00
LastPacket chan v2net.Packet
2016-02-04 12:13:15 +00:00
Handler func(packet v2net.Packet, traffic ray.OutboundRay)
}
2016-02-04 12:27:35 +00:00
func NewTestPacketDispatcher(handler func(packet v2net.Packet, traffic ray.OutboundRay)) *TestPacketDispatcher {
if handler == nil {
handler = func(packet v2net.Packet, traffic ray.OutboundRay) {
2016-02-04 12:13:15 +00:00
for payload := range traffic.OutboundInput() {
traffic.OutboundOutput() <- payload.Prepend([]byte("Processed: "))
}
close(traffic.OutboundOutput())
2016-02-04 12:27:35 +00:00
}
2016-02-04 12:13:15 +00:00
}
2016-02-04 12:27:35 +00:00
return &TestPacketDispatcher{
LastPacket: make(chan v2net.Packet, 16),
Handler: handler,
}
}
func (this *TestPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
traffic := ray.NewRay()
this.LastPacket <- packet
go this.Handler(packet, traffic)
2016-02-04 12:13:15 +00:00
return traffic
}