inboundhandler.go 958 B

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