github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/buf/readv_posix.go (about)

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