diff --git a/src/io/network/socket.cpp b/src/io/network/socket.cpp index 87da79a71..aea432c39 100644 --- a/src/io/network/socket.cpp +++ b/src/io/network/socket.cpp @@ -137,6 +137,16 @@ bool Socket::SetKeepAlive() { return true; } +bool Socket::SetNoDelay() { + int optval = 1; + socklen_t optlen = sizeof(optval); + + if (setsockopt(socket_, SOL_TCP, TCP_NODELAY, (void*)&optval, optlen) < 0) + return false; + + return true; +} + bool Socket::SetTimeout(long sec, long usec) { struct timeval tv; tv.tv_sec = sec; diff --git a/src/io/network/socket.hpp b/src/io/network/socket.hpp index b37f091d7..3fa02ed2b 100644 --- a/src/io/network/socket.hpp +++ b/src/io/network/socket.hpp @@ -97,6 +97,17 @@ class Socket { */ bool SetKeepAlive(); + /** + * Enables TCP no_delay on the socket. + * When enabled, the socket doesn't wait for an ACK of every data packet + * before sending the next packet. + * + * @return enable no_delay success status: + * true if no_delay was successfully enabled on the socket + * false if no_delay was not enabled + */ + bool SetNoDelay(); + /** * Sets the socket timeout. * diff --git a/src/io/network/stream_reader.hpp b/src/io/network/stream_reader.hpp index 2910039ca..be4b8cc28 100644 --- a/src/io/network/stream_reader.hpp +++ b/src/io/network/stream_reader.hpp @@ -30,6 +30,7 @@ class StreamReader : public StreamListener { s.endpoint().port()); if (!s.SetKeepAlive()) return false; + if (!s.SetNoDelay()) return false; auto& stream = this->derived().OnConnect(std::move(s));