direct.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.RWMutex
  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) Peek() buf.MultiBuffer {
  63. s.access.RLock()
  64. defer s.access.RUnlock()
  65. return s.data
  66. }
  67. func (s *Stream) Read() (buf.MultiBuffer, error) {
  68. for {
  69. mb, err := s.getData()
  70. if err != nil {
  71. return nil, err
  72. }
  73. if mb != nil {
  74. return mb, nil
  75. }
  76. select {
  77. case <-s.ctx.Done():
  78. return nil, io.EOF
  79. case <-s.wakeup:
  80. }
  81. }
  82. }
  83. func (s *Stream) ReadTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
  84. for {
  85. mb, err := s.getData()
  86. if err != nil {
  87. return nil, err
  88. }
  89. if mb != nil {
  90. return mb, nil
  91. }
  92. select {
  93. case <-s.ctx.Done():
  94. return nil, io.EOF
  95. case <-time.After(timeout):
  96. return nil, buf.ErrReadTimeout
  97. case <-s.wakeup:
  98. }
  99. }
  100. }
  101. func (s *Stream) Write(data buf.MultiBuffer) error {
  102. if data.IsEmpty() {
  103. return nil
  104. }
  105. s.access.Lock()
  106. defer s.access.Unlock()
  107. if s.err || s.close {
  108. data.Release()
  109. return io.ErrClosedPipe
  110. }
  111. if s.data == nil {
  112. s.data = data
  113. } else {
  114. s.data.AppendMulti(data)
  115. }
  116. s.wakeUp()
  117. return nil
  118. }
  119. func (s *Stream) wakeUp() {
  120. select {
  121. case s.wakeup <- true:
  122. default:
  123. }
  124. }
  125. func (s *Stream) Close() {
  126. s.access.Lock()
  127. s.close = true
  128. s.wakeUp()
  129. s.access.Unlock()
  130. }
  131. func (s *Stream) CloseError() {
  132. s.access.Lock()
  133. s.err = true
  134. if s.data != nil {
  135. s.data.Release()
  136. s.data = nil
  137. }
  138. s.wakeUp()
  139. s.access.Unlock()
  140. }