-
Notifications
You must be signed in to change notification settings - Fork 0
/
readv_reader.go
113 lines (94 loc) · 1.9 KB
/
readv_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// +build !windows
package buf
import (
"io"
"runtime"
"syscall"
"unsafe"
"v2ray.com/core/common/platform"
)
type ReadVReader struct {
io.Reader
rawConn syscall.RawConn
iovects []syscall.Iovec
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)
var iovecs []syscall.Iovec
if r.iovects != nil {
iovecs = r.iovects
}
for idx, b := range bs {
iovecs = append(iovecs, syscall.Iovec{
Base: &(b.v[0]),
})
iovecs[idx].SetLen(int(Size))
}
r.iovects = iovecs[:0]
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
}
var isFull bool = (nBytes == int(r.nBuf)*Size)
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
}
if isFull && nBuf < 8 {
r.nBuf *= 4
} else {
r.nBuf = int32(nBuf)
}
return MultiBuffer(bs[:nBuf]), nil
}
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
}
}