diff --git a/src/net/tcp_server_socket.cpp b/src/net/tcp_server_socket.cpp index d98d2ce377449b98d2718b1c04dc36a0d45264e8..d49f7de1ee57bc409020725e8ed94b0520bb73f3 100644 --- a/src/net/tcp_server_socket.cpp +++ b/src/net/tcp_server_socket.cpp @@ -17,6 +17,7 @@ #include <sys/socket.h> #include <sys/stat.h> #include <netinet/in.h> +#include <netinet/tcp.h> #include <functional> @@ -71,6 +72,7 @@ tcp_server_socket::tcp_server_socket( using namespace std::placeholders; this->ss->set_accept_handler(std::bind(&tcp_server_socket::await_raw_socket_incomming, this, _1, _2)); + this->set_low_latency_mode(false); } static inline socketaddr get_ipv6_socketaddr(const uint16_t port) { @@ -112,6 +114,14 @@ void tcp_server_socket::await_raw_socket_incomming( this->number_of_connected_clients++; using namespace std::placeholders; + if (this->is_low_latency_mode_enabled()) { + int one = 1; + setsockopt(socket.get(), IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); +#ifdef __linux__ + setsockopt(socket.get(), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); +#endif + } + if (this->max_number_of_simulataneusly_allowed_clients == 0 || this->get_number_of_connected_clients() <= this->max_number_of_simulataneusly_allowed_clients) { this->client_listener(tcp_client(std::bind(&tcp_server_socket::client_destructed_cb, this, _1), auto_fd(client_fd_raw), address, port)); } else { diff --git a/src/net/tcp_server_socket.hpp b/src/net/tcp_server_socket.hpp index ffc27579a73386a9804c6fb1cf9aeafda0fa1c0d..ed8222aa3259dafd8c1ee4eec2bb1f51cd9e8ff2 100644 --- a/src/net/tcp_server_socket.hpp +++ b/src/net/tcp_server_socket.hpp @@ -35,6 +35,7 @@ private: incoming_client_listener_type overflow_client_listener; std::atomic_uint32_t number_of_connected_clients; unsigned int max_number_of_simulataneusly_allowed_clients; + bool low_latency = false; public: /** @@ -81,6 +82,24 @@ public: */ void set_maximum_concurrent_connections(unsigned int max_connections); + /** + * Enable TCP low latency mode. It disables Nagle's algorithm on all platforms and + * furthermore quickacks on linux. Please note that this will increase traffic. + * @brief Enable or disable TCP low latency mode. + * @param enabled enable or disable fast package dispatching. + */ + inline void set_low_latency_mode(bool enabled) { + this->low_latency = enabled; + } + + /** + * @brief Get the current low latency mode flag + * @return True if the socket is in low latency mode. Otherwise false. + */ + inline bool is_low_latency_mode_enabled() { + return this->low_latency; + } + private: void await_raw_socket_incomming(async_server_socket::self_ptr_type ass, const auto_fd& socket); void client_destructed_cb(exit_status_t exit_status);