]> Cypherpunks repositories - gostls13.git/commitdiff
net: add SetKeepAlivePeriod for windows
authorNicholas Katsaros <nick@nickkatsaros.com>
Fri, 10 Jan 2014 03:33:54 +0000 (14:33 +1100)
committerAlex Brainman <alex.brainman@gmail.com>
Fri, 10 Jan 2014 03:33:54 +0000 (14:33 +1100)
R=golang-codereviews, alex.brainman, bradfitz, mikioh.mikioh
CC=golang-codereviews
https://golang.org/cl/11393043

src/pkg/net/tcpsockopt_windows.go
src/pkg/syscall/ztypes_windows.go

index 0bf4312f248b04befe5acf13ed2784fec466fff0..8ef1407977f5a3839af7ae01ea8a38742090c4ef 100644 (file)
@@ -7,7 +7,10 @@
 package net
 
 import (
+       "os"
+       "syscall"
        "time"
+       "unsafe"
 )
 
 func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
@@ -16,6 +19,16 @@ func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
        }
        defer fd.decref()
 
-       // We can't actually set this per connection.  Act as a noop rather than an error.
-       return nil
+       // Windows expects milliseconds so round to next highest millisecond.
+       d += (time.Millisecond - time.Nanosecond)
+       millis := uint32(d / time.Millisecond)
+       ka := syscall.TCPKeepalive{
+               OnOff:    1,
+               Time:     millis,
+               Interval: millis,
+       }
+       ret := uint32(0)
+       size := uint32(unsafe.Sizeof(ka))
+       err := syscall.WSAIoctl(fd.sysfd, syscall.SIO_KEEPALIVE_VALS, (*byte)(unsafe.Pointer(&ka)), size, nil, 0, &ret, nil, 0)
+       return os.NewSyscallError("WSAIoctl", err)
 }
index bdc15ce3bc5da847a3829f0549b581c101656876..28cd3f61693430e8466ba89df293c8fe0a6ec422 100644 (file)
@@ -512,9 +512,11 @@ const (
 
        IOC_OUT                            = 0x40000000
        IOC_IN                             = 0x80000000
+       IOC_VENDOR                         = 0x18000000
        IOC_INOUT                          = IOC_IN | IOC_OUT
        IOC_WS2                            = 0x08000000
        SIO_GET_EXTENSION_FUNCTION_POINTER = IOC_INOUT | IOC_WS2 | 6
+       SIO_KEEPALIVE_VALS                 = IOC_IN | IOC_VENDOR | 4
 
        // cf. http://support.microsoft.com/default.aspx?scid=kb;en-us;257460
 
@@ -1031,3 +1033,9 @@ type WSAProtocolChain struct {
        ChainLen     int32
        ChainEntries [MAX_PROTOCOL_CHAIN]uint32
 }
+
+type TCPKeepalive struct {
+       OnOff    uint32
+       Time     uint32
+       Interval uint32
+}