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

39 lines
996 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-04-18 16:44:10 +00:00
Destination chan v2net.Destination
Handler func(packet v2net.Packet, traffic ray.OutboundRay)
2016-02-04 12:13:15 +00:00
}
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-04-18 16:44:10 +00:00
for {
payload, err := traffic.OutboundInput().Read()
if err != nil {
break
}
traffic.OutboundOutput().Write(payload.Prepend([]byte("Processed: ")))
2016-02-04 12:13:15 +00:00
}
2016-04-18 16:44:10 +00:00
traffic.OutboundOutput().Close()
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{
2016-04-18 16:44:10 +00:00
Destination: make(chan v2net.Destination),
Handler: handler,
2016-02-04 12:27:35 +00:00
}
}
func (this *TestPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
traffic := ray.NewRay()
2016-04-18 16:44:10 +00:00
this.Destination <- packet.Destination()
2016-02-04 12:27:35 +00:00
go this.Handler(packet, traffic)
2016-02-04 12:13:15 +00:00
return traffic
}