Merge pull request #76 from Boris-Barboris/keepalive_params
fix Keepalive on Linux, expose keepalive options, TCP_USER_TIMEOUT for linux.
This commit is contained in:
commit
c404cc2e5b
7 changed files with 121 additions and 5 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,20 @@ 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
|
||||
// Some day we should siply import core.sys.linux.netinet.tcp;
|
||||
static if (!is(typeof(SOL_TCP)))
|
||||
enum SOL_TCP = 6;
|
||||
static if (!is(typeof(TCP_KEEPIDLE)))
|
||||
enum TCP_KEEPIDLE = 4;
|
||||
static if (!is(typeof(TCP_KEEPINTVL)))
|
||||
enum TCP_KEEPINTVL = 5;
|
||||
static if (!is(typeof(TCP_KEEPCNT)))
|
||||
enum TCP_KEEPCNT = 6;
|
||||
static if (!is(typeof(TCP_USER_TIMEOUT)))
|
||||
enum TCP_USER_TIMEOUT = 18;
|
||||
}
|
||||
version(OSX) {
|
||||
static if (__VERSION__ < 2077) {
|
||||
|
@ -294,10 +310,45 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets
|
|||
() @trusted { setsockopt(cast(sock_t)socket, IPPROTO_TCP, TCP_NODELAY, cast(char*)&opt, opt.sizeof); } ();
|
||||
}
|
||||
|
||||
final override void setKeepAlive(StreamSocketFD socket, bool enable)
|
||||
override void setKeepAlive(StreamSocketFD socket, bool enable) @trusted
|
||||
{
|
||||
ubyte opt = enable;
|
||||
() @trusted { setsockopt(cast(sock_t)socket, SOL_SOCKET, SO_KEEPALIVE, cast(char*)&opt, opt.sizeof); } ();
|
||||
int opt = enable;
|
||||
int err = setsockopt(cast(sock_t)socket, SOL_SOCKET, SO_KEEPALIVE, &opt, int.sizeof);
|
||||
if (err != 0)
|
||||
print("sock error in setKeepAlive: %s", getSocketError);
|
||||
}
|
||||
|
||||
override void setKeepAliveParams(StreamSocketFD socket, Duration idle, Duration interval, int probeCount) @trusted
|
||||
{
|
||||
// dunnno about BSD\OSX, maybe someone should fix it for them later
|
||||
version (linux) {
|
||||
setKeepAlive(socket, true);
|
||||
int int_opt = cast(int) idle.total!"seconds"();
|
||||
int err = setsockopt(cast(sock_t)socket, SOL_TCP, TCP_KEEPIDLE, &int_opt, int.sizeof);
|
||||
if (err != 0) {
|
||||
print("sock error on setsockopt TCP_KEEPIDLE: %s", getSocketError);
|
||||
return;
|
||||
}
|
||||
int_opt = cast(int) interval.total!"seconds"();
|
||||
err = setsockopt(cast(sock_t)socket, SOL_TCP, TCP_KEEPINTVL, &int_opt, int.sizeof);
|
||||
if (err != 0) {
|
||||
print("sock error on setsockopt TCP_KEEPINTVL: %s", getSocketError);
|
||||
return;
|
||||
}
|
||||
err = setsockopt(cast(sock_t)socket, SOL_TCP, TCP_KEEPCNT, &probeCount, int.sizeof);
|
||||
if (err != 0)
|
||||
print("sock error on setsockopt TCP_KEEPCNT: %s", getSocketError);
|
||||
}
|
||||
}
|
||||
|
||||
override void setUserTimeout(StreamSocketFD socket, Duration timeout) @trusted
|
||||
{
|
||||
version (linux) {
|
||||
uint tmsecs = cast(uint) timeout.total!"msecs";
|
||||
int err = setsockopt(cast(sock_t)socket, SOL_TCP, TCP_USER_TIMEOUT, &tmsecs, uint.sizeof);
|
||||
if (err != 0)
|
||||
print("sock error on setsockopt TCP_USER_TIMEOUT %s", getSocketError);
|
||||
}
|
||||
}
|
||||
|
||||
final override void read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish)
|
||||
|
|
|
@ -8,6 +8,8 @@ import eventcore.internal.win32;
|
|||
import eventcore.internal.utils : AlgebraicChoppedVector, print, nogc_assert;
|
||||
import std.socket : Address;
|
||||
|
||||
import core.time: Duration;
|
||||
|
||||
private enum WM_USER_SOCKET = WM_USER + 1;
|
||||
|
||||
|
||||
|
@ -221,6 +223,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
|
||||
{
|
||||
tcp_keepalive opts = tcp_keepalive(1, cast(c_ulong) idle.total!"msecs"(),
|
||||
cast(c_ulong) interval.total!"msecs");
|
||||
int result = WSAIoctl(socket, SIO_KEEPALIVE_VALS, &opts, cast(DWORD) tcp_keepalive.sizeof,
|
||||
null, 0, null, null, null);
|
||||
if (result != 0)
|
||||
print("WSAIoctl error on SIO_KEEPALIVE_VALS: %d", WSAGetLastError());
|
||||
}
|
||||
|
||||
override void setUserTimeout(StreamSocketFD socket, Duration timeout) {}
|
||||
|
||||
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