inboundhandler.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package mocks
  2. import (
  3. "io"
  4. "sync"
  5. "github.com/v2ray/v2ray-core/app/dispatcher"
  6. v2io "github.com/v2ray/v2ray-core/common/io"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. )
  9. type InboundConnectionHandler struct {
  10. port v2net.Port
  11. PacketDispatcher dispatcher.PacketDispatcher
  12. ConnInput io.Reader
  13. ConnOutput io.Writer
  14. }
  15. func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
  16. this.port = port
  17. return nil
  18. }
  19. func (this *InboundConnectionHandler) Port() v2net.Port {
  20. return this.port
  21. }
  22. func (this *InboundConnectionHandler) Close() {
  23. }
  24. func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
  25. ray := this.PacketDispatcher.DispatchToOutbound(packet)
  26. input := ray.InboundInput()
  27. output := ray.InboundOutput()
  28. readFinish := &sync.Mutex{}
  29. writeFinish := &sync.Mutex{}
  30. readFinish.Lock()
  31. writeFinish.Lock()
  32. go func() {
  33. v2io.RawReaderToChan(input, this.ConnInput)
  34. close(input)
  35. readFinish.Unlock()
  36. }()
  37. go func() {
  38. v2io.ChanToWriter(this.ConnOutput, output)
  39. writeFinish.Unlock()
  40. }()
  41. readFinish.Lock()
  42. writeFinish.Lock()
  43. return nil
  44. }