add setKeepAliveParams method
This commit is contained in:
parent
242432c416
commit
cdb141ba4a
4 changed files with 71 additions and 0 deletions
|
@ -8,6 +8,8 @@ import eventcore.internal.utils;
|
|||
import std.algorithm.comparison : among, min, max;
|
||||
import std.socket : Address, AddressFamily, InternetAddress, Internet6Address, UnknownAddress;
|
||||
|
||||
import core.time: Duration;
|
||||
|
||||
version (Posix) {
|
||||
import std.socket : UnixAddress;
|
||||
import core.sys.posix.netdb : AI_ADDRCONFIG, AI_V4MAPPED, addrinfo, freeaddrinfo, getaddrinfo;
|
||||
|
@ -45,6 +47,13 @@ version (linux) {
|
|||
}
|
||||
else
|
||||
import core.sys.linux.netinet.in_ : IP_ADD_MEMBERSHIP, IP_MULTICAST_LOOP;
|
||||
|
||||
// Linux-specific TCP options
|
||||
// https://github.com/torvalds/linux/blob/master/include/uapi/linux/tcp.h#L95
|
||||
enum SOL_TCP = 6;
|
||||
enum TCP_KEEPIDLE = 4;
|
||||
enum TCP_KEEPINTVL = 5;
|
||||
enum TCP_KEEPCNT = 6;
|
||||
}
|
||||
version(OSX) {
|
||||
static if (__VERSION__ < 2077) {
|
||||
|
@ -300,6 +309,19 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets
|
|||
() @trusted { setsockopt(cast(sock_t)socket, SOL_SOCKET, SO_KEEPALIVE, cast(char*)&opt, opt.sizeof); } ();
|
||||
}
|
||||
|
||||
override void setKeepAliveParams(StreamSocketFD socket, Duration idle, Duration interval, int probeCount) @trusted
|
||||
{
|
||||
version (linux) {
|
||||
ubyte opt = 1;
|
||||
setsockopt(cast(sock_t)socket, SOL_SOCKET, SO_KEEPALIVE, cast(char*)&opt, opt.sizeof);
|
||||
int int_opt = cast(int) idle.total!"seconds"();
|
||||
setsockopt(cast(sock_t)socket, SOL_TCP, TCP_KEEPIDLE, &int_opt, int.sizeof);
|
||||
int_opt = cast(int) interval.total!"seconds"();
|
||||
setsockopt(cast(sock_t)socket, SOL_TCP, TCP_KEEPINTVL, &int_opt, int.sizeof);
|
||||
setsockopt(cast(sock_t)socket, SOL_TCP, TCP_KEEPCNT, &probeCount, int.sizeof);
|
||||
}
|
||||
}
|
||||
|
||||
final override void read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish)
|
||||
{
|
||||
/*if (buffer.length == 0) {
|
||||
|
|
|
@ -221,6 +221,18 @@ final class WinAPIEventDriverSockets : EventDriverSockets {
|
|||
setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &eni, eni.sizeof);
|
||||
}
|
||||
|
||||
override void setKeepAliveParams(StreamSocketFD socket, Duration idle, Duration interval, int probeCount) @trusted
|
||||
{
|
||||
if (idle < Duration.zero)
|
||||
assert(0, "negative idle duration");
|
||||
if (interval < Duration.zero)
|
||||
assert(0, "negative interval duration");
|
||||
tcp_keepalive opts = tcp_keepalive(1, cast(ulong) idle.total!"msecs"(),
|
||||
cast(ulong) interval.total!"msecs");
|
||||
int result = WSAIoctl(socket, SIO_KEEPALIVE_VALS, &opts, tcp_keepalive.sizeof, null, 0, null, null);
|
||||
assert(result == 0);
|
||||
}
|
||||
|
||||
override void read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish)
|
||||
{
|
||||
auto slot = () @trusted { return &m_sockets[socket].streamSocket(); } ();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue