1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-16 04:35:24 +00:00
v2fly/app/dispatcher/testing/dispatcher.go

40 lines
1.0 KiB
Go
Raw Normal View History

2016-02-04 12:13:15 +00:00
package testing
import (
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
2016-02-04 12:13:15 +00:00
)
type TestPacketDispatcher struct {
2016-04-18 16:44:10 +00:00
Destination chan v2net.Destination
2016-04-25 22:13:26 +00:00
Handler func(destination v2net.Destination, traffic ray.OutboundRay)
2016-02-04 12:13:15 +00:00
}
2016-04-25 22:13:26 +00:00
func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic ray.OutboundRay)) *TestPacketDispatcher {
2016-02-04 12:27:35 +00:00
if handler == nil {
2016-04-25 22:13:26 +00:00
handler = func(destination v2net.Destination, 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
}
}
2016-08-14 15:08:01 +00:00
func (this *TestPacketDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay {
2016-02-04 12:27:35 +00:00
traffic := ray.NewRay()
2016-08-14 15:08:01 +00:00
this.Destination <- session.Destination
go this.Handler(session.Destination, traffic)
2016-02-04 12:13:15 +00:00
return traffic
}