Skip to content

Commit

Permalink
Implement TCPConnection.keepAlive. Fixes #622.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed May 17, 2014
1 parent 26306eb commit 7d1f35e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions source/vibe/core/drivers/libev.d
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,14 @@ class LibevTCPConnection : TCPConnection {
}
@property Duration readTimeout() const { return m_readTimeout; }

@property void keepAlive(bool enabled)
{
m_keepAlive = enabled;
ubyte opt = enabled;
setsockopt(m_socket, SOL_SOCKET, SO_KEEPALIVE, &opt, opt.sizeof);
}
@property bool keepAlive() const { return m_keepAlive; }

@property bool connected() const { return m_socket >= 0; }

@property bool dataAvailableForRead(){ return m_readBufferContent.length > 0; }
Expand Down
11 changes: 11 additions & 0 deletions source/vibe/core/drivers/libevent2_tcp.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ package class Libevent2TCPConnection : TCPConnection {
string m_peerAddress;
ubyte[64] m_peekBuffer;
bool m_tcpNoDelay = false;
bool m_tcpKeepAlive = false;
Duration m_readTimeout;
char[64] m_peerAddressBuf;
NetworkAddress m_localAddress, m_remoteAddress;
Expand Down Expand Up @@ -112,6 +113,16 @@ package class Libevent2TCPConnection : TCPConnection {
}
@property Duration readTimeout() const { return m_readTimeout; }

@property void keepAlive(bool enable)
{
m_tcpKeepAlive = enable;
auto fd = m_ctx.socketfd;
ubyte opt = enable;
assert(fd <= int.max, "Socket descriptor > int.max");
setsockopt(cast(int)fd, SOL_SOCKET, SO_KEEPALIVE, &opt, opt.sizeof);
}
@property bool keepAlive() const { return m_tcpKeepAlive; }

@property NetworkAddress localAddress() const { return m_localAddress; }
@property NetworkAddress remoteAddress() const { return m_remoteAddress; }

Expand Down
10 changes: 10 additions & 0 deletions source/vibe/core/drivers/win32.d
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ class Win32TCPConnection : TCPConnection, SocketEventHandler {
Task m_writeOwner;
bool m_tcpNoDelay;
Duration m_readTimeout;
bool m_keepAlive;
SOCKET m_socket;
NetworkAddress m_localAddress;
NetworkAddress m_peerAddress;
Expand Down Expand Up @@ -1168,6 +1169,15 @@ class Win32TCPConnection : TCPConnection, SocketEventHandler {
}
@property Duration readTimeout() const { return m_readTimeout; }

@property void keepAlive(bool enabled)
{
m_keepAlive = enabled;
BOOL eni = enabled;
setsockopt(m_socket, SOL_SOCKET, SO_KEEPALIVE, &eni, eni.sizeof);
assert(false);
}
@property bool keepAlive() const { return m_keepAlive; }

@property bool connected() const { return m_status == ConnectionStatus.Connected; }

@property string peerAddress() const { return m_peerAddressString; }
Expand Down
8 changes: 8 additions & 0 deletions source/vibe/core/drivers/winrt.d
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ version(VibeWinrtDriver)
class WinRTTCPConnection : TCPConnection {
private {
bool m_tcpNoDelay;
bool m_keepAlive;
Duration m_readTimeout;
}

Expand All @@ -302,6 +303,13 @@ version(VibeWinrtDriver)
}
@property Duration readTimeout() const { return m_readTimeout; }

@property void keepAlive(bool enabled)
{
m_keepAlive = enabled;
assert(false);
}
@property bool keepAlive() const { return m_keepAlive; }

void close()
{
}
Expand Down
6 changes: 6 additions & 0 deletions source/vibe/core/net.d
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ interface TCPConnection : ConnectionStream {
/// ditto
@property bool tcpNoDelay() const;


/// Enables TCP keep-alive packets.
@property void keepAlive(bool enable);
/// ditto
@property bool keepAlive() const;

/// Controls the read time out after which the connection is closed automatically.
@property void readTimeout(Duration duration);
/// ditto
Expand Down

0 comments on commit 7d1f35e

Please sign in to comment.