Remove byte counters from sockets

This commit is contained in:
Patrick Nicolas
2025-11-11 21:12:01 +01:00
parent ffd5cf882a
commit efde4bf247
4 changed files with 51 additions and 53 deletions
+18 -22
View File
@@ -266,8 +266,6 @@ std::pair<wivrn::deserialization_packet, sockaddr_in6> wivrn::UDP::receive_from_
if (received < 0)
throw std::system_error{errno, std::generic_category()};
bytes_received_ += received;
std::span message{buffer.get(), (size_t)received};
if (encrypted)
@@ -346,8 +344,6 @@ wivrn::deserialization_packet wivrn::UDP::receive_raw()
for (int i = received - 1; i >= 0; --i)
{
bytes_received_ += mmsgs[i].msg_len;
std::span<uint8_t> message{(uint8_t *)iovecs[i].iov_base, mmsgs[i].msg_len};
assert(message.data() != nullptr);
@@ -376,7 +372,7 @@ wivrn::deserialization_packet wivrn::UDP::receive_raw()
__builtin_unreachable();
}
void wivrn::UDP::send_raw(serialization_packet && packet)
size_t wivrn::UDP::send_raw(serialization_packet && packet)
{
thread_local std::vector<iovec> iovecs;
iovecs.clear();
@@ -399,23 +395,21 @@ void wivrn::UDP::send_raw(serialization_packet && packet)
}
for (const auto & span: data)
{
iovecs.emplace_back(span.data(), span.size());
bytes_sent_ += span.size();
}
if (::writev(fd, iovecs.data(), iovecs.size()) < 0)
throw std::system_error{errno, std::generic_category()};
if (ssize_t sent = ::writev(fd, iovecs.data(), iovecs.size()); sent >= 0)
return sent;
throw std::system_error{errno, std::generic_category()};
}
void wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
size_t wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
{
thread_local std::vector<iovec> iovecs;
thread_local std::vector<mmsghdr> mmsgs;
thread_local std::vector<uint64_t> iv_counters;
if (packets.empty())
return;
return 0;
iovecs.clear();
mmsgs.clear();
@@ -423,6 +417,7 @@ void wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
iv_counters.reserve(packets.size());
size_t sent = 0;
for (serialization_packet & packet: packets)
{
std::vector<std::span<uint8_t>> & data = packet;
@@ -444,7 +439,7 @@ void wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
for (const auto & span: data)
{
iovecs.emplace_back(span.data(), span.size_bytes());
bytes_sent_ += span.size();
sent += span.size();
}
if (encrypted)
@@ -462,6 +457,7 @@ void wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
// sendmmsg may not send all messages, just consider them as lost for UDP
if (sendmmsg(fd, mmsgs.data(), mmsgs.size(), 0) < 0)
throw std::system_error{errno, std::generic_category()};
return sent;
}
wivrn::deserialization_packet wivrn::TCP::receive_raw()
@@ -503,8 +499,6 @@ wivrn::deserialization_packet wivrn::TCP::receive_raw()
if (received_size == 0)
throw socket_shutdown{};
bytes_received_ += received_size;
if (decrypter)
{
std::span<uint8_t> received_data{&*data.end(), (size_t)received_size};
@@ -547,8 +541,9 @@ wivrn::deserialization_packet wivrn::TCP::receive_pending()
return deserialization_packet{buffer, span};
}
void wivrn::TCP::send_raw(serialization_packet && packet)
size_t wivrn::TCP::send_raw(serialization_packet && packet)
{
size_t total_sent = 0;
thread_local std::vector<iovec> iovecs;
iovecs.clear();
@@ -589,7 +584,7 @@ void wivrn::TCP::send_raw(serialization_packet && packet)
if (sent < 0)
throw std::system_error{errno, std::generic_category()};
bytes_sent_ += sent;
total_sent += sent;
// iov fully consumed
while (hdr.msg_iovlen > 0 and sent >= hdr.msg_iov[0].iov_len)
@@ -599,20 +594,20 @@ void wivrn::TCP::send_raw(serialization_packet && packet)
--hdr.msg_iovlen;
}
if (hdr.msg_iovlen == 0)
return;
return total_sent;
hdr.msg_iov[0].iov_base = (void *)((uintptr_t)hdr.msg_iov[0].iov_base + sent);
hdr.msg_iov[0].iov_len -= sent;
}
}
void wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
size_t wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
{
thread_local std::vector<iovec> iovecs;
thread_local std::vector<uint32_t> sizes;
thread_local std::vector<std::span<uint8_t>> spans;
if (packets.empty())
return;
return 0;
iovecs.clear();
sizes.clear();
@@ -652,6 +647,7 @@ void wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
encrypter.encrypt_in_place(spans);
}
size_t total_sent = 0;
while (true)
{
ssize_t sent = ::sendmsg(fd, &hdr, MSG_NOSIGNAL);
@@ -662,7 +658,7 @@ void wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
if (sent < 0)
throw std::system_error{errno, std::generic_category()};
bytes_sent_ += sent;
total_sent += sent;
// iov fully consumed
while (hdr.msg_iovlen > 0 and sent >= hdr.msg_iov[0].iov_len)
@@ -672,7 +668,7 @@ void wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
--hdr.msg_iovlen;
}
if (hdr.msg_iovlen == 0)
return;
return total_sent;
hdr.msg_iov[0].iov_base = (void *)((uintptr_t)hdr.msg_iov[0].iov_base + sent);
hdr.msg_iov[0].iov_len -= sent;
}