direct.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package ray
  2. import (
  3. "context"
  4. "io"
  5. "sync"
  6. "time"
  7. "v2ray.com/core/common/buf"
  8. )
  9. // NewRay creates a new Ray for direct traffic transport.
  10. func NewRay(ctx context.Context) Ray {
  11. return &directRay{
  12. Input: NewStream(ctx),
  13. Output: NewStream(ctx),
  14. }
  15. }
  16. type directRay struct {
  17. Input *Stream
  18. Output *Stream
  19. }
  20. func (v *directRay) OutboundInput() InputStream {
  21. return v.Input
  22. }
  23. func (v *directRay) OutboundOutput() OutputStream {
  24. return v.Output
  25. }
  26. func (v *directRay) InboundInput() OutputStream {
  27. return v.Input
  28. }
  29. func (v *directRay) InboundOutput() InputStream {
  30. return v.Output
  31. }
  32. type Stream struct {
  33. access sync.Mutex
  34. data buf.MultiBuffer
  35. ctx context.Context
  36. wakeup chan bool
  37. close bool
  38. err bool
  39. }
  40. func NewStream(ctx context.Context) *Stream {
  41. return &Stream{
  42. ctx: ctx,
  43. wakeup: make(chan bool, 1),
  44. }
  45. }
  46. func (s *Stream) getData() (buf.MultiBuffer, error) {
  47. s.access.Lock()
  48. defer s.access.Unlock()
  49. if s.data != nil {
  50. mb := s.data
  51. s.data = nil
  52. return mb, nil
  53. }
  54. if s.close {
  55. return nil, io.EOF
  56. }
  57. if s.err {
  58. return nil, io.ErrClosedPipe
  59. }
  60. return nil, nil
  61. }
  62. func (s *Stream) Read() (buf.MultiBuffer, error) {
  63. for {
  64. mb, err := s.getData()
  65. if err != nil {
  66. return nil, err
  67. }
  68. if mb != nil {
  69. return mb, nil
  70. }
  71. select {
  72. case <-s.ctx.Done():
  73. return nil, io.ErrClosedPipe
  74. case <-s.wakeup:
  75. }
  76. }
  77. }
  78. func (s *Stream) ReadTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
  79. for {
  80. mb, err := s.getData()
  81. if err != nil {
  82. return nil, err
  83. }
  84. if mb != nil {
  85. return mb, nil
  86. }
  87. select {
  88. case <-s.ctx.Done():
  89. return nil, io.ErrClosedPipe
  90. case <-time.After(timeout):
  91. return nil, buf.ErrReadTimeout
  92. case <-s.wakeup:
  93. }
  94. }
  95. }
  96. func (s *Stream) Write(data buf.MultiBuffer) (err error) {
  97. if data.IsEmpty() {
  98. return
  99. }
  100. s.access.Lock()
  101. defer s.access.Unlock()
  102. if s.err {
  103. data.Release()
  104. return io.ErrClosedPipe
  105. }
  106. if s.close {
  107. data.Release()
  108. return io.ErrClosedPipe
  109. }
  110. if s.data == nil {
  111. s.data = data
  112. } else {
  113. s.data.AppendMulti(data)
  114. }
  115. s.wakeUp()
  116. return nil
  117. }
  118. func (s *Stream) wakeUp() {
  119. select {
  120. case s.wakeup <- true:
  121. default:
  122. }
  123. }
  124. func (s *Stream) Close() {
  125. s.access.Lock()
  126. s.close = true
  127. s.wakeUp()
  128. s.access.Unlock()
  129. }
  130. func (s *Stream) CloseError() {
  131. s.access.Lock()
  132. s.err = true
  133. if s.data != nil {
  134. s.data.Release()
  135. s.data = nil
  136. }
  137. s.wakeUp()
  138. s.access.Unlock()
  139. }
  140. func (v *Stream) Release() {}