readv_reader.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // +build !wasm
  2. package buf
  3. import (
  4. "io"
  5. "syscall"
  6. "github.com/xtls/xray-core/features/stats"
  7. "github.com/xtls/xray-core/common/platform"
  8. )
  9. type allocStrategy struct {
  10. current uint32
  11. }
  12. func (s *allocStrategy) Current() uint32 {
  13. return s.current
  14. }
  15. func (s *allocStrategy) Adjust(n uint32) {
  16. if n >= s.current {
  17. s.current *= 2
  18. } else {
  19. s.current = n
  20. }
  21. if s.current > 8 {
  22. s.current = 8
  23. }
  24. if s.current == 0 {
  25. s.current = 1
  26. }
  27. }
  28. func (s *allocStrategy) Alloc() []*Buffer {
  29. bs := make([]*Buffer, s.current)
  30. for i := range bs {
  31. bs[i] = New()
  32. }
  33. return bs
  34. }
  35. type multiReader interface {
  36. Init([]*Buffer)
  37. Read(fd uintptr) int32
  38. Clear()
  39. }
  40. // ReadVReader is a Reader that uses readv(2) syscall to read data.
  41. type ReadVReader struct {
  42. io.Reader
  43. rawConn syscall.RawConn
  44. mr multiReader
  45. alloc allocStrategy
  46. counter stats.Counter
  47. }
  48. // NewReadVReader creates a new ReadVReader.
  49. func NewReadVReader(reader io.Reader, rawConn syscall.RawConn, counter stats.Counter) *ReadVReader {
  50. return &ReadVReader{
  51. Reader: reader,
  52. rawConn: rawConn,
  53. alloc: allocStrategy{
  54. current: 1,
  55. },
  56. mr: newMultiReader(),
  57. counter: counter,
  58. }
  59. }
  60. func (r *ReadVReader) readMulti() (MultiBuffer, error) {
  61. bs := r.alloc.Alloc()
  62. r.mr.Init(bs)
  63. var nBytes int32
  64. err := r.rawConn.Read(func(fd uintptr) bool {
  65. n := r.mr.Read(fd)
  66. if n < 0 {
  67. return false
  68. }
  69. nBytes = n
  70. return true
  71. })
  72. r.mr.Clear()
  73. if err != nil {
  74. ReleaseMulti(MultiBuffer(bs))
  75. return nil, err
  76. }
  77. if nBytes == 0 {
  78. ReleaseMulti(MultiBuffer(bs))
  79. return nil, io.EOF
  80. }
  81. nBuf := 0
  82. for nBuf < len(bs) {
  83. if nBytes <= 0 {
  84. break
  85. }
  86. end := nBytes
  87. if end > Size {
  88. end = Size
  89. }
  90. bs[nBuf].end = end
  91. nBytes -= end
  92. nBuf++
  93. }
  94. for i := nBuf; i < len(bs); i++ {
  95. bs[i].Release()
  96. bs[i] = nil
  97. }
  98. return MultiBuffer(bs[:nBuf]), nil
  99. }
  100. // ReadMultiBuffer implements Reader.
  101. func (r *ReadVReader) ReadMultiBuffer() (MultiBuffer, error) {
  102. if r.alloc.Current() == 1 {
  103. b, err := ReadBuffer(r.Reader)
  104. if b.IsFull() {
  105. r.alloc.Adjust(1)
  106. }
  107. if r.counter != nil && b != nil {
  108. r.counter.Add(int64(b.Len()))
  109. }
  110. return MultiBuffer{b}, err
  111. }
  112. mb, err := r.readMulti()
  113. if r.counter != nil && mb != nil {
  114. r.counter.Add(int64(mb.Len()))
  115. }
  116. if err != nil {
  117. return nil, err
  118. }
  119. r.alloc.Adjust(uint32(len(mb)))
  120. return mb, nil
  121. }
  122. var useReadv bool
  123. func init() {
  124. const defaultFlagValue = "NOT_DEFINED_AT_ALL"
  125. value := platform.NewEnvFlag("xray.buf.readv").GetValue(func() string { return defaultFlagValue })
  126. switch value {
  127. case defaultFlagValue, "auto", "enable":
  128. useReadv = true
  129. }
  130. }