Skip to content

Commit

Permalink
Merge pull request #18 from niklas88/driveby_cleanups
Browse files Browse the repository at this point in the history
Set SO_KEEPALIVE on client sockets
  • Loading branch information
Buchhold committed Jul 17, 2017
2 parents c392c68 + dada9a5 commit 5fc9edd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/engine/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ void Server::run() {
LOG(ERROR) << "Socket error while trying to accept client" << std::endl;
continue;
}
// Setting Keep Alive on the socket helps detect vanished clients
// instead of blocking operations idefinitely
client.setKeepAlive(true);
// TODO(buchholb): Spawn a new thread here / use a ThreadPool / etc.
LOG(INFO) << "Incoming connection, processing..." << std::endl;
process(&client, &qec);
Expand Down
9 changes: 9 additions & 0 deletions src/util/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ class Socket {
return isOpen();
}

void setKeepAlive(bool keepAlive) const {
int keepAliveVal = (keepAlive) ? 1 : 0;
int rc = setsockopt(_fd, SOL_SOCKET,
SO_KEEPALIVE, &keepAliveVal, sizeof(keepAliveVal));
if (rc < 0) {
LOG(WARN) << "setsockopt(SO_KEEPALIVE) failed" << std::endl;
}
}

void makeResusableAfterClosing() const {
// Make sockets reusable immediately after closing
int on = 1;
Expand Down

0 comments on commit 5fc9edd

Please sign in to comment.