blob: 948c348789f4bf3e7d45810ddd69fa362208eabb [file] [log] [blame]
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
"runtime"
"syscall"
"time"
)
func setNoDelay(fd *netFD, noDelay bool) error {
b := make([]byte, 4)
if noDelay {
b[0] = 1
}
err := fd.sock.SetSockOpt(syscall.SOL_TCP, syscall.TCP_NODELAY, b)
runtime.KeepAlive(fd)
return err
}
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The socket option expects seconds so round to next highest second.
secs := uint32((d + time.Second - 1) / time.Second)
b := make([]byte, 4)
putUint32(b, secs)
if err := fd.sock.SetSockOpt(syscall.SOL_TCP, syscall.TCP_KEEPINTVL, b); err != nil {
return wrapSyscallError("setsockopt", err)
}
err := fd.sock.SetSockOpt(syscall.SOL_TCP, syscall.TCP_KEEPIDLE, b)
runtime.KeepAlive(fd)
return wrapSyscallError("setsockopt", err)
}
func putUint32(b []byte, v uint32) {
_ = b[3] // early bounds check to guarantee safety of writes below
b[0] = byte(v)
b[1] = byte(v >> 8)
b[2] = byte(v >> 16)
b[3] = byte(v >> 24)
}