outboundhandler.go 1.1 KB

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