dispatcher.go 996 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. Destination chan v2net.Destination
  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 {
  14. payload, err := traffic.OutboundInput().Read()
  15. if err != nil {
  16. break
  17. }
  18. traffic.OutboundOutput().Write(payload.Prepend([]byte("Processed: ")))
  19. }
  20. traffic.OutboundOutput().Close()
  21. }
  22. }
  23. return &TestPacketDispatcher{
  24. Destination: make(chan v2net.Destination),
  25. Handler: handler,
  26. }
  27. }
  28. func (this *TestPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
  29. traffic := ray.NewRay()
  30. this.Destination <- packet.Destination()
  31. go this.Handler(packet, traffic)
  32. return traffic
  33. }