inboundhandler.go 906 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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) Communicate(packet v2net.Packet) error {
  19. ray := this.Space.PacketDispatcher().DispatchToOutbound(packet)
  20. input := ray.InboundInput()
  21. output := ray.InboundOutput()
  22. readFinish := &sync.Mutex{}
  23. writeFinish := &sync.Mutex{}
  24. readFinish.Lock()
  25. writeFinish.Lock()
  26. go func() {
  27. v2net.ReaderToChan(input, this.ConnInput)
  28. close(input)
  29. readFinish.Unlock()
  30. }()
  31. go func() {
  32. v2net.ChanToWriter(this.ConnOutput, output)
  33. writeFinish.Unlock()
  34. }()
  35. readFinish.Lock()
  36. writeFinish.Lock()
  37. return nil
  38. }