Skip to content

Commit

Permalink
Set TCP_QUICKACK (and also TCP_NODELAY).
Browse files Browse the repository at this point in the history
  • Loading branch information
madars committed Apr 5, 2024
1 parent 84db009 commit 5b45369
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/util/network/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <array>
#include <cstring>
#include <unistd.h>
#include <netinet/tcp.h> // for TCP_NODELAY, TCP_QUICKACK

namespace cbdc::network {
auto tcp_socket::connect(const endpoint_t& ep) -> bool {
Expand Down Expand Up @@ -36,6 +37,10 @@ namespace cbdc::network {
continue;
}

static constexpr int one = 1;
setsockopt(m_sock_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); // sending side. needed?
setsockopt(m_sock_fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); // receiving side

break;
}

Expand Down Expand Up @@ -88,13 +93,18 @@ namespace cbdc::network {
}

auto tcp_socket::receive(buffer& pkt) const -> bool {
static constexpr int one = 1;
// apparently TCP_QUICKACK needs to be re-set after each read (incurring a syscall...)
// cf. https://github.com/netty/netty/issues/13610

uint64_t pkt_sz{};
std::array<std::byte, sizeof(pkt_sz)> sz_buf{};
uint64_t total_read{0};
while(total_read != sz_buf.size()) {
auto n = read(m_sock_fd,
&sz_buf.at(total_read),
sizeof(pkt_sz) - total_read);
setsockopt(m_sock_fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); // receiving side
if(n <= 0) {
return false;
}
Expand All @@ -109,6 +119,7 @@ namespace cbdc::network {
while(total_read < pkt_sz) {
const auto buf_sz = pkt_sz - total_read;
auto n = read(m_sock_fd, buf.data(), buf_sz);
setsockopt(m_sock_fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); // receiving side
if(n <= 0) {
return false;
}
Expand Down

0 comments on commit 5b45369

Please sign in to comment.