Skip to content

Commit c5e14ab

Browse files
committed
Add control.SetKeepAlivePeriod
1 parent 3f6c423 commit c5e14ab

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !linux
2+
3+
package control
4+
5+
import (
6+
"net"
7+
"time"
8+
)
9+
10+
func SetKeepAlivePeriod(conn *net.TCPConn, initial time.Duration, interval time.Duration) error {
11+
return conn.SetKeepAlivePeriod(initial)
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package control
2+
3+
import (
4+
E "github.com/sagernet/sing/common/exceptions"
5+
"net"
6+
"time"
7+
8+
"golang.org/x/sys/unix"
9+
_ "unsafe"
10+
)
11+
12+
func SetKeepAlivePeriod(conn *net.TCPConn, initial time.Duration, interval time.Duration) error {
13+
return Conn(conn, func(fd uintptr) error {
14+
return E.Errors(
15+
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPIDLE, int(roundDurationUp(initial, time.Second))),
16+
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPINTVL, int(roundDurationUp(interval, time.Second))),
17+
)
18+
})
19+
}
20+
21+
//go:linkname roundDurationUp net.roundDurationUp
22+
func roundDurationUp(d time.Duration, to time.Duration) time.Duration

0 commit comments

Comments
 (0)