1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 10:45:22 +00:00
v2fly/common/buf/readv_reader.go

152 lines
2.5 KiB
Go
Raw Normal View History

//go:build !wasm
2018-08-27 18:56:49 +00:00
// +build !wasm
2018-07-24 19:48:28 +00:00
package buf
import (
"io"
2018-07-25 09:19:16 +00:00
"runtime"
2018-07-24 19:48:28 +00:00
"syscall"
2018-07-25 09:19:16 +00:00
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common/platform"
2018-07-24 19:48:28 +00:00
)
2018-08-18 21:12:22 +00:00
type allocStrategy struct {
current uint32
}
func (s *allocStrategy) Current() uint32 {
return s.current
}
func (s *allocStrategy) Adjust(n uint32) {
if n >= s.current {
s.current *= 4
} else {
s.current = n
}
if s.current > 32 {
s.current = 32
}
if s.current == 0 {
s.current = 1
}
}
func (s *allocStrategy) Alloc() []*Buffer {
bs := make([]*Buffer, s.current)
for i := range bs {
bs[i] = New()
}
return bs
}
type multiReader interface {
Init([]*Buffer)
Read(fd uintptr) int32
Clear()
}
2018-11-18 23:33:00 +00:00
// ReadVReader is a Reader that uses readv(2) syscall to read data.
2018-07-24 19:48:28 +00:00
type ReadVReader struct {
io.Reader
rawConn syscall.RawConn
2018-08-18 21:12:22 +00:00
mr multiReader
alloc allocStrategy
2018-07-24 19:48:28 +00:00
}
2018-11-18 23:33:00 +00:00
// NewReadVReader creates a new ReadVReader.
2018-07-24 19:48:28 +00:00
func NewReadVReader(reader io.Reader, rawConn syscall.RawConn) *ReadVReader {
return &ReadVReader{
Reader: reader,
rawConn: rawConn,
2018-08-18 21:12:22 +00:00
alloc: allocStrategy{
current: 1,
},
mr: newMultiReader(),
2018-07-24 19:48:28 +00:00
}
}
2018-07-31 11:04:55 +00:00
func (r *ReadVReader) readMulti() (MultiBuffer, error) {
2018-08-18 21:12:22 +00:00
bs := r.alloc.Alloc()
2018-07-24 19:48:28 +00:00
2018-08-18 21:12:22 +00:00
r.mr.Init(bs)
var nBytes int32
2018-07-24 19:48:28 +00:00
err := r.rawConn.Read(func(fd uintptr) bool {
2018-08-18 21:12:22 +00:00
n := r.mr.Read(fd)
if n < 0 {
2018-07-24 19:48:28 +00:00
return false
}
2018-08-18 21:12:22 +00:00
nBytes = n
2018-07-24 19:48:28 +00:00
return true
})
2018-08-18 21:12:22 +00:00
r.mr.Clear()
2018-07-24 19:48:28 +00:00
if err != nil {
2018-11-17 21:45:07 +00:00
ReleaseMulti(MultiBuffer(bs))
2018-07-24 19:48:28 +00:00
return nil, err
}
if nBytes == 0 {
2018-11-17 21:45:07 +00:00
ReleaseMulti(MultiBuffer(bs))
2018-07-24 19:48:28 +00:00
return nil, io.EOF
}
nBuf := 0
for nBuf < len(bs) {
if nBytes <= 0 {
break
}
2018-11-13 22:19:58 +00:00
end := nBytes
2018-07-24 19:48:28 +00:00
if end > Size {
end = Size
}
bs[nBuf].end = end
2018-08-18 21:12:22 +00:00
nBytes -= end
2018-07-24 19:48:28 +00:00
nBuf++
}
for i := nBuf; i < len(bs); i++ {
bs[i].Release()
bs[i] = nil
}
2018-07-31 11:04:55 +00:00
return MultiBuffer(bs[:nBuf]), nil
}
// ReadMultiBuffer implements Reader.
func (r *ReadVReader) ReadMultiBuffer() (MultiBuffer, error) {
2018-08-18 21:12:22 +00:00
if r.alloc.Current() == 1 {
2018-12-03 15:01:14 +00:00
b, err := ReadBuffer(r.Reader)
2018-07-31 11:04:55 +00:00
if b.IsFull() {
2018-08-18 21:12:22 +00:00
r.alloc.Adjust(1)
2018-07-31 11:04:55 +00:00
}
2019-11-22 02:11:23 +00:00
return MultiBuffer{b}, err
2018-07-24 19:48:28 +00:00
}
2018-07-31 11:04:55 +00:00
mb, err := r.readMulti()
if err != nil {
return nil, err
}
2018-08-18 21:12:22 +00:00
r.alloc.Adjust(uint32(len(mb)))
2018-07-31 11:04:55 +00:00
return mb, nil
2018-07-24 19:48:28 +00:00
}
2018-07-25 09:19:16 +00:00
var useReadv = false
func init() {
const defaultFlagValue = "NOT_DEFINED_AT_ALL"
value := platform.NewEnvFlag("v2ray.buf.readv").GetValue(func() string { return defaultFlagValue })
2018-08-25 08:41:39 +00:00
switch value {
case defaultFlagValue, "auto":
2018-08-31 13:17:45 +00:00
if (runtime.GOARCH == "386" || runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x") && (runtime.GOOS == "linux" || runtime.GOOS == "darwin" || runtime.GOOS == "windows") {
2018-08-25 08:41:39 +00:00
useReadv = true
}
case "enable":
2018-07-25 09:19:16 +00:00
useReadv = true
}
}