inboundhandler.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. )
  8. type InboundConnectionHandler struct {
  9. port v2net.Port
  10. Space app.Space
  11. ConnInput io.Reader
  12. ConnOutput io.Writer
  13. }
  14. func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
  15. this.port = port
  16. return nil
  17. }
  18. func (this *InboundConnectionHandler) Port() v2net.Port {
  19. return this.port
  20. }
  21. func (this *InboundConnectionHandler) Close() {
  22. }
  23. func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
  24. ray := this.Space.PacketDispatcher().DispatchToOutbound(packet)
  25. input := ray.InboundInput()
  26. output := ray.InboundOutput()
  27. readFinish := &sync.Mutex{}
  28. writeFinish := &sync.Mutex{}
  29. readFinish.Lock()
  30. writeFinish.Lock()
  31. go func() {
  32. v2net.ReaderToChan(input, this.ConnInput)
  33. close(input)
  34. readFinish.Unlock()
  35. }()
  36. go func() {
  37. v2net.ChanToWriter(this.ConnOutput, output)
  38. writeFinish.Unlock()
  39. }()
  40. readFinish.Lock()
  41. writeFinish.Lock()
  42. return nil
  43. }