outboundhandler.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package mocks
  2. import (
  3. "io"
  4. "sync"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  7. "github.com/v2ray/v2ray-core/transport/ray"
  8. )
  9. type OutboundConnectionHandler struct {
  10. Destination v2net.Destination
  11. ConnInput io.Reader
  12. ConnOutput io.Writer
  13. }
  14. func (this *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray ray.OutboundRay) error {
  15. input := ray.OutboundInput()
  16. output := ray.OutboundOutput()
  17. this.Destination = packet.Destination()
  18. if packet.Chunk() != nil {
  19. this.ConnOutput.Write(packet.Chunk().Value)
  20. packet.Chunk().Release()
  21. }
  22. if packet.MoreChunks() {
  23. writeFinish := &sync.Mutex{}
  24. writeFinish.Lock()
  25. go func() {
  26. v2net.ChanToWriter(this.ConnOutput, input)
  27. writeFinish.Unlock()
  28. }()
  29. writeFinish.Lock()
  30. }
  31. v2net.ReaderToChan(output, this.ConnInput)
  32. close(output)
  33. return nil
  34. }
  35. func (this *OutboundConnectionHandler) Create(config interface{}) (connhandler.OutboundConnectionHandler, error) {
  36. return this, nil
  37. }