outboundhandler.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package mocks
  2. import (
  3. "io"
  4. "sync"
  5. "v2ray.com/core/app"
  6. "v2ray.com/core/common/alloc"
  7. v2io "v2ray.com/core/common/io"
  8. v2net "v2ray.com/core/common/net"
  9. "v2ray.com/core/proxy"
  10. "v2ray.com/core/transport/ray"
  11. )
  12. type OutboundConnectionHandler struct {
  13. Destination v2net.Destination
  14. ConnInput io.Reader
  15. ConnOutput io.Writer
  16. }
  17. func (this *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  18. input := ray.OutboundInput()
  19. output := ray.OutboundOutput()
  20. this.Destination = destination
  21. if !payload.IsEmpty() {
  22. this.ConnOutput.Write(payload.Value)
  23. }
  24. payload.Release()
  25. writeFinish := &sync.Mutex{}
  26. writeFinish.Lock()
  27. go func() {
  28. v2writer := v2io.NewAdaptiveWriter(this.ConnOutput)
  29. defer v2writer.Release()
  30. v2io.Pipe(input, v2writer)
  31. writeFinish.Unlock()
  32. input.Release()
  33. }()
  34. writeFinish.Lock()
  35. v2reader := v2io.NewAdaptiveReader(this.ConnInput)
  36. defer v2reader.Release()
  37. v2io.Pipe(v2reader, output)
  38. output.Close()
  39. return nil
  40. }
  41. func (this *OutboundConnectionHandler) Create(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
  42. return this, nil
  43. }