mirror of https://github.com/v2ray/v2ray-core
Darien Raymond
6 years ago
4 changed files with 142 additions and 55 deletions
@ -0,0 +1,45 @@
|
||||
// +build !windows
|
||||
|
||||
package buf |
||||
|
||||
import ( |
||||
"syscall" |
||||
"unsafe" |
||||
) |
||||
|
||||
type posixReader struct { |
||||
iovecs []syscall.Iovec |
||||
} |
||||
|
||||
func (r *posixReader) Init(bs []*Buffer) { |
||||
iovecs := r.iovecs |
||||
if iovecs == nil { |
||||
iovecs = make([]syscall.Iovec, 0, len(bs)) |
||||
} |
||||
for idx, b := range bs { |
||||
iovecs = append(iovecs, syscall.Iovec{ |
||||
Base: &(b.v[0]), |
||||
}) |
||||
iovecs[idx].SetLen(int(Size)) |
||||
} |
||||
r.iovecs = iovecs |
||||
} |
||||
|
||||
func (r *posixReader) Read(fd uintptr) int32 { |
||||
n, _, e := syscall.Syscall(syscall.SYS_READV, fd, uintptr(unsafe.Pointer(&r.iovecs[0])), uintptr(len(r.iovecs))) |
||||
if e != 0 { |
||||
return -1 |
||||
} |
||||
return int32(n) |
||||
} |
||||
|
||||
func (r *posixReader) Clear() { |
||||
for idx := range r.iovecs { |
||||
r.iovecs[idx].Base = nil |
||||
} |
||||
r.iovecs = r.iovecs[:0] |
||||
} |
||||
|
||||
func newMultiReader() multiReader { |
||||
return &posixReader{} |
||||
} |
@ -1,14 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
package buf |
||||
|
||||
import ( |
||||
"io" |
||||
"syscall" |
||||
) |
||||
|
||||
const useReadv = false |
||||
|
||||
func NewReadVReader(reader io.Reader, rawConn syscall.RawConn) Reader { |
||||
panic("Shoud not happen") |
||||
} |
@ -0,0 +1,41 @@
|
||||
// +build windows
|
||||
|
||||
package buf |
||||
|
||||
import "syscall" |
||||
|
||||
type windowsReader struct { |
||||
o syscall.Overlapped |
||||
bufs []syscall.WSABuf |
||||
flags uint32 |
||||
qty uint32 |
||||
} |
||||
|
||||
func (r *windowsReader) Init(bs []*Buffer) { |
||||
if r.bufs == nil { |
||||
r.bufs = make([]syscall.WSABuf, 0, len(bs)) |
||||
} |
||||
for _, b := range bs { |
||||
r.bufs = append(r.bufs, syscall.WSABuf{Len: uint32(b.Len()), Buf: &b.v[0]}) |
||||
} |
||||
} |
||||
|
||||
func (r *windowsReader) Clear() { |
||||
for idx := range r.bufs { |
||||
r.bufs[idx].Buf = nil |
||||
} |
||||
r.bufs = r.bufs[:0] |
||||
} |
||||
|
||||
func (r *windowsReader) Read(fd uintptr) int32 { |
||||
var nBytes uint32 |
||||
err := syscall.WSARecv(syscall.Handle(fd), &r.bufs[0], uint32(len(r.bufs)), &nBytes, &r.flags, &r.o, nil) |
||||
if err != nil { |
||||
return -1 |
||||
} |
||||
return int32(nBytes) |
||||
} |
||||
|
||||
func newMultiReader() multiReader { |
||||
return new(windowsReader) |
||||
} |
Loading…
Reference in new issue