2018-07-25 05:19:16 -04:00
|
|
|
// +build !windows
|
2018-07-25 05:46:21 -04:00
|
|
|
|
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"
|
|
|
|
"unsafe"
|
2018-07-25 05:19:16 -04:00
|
|
|
|
|
|
|
"v2ray.com/core/common/platform"
|
2018-07-24 15:48:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReadVReader struct {
|
|
|
|
io.Reader
|
|
|
|
rawConn syscall.RawConn
|
2018-07-24 16:17:30 -04:00
|
|
|
iovects []syscall.Iovec
|
2018-07-24 15:48:28 -04:00
|
|
|
nBuf int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReadVReader(reader io.Reader, rawConn syscall.RawConn) *ReadVReader {
|
|
|
|
return &ReadVReader{
|
|
|
|
Reader: reader,
|
|
|
|
rawConn: rawConn,
|
|
|
|
nBuf: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func allocN(n int32) []*Buffer {
|
|
|
|
bs := make([]*Buffer, 0, n)
|
|
|
|
for i := int32(0); i < n; i++ {
|
|
|
|
bs = append(bs, New())
|
|
|
|
}
|
|
|
|
return bs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ReadVReader) ReadMultiBuffer() (MultiBuffer, error) {
|
|
|
|
bs := allocN(r.nBuf)
|
|
|
|
|
2018-07-24 16:17:30 -04:00
|
|
|
var iovecs []syscall.Iovec
|
|
|
|
if r.iovects != nil {
|
|
|
|
iovecs = r.iovects
|
|
|
|
}
|
2018-07-24 15:48:28 -04:00
|
|
|
for idx, b := range bs {
|
2018-07-24 16:17:30 -04:00
|
|
|
iovecs = append(iovecs, syscall.Iovec{
|
2018-07-24 15:48:28 -04:00
|
|
|
Base: &(b.v[0]),
|
2018-07-24 16:17:30 -04:00
|
|
|
})
|
2018-07-24 15:48:28 -04:00
|
|
|
iovecs[idx].SetLen(int(Size))
|
|
|
|
}
|
2018-07-24 16:17:30 -04:00
|
|
|
r.iovects = iovecs[:0]
|
2018-07-24 15:48:28 -04:00
|
|
|
|
|
|
|
var nBytes int
|
|
|
|
|
|
|
|
err := r.rawConn.Read(func(fd uintptr) bool {
|
|
|
|
n, _, e := syscall.Syscall(syscall.SYS_READV, fd, uintptr(unsafe.Pointer(&iovecs[0])), uintptr(len(iovecs)))
|
|
|
|
if e != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
nBytes = int(n)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
mb := MultiBuffer(bs)
|
|
|
|
mb.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nBytes == 0 {
|
|
|
|
mb := MultiBuffer(bs)
|
|
|
|
mb.Release()
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:17:30 -04:00
|
|
|
var isFull bool = (nBytes == int(r.nBuf)*Size)
|
|
|
|
|
2018-07-24 15:48:28 -04:00
|
|
|
nBuf := 0
|
|
|
|
for nBuf < len(bs) {
|
|
|
|
if nBytes <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
end := int32(nBytes)
|
|
|
|
if end > Size {
|
|
|
|
end = Size
|
|
|
|
}
|
|
|
|
bs[nBuf].end = end
|
|
|
|
nBytes -= int(end)
|
|
|
|
nBuf++
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := nBuf; i < len(bs); i++ {
|
|
|
|
bs[i].Release()
|
|
|
|
bs[i] = nil
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:17:30 -04:00
|
|
|
if isFull && nBuf < 128 {
|
2018-07-24 15:48:28 -04:00
|
|
|
r.nBuf *= 4
|
|
|
|
} else {
|
|
|
|
r.nBuf = int32(nBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
return MultiBuffer(bs[:nBuf]), nil
|
|
|
|
}
|
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 })
|
|
|
|
if value != defaultFlagValue && (runtime.GOOS == "linux" || runtime.GOOS == "darwin") {
|
|
|
|
useReadv = true
|
|
|
|
}
|
|
|
|
}
|