dispatcher.go 919 B

12345678910111213141516171819202122232425262728293031323334
  1. package testing
  2. import (
  3. v2net "github.com/v2ray/v2ray-core/common/net"
  4. "github.com/v2ray/v2ray-core/transport/ray"
  5. )
  6. type TestPacketDispatcher struct {
  7. LastPacket chan v2net.Packet
  8. Handler func(packet v2net.Packet, traffic ray.OutboundRay)
  9. }
  10. func NewTestPacketDispatcher(handler func(packet v2net.Packet, traffic ray.OutboundRay)) *TestPacketDispatcher {
  11. if handler == nil {
  12. handler = func(packet v2net.Packet, traffic ray.OutboundRay) {
  13. for payload := range traffic.OutboundInput() {
  14. traffic.OutboundOutput() <- payload.Prepend([]byte("Processed: "))
  15. }
  16. close(traffic.OutboundOutput())
  17. }
  18. }
  19. return &TestPacketDispatcher{
  20. LastPacket: make(chan v2net.Packet, 16),
  21. Handler: handler,
  22. }
  23. }
  24. func (this *TestPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
  25. traffic := ray.NewRay()
  26. this.LastPacket <- packet
  27. go this.Handler(packet, traffic)
  28. return traffic
  29. }