direct.go 2.5 KB

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