From 725f06962192a1c0191535e00cd6f825caef122a Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 11 Jan 2018 18:38:29 -0800 Subject: [PATCH] ircd::net: Add discard_all() convenience. --- include/ircd/net/read.h | 3 +++ ircd/net.cc | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/ircd/net/read.h b/include/ircd/net/read.h index 77820ed95..14ce68871 100644 --- a/include/ircd/net/read.h +++ b/include/ircd/net/read.h @@ -39,6 +39,9 @@ namespace ircd::net // Alias to read_any(); size_t read(socket &, const vector_view &); size_t read(socket &, const mutable_buffer &); + + // Yields until len has been discarded + size_t discard_all(socket &, const size_t &len); } /// Alias to read_any(); diff --git a/ircd/net.cc b/ircd/net.cc index 605125558..fc717fe2b 100644 --- a/ircd/net.cc +++ b/ircd/net.cc @@ -196,6 +196,29 @@ ircd::net::write_one(socket &socket, // net/read.h // +/// Yields ircd::ctx until len bytes have been received and discarded from the +/// socket. +/// +size_t +ircd::net::discard_all(socket &socket, + const size_t &len) +{ + static char buffer[512] alignas(16); + + size_t remain{len}; while(remain) + { + const mutable_buffer mb + { + buffer, std::min(remain, sizeof(buffer)) + }; + + __builtin_prefetch(data(mb), 1, 0); // 1 = write, 0 = no cache + read_all(socket, mb); + } + + return len; +} + /// Yields ircd::ctx until buffers are full. /// /// Use this only if the following are true: