github.com/eagleql/xray-core@v1.4.4/common/buf/readv_posix.go (about)

     1  // +build !windows
     2  // +build !wasm
     3  // +build !illumos
     4  
     5  package buf
     6  
     7  import (
     8  	"syscall"
     9  	"unsafe"
    10  )
    11  
    12  type posixReader struct {
    13  	iovecs []syscall.Iovec
    14  }
    15  
    16  func (r *posixReader) Init(bs []*Buffer) {
    17  	iovecs := r.iovecs
    18  	if iovecs == nil {
    19  		iovecs = make([]syscall.Iovec, 0, len(bs))
    20  	}
    21  	for idx, b := range bs {
    22  		iovecs = append(iovecs, syscall.Iovec{
    23  			Base: &(b.v[0]),
    24  		})
    25  		iovecs[idx].SetLen(int(Size))
    26  	}
    27  	r.iovecs = iovecs
    28  }
    29  
    30  func (r *posixReader) Read(fd uintptr) int32 {
    31  	n, _, e := syscall.Syscall(syscall.SYS_READV, fd, uintptr(unsafe.Pointer(&r.iovecs[0])), uintptr(len(r.iovecs)))
    32  	if e != 0 {
    33  		return -1
    34  	}
    35  	return int32(n)
    36  }
    37  
    38  func (r *posixReader) Clear() {
    39  	for idx := range r.iovecs {
    40  		r.iovecs[idx].Base = nil
    41  	}
    42  	r.iovecs = r.iovecs[:0]
    43  }
    44  
    45  func newMultiReader() multiReader {
    46  	return &posixReader{}
    47  }