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