readv_windows.go 733 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package buf
  2. import (
  3. "syscall"
  4. )
  5. type windowsReader struct {
  6. bufs []syscall.WSABuf
  7. }
  8. func (r *windowsReader) Init(bs []*Buffer) {
  9. if r.bufs == nil {
  10. r.bufs = make([]syscall.WSABuf, 0, len(bs))
  11. }
  12. for _, b := range bs {
  13. r.bufs = append(r.bufs, syscall.WSABuf{Len: uint32(Size), Buf: &b.v[0]})
  14. }
  15. }
  16. func (r *windowsReader) Clear() {
  17. for idx := range r.bufs {
  18. r.bufs[idx].Buf = nil
  19. }
  20. r.bufs = r.bufs[:0]
  21. }
  22. func (r *windowsReader) Read(fd uintptr) int32 {
  23. var nBytes uint32
  24. var flags uint32
  25. err := syscall.WSARecv(syscall.Handle(fd), &r.bufs[0], uint32(len(r.bufs)), &nBytes, &flags, nil, nil)
  26. if err != nil {
  27. return -1
  28. }
  29. return int32(nBytes)
  30. }
  31. func newMultiReader() multiReader {
  32. return new(windowsReader)
  33. }