Brad Fitzpatrick | 5194744 | 2016-03-01 22:57:46 +0000 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 5 | package net |
| 6 | |
| 7 | import ( |
Nicholas Katsaros | 5277b90 | 2014-01-10 14:33:54 +1100 | [diff] [blame] | 8 | "os" |
| 9 | "syscall" |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 10 | "time" |
Nicholas Katsaros | 5277b90 | 2014-01-10 14:33:54 +1100 | [diff] [blame] | 11 | "unsafe" |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | func setKeepAlivePeriod(fd *netFD, d time.Duration) error { |
Dmitriy Vyukov | 23e15f7 | 2013-08-09 21:43:00 +0400 | [diff] [blame] | 15 | if err := fd.incref(); err != nil { |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 16 | return err |
| 17 | } |
| 18 | defer fd.decref() |
Mikio Hara | f956740 | 2014-09-11 17:56:58 +0900 | [diff] [blame] | 19 | // The kernel expects milliseconds so round to next highest |
| 20 | // millisecond. |
Nicholas Katsaros | 5277b90 | 2014-01-10 14:33:54 +1100 | [diff] [blame] | 21 | d += (time.Millisecond - time.Nanosecond) |
Mikio Hara | f956740 | 2014-09-11 17:56:58 +0900 | [diff] [blame] | 22 | msecs := uint32(d / time.Millisecond) |
Nicholas Katsaros | 5277b90 | 2014-01-10 14:33:54 +1100 | [diff] [blame] | 23 | ka := syscall.TCPKeepalive{ |
| 24 | OnOff: 1, |
Mikio Hara | f956740 | 2014-09-11 17:56:58 +0900 | [diff] [blame] | 25 | Time: msecs, |
| 26 | Interval: msecs, |
Nicholas Katsaros | 5277b90 | 2014-01-10 14:33:54 +1100 | [diff] [blame] | 27 | } |
| 28 | ret := uint32(0) |
| 29 | size := uint32(unsafe.Sizeof(ka)) |
| 30 | err := syscall.WSAIoctl(fd.sysfd, syscall.SIO_KEEPALIVE_VALS, (*byte)(unsafe.Pointer(&ka)), size, nil, 0, &ret, nil, 0) |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 31 | return os.NewSyscallError("wsaioctl", err) |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 32 | } |