1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-12 23:18:36 -04:00
v2fly/app/dispatcher/testing/dispatcher.go

39 lines
1.0 KiB
Go
Raw Normal View History

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