tcpsockopt_windows.go 818 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package net
  5. import (
  6. "os"
  7. "syscall"
  8. "time"
  9. "unsafe"
  10. )
  11. func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
  12. if err := fd.incref(); err != nil {
  13. return err
  14. }
  15. defer fd.decref()
  16. // The kernel expects milliseconds so round to next highest
  17. // millisecond.
  18. d += (time.Millisecond - time.Nanosecond)
  19. msecs := uint32(d / time.Millisecond)
  20. ka := syscall.TCPKeepalive{
  21. OnOff: 1,
  22. Time: msecs,
  23. Interval: msecs,
  24. }
  25. ret := uint32(0)
  26. size := uint32(unsafe.Sizeof(ka))
  27. err := syscall.WSAIoctl(fd.sysfd, syscall.SIO_KEEPALIVE_VALS, (*byte)(unsafe.Pointer(&ka)), size, nil, 0, &ret, nil, 0)
  28. return os.NewSyscallError("WSAIoctl", err)
  29. }