readv_reader.go 2.5 KB

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