Remove byte counters from sockets
This commit is contained in:
+14
-9
@@ -46,6 +46,9 @@ private:
|
||||
control_socket_t control;
|
||||
stream_socket_t stream;
|
||||
|
||||
std::atomic<uint64_t> bytes_sent_ = 0;
|
||||
std::atomic<uint64_t> bytes_received_ = 0;
|
||||
|
||||
template <typename T>
|
||||
void handshake(T address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
|
||||
|
||||
@@ -60,16 +63,16 @@ public:
|
||||
template <typename T>
|
||||
void send_control(T && packet)
|
||||
{
|
||||
control.send(std::forward<T>(packet));
|
||||
bytes_sent_ += control.send(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void send_stream(T && packet)
|
||||
{
|
||||
if (stream)
|
||||
stream.send(std::forward<T>(packet));
|
||||
bytes_sent_ += stream.send(std::forward<T>(packet));
|
||||
else
|
||||
control.send(std::forward<T>(packet));
|
||||
bytes_sent_ += control.send(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -81,9 +84,9 @@ public:
|
||||
fds[1].events = POLLIN;
|
||||
fds[1].fd = control.get_fd();
|
||||
|
||||
while (auto packet = stream.receive_pending())
|
||||
while (auto packet = stream.receive_pending(&bytes_received_))
|
||||
std::visit(std::forward<T>(visitor), std::move(*packet));
|
||||
while (auto packet = control.receive_pending())
|
||||
while (auto packet = control.receive_pending(&bytes_received_))
|
||||
std::visit(std::forward<T>(visitor), std::move(*packet));
|
||||
|
||||
int r = ::poll(fds, std::size(fds), timeout.count());
|
||||
@@ -98,14 +101,16 @@ public:
|
||||
|
||||
if (fds[0].revents & POLLIN)
|
||||
{
|
||||
auto packet = stream.receive();
|
||||
auto packet = stream.receive(&bytes_received_);
|
||||
if (packet)
|
||||
{
|
||||
std::visit(std::forward<T>(visitor), std::move(*packet));
|
||||
}
|
||||
}
|
||||
|
||||
if (fds[1].revents & POLLIN)
|
||||
{
|
||||
auto packet = control.receive();
|
||||
auto packet = control.receive(&bytes_received_);
|
||||
if (packet)
|
||||
std::visit(std::forward<T>(visitor), std::move(*packet));
|
||||
}
|
||||
@@ -115,11 +120,11 @@ public:
|
||||
|
||||
uint64_t bytes_received() const
|
||||
{
|
||||
return control.bytes_received() + stream.bytes_received();
|
||||
return bytes_received_;
|
||||
}
|
||||
|
||||
uint64_t bytes_sent() const
|
||||
{
|
||||
return control.bytes_sent() + stream.bytes_sent();
|
||||
return bytes_sent_;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -288,6 +288,11 @@ public:
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t wire_size()
|
||||
{
|
||||
return initial_buffer.size_bytes();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deserialize(T & v)
|
||||
{
|
||||
|
||||
+18
-22
@@ -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;
|
||||
}
|
||||
|
||||
+14
-22
@@ -68,8 +68,6 @@ protected:
|
||||
int fd = -1;
|
||||
|
||||
fd_base(const fd_base &) = delete;
|
||||
std::atomic<uint64_t> bytes_sent_ = 0;
|
||||
std::atomic<uint64_t> bytes_received_ = 0;
|
||||
|
||||
public:
|
||||
fd_base() = default;
|
||||
@@ -88,16 +86,6 @@ public:
|
||||
{
|
||||
return fd != -1;
|
||||
}
|
||||
|
||||
uint64_t bytes_sent() const
|
||||
{
|
||||
return bytes_sent_;
|
||||
}
|
||||
|
||||
uint64_t bytes_received() const
|
||||
{
|
||||
return bytes_received_;
|
||||
}
|
||||
};
|
||||
|
||||
class UDP : public fd_base
|
||||
@@ -122,8 +110,8 @@ public:
|
||||
deserialization_packet receive_raw();
|
||||
deserialization_packet receive_pending();
|
||||
std::pair<wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
|
||||
void send_raw(serialization_packet && packet);
|
||||
void send_many_raw(std::span<serialization_packet> packets);
|
||||
size_t send_raw(serialization_packet && packet);
|
||||
size_t send_many_raw(std::span<serialization_packet> packets);
|
||||
|
||||
void connect(in6_addr address, int port);
|
||||
void connect(in_addr address, int port);
|
||||
@@ -157,8 +145,8 @@ public:
|
||||
|
||||
deserialization_packet receive_raw();
|
||||
deserialization_packet receive_pending();
|
||||
void send_raw(serialization_packet && packet);
|
||||
void send_many_raw(std::span<serialization_packet> packets);
|
||||
size_t send_raw(serialization_packet && packet);
|
||||
size_t send_many_raw(std::span<serialization_packet> packets);
|
||||
|
||||
void set_aes_key_and_ivs(std::span<std::uint8_t, 16> key, std::span<std::uint8_t, 16> recv_iv, std::span<std::uint8_t, 16> send_iv);
|
||||
};
|
||||
@@ -220,20 +208,24 @@ public:
|
||||
Socket(std::forward<Args>(args)...)
|
||||
{}
|
||||
|
||||
std::optional<ReceivedType> receive_pending()
|
||||
std::optional<ReceivedType> receive_pending(std::atomic<uint64_t> * size = nullptr)
|
||||
{
|
||||
deserialization_packet packet = ((Socket *)this)->receive_pending();
|
||||
if (packet.empty())
|
||||
return {};
|
||||
if (size)
|
||||
size->fetch_add(packet.wire_size());
|
||||
|
||||
return packet.deserialize<ReceivedType>();
|
||||
}
|
||||
|
||||
std::optional<ReceivedType> receive()
|
||||
std::optional<ReceivedType> receive(std::atomic<uint64_t> * size = nullptr)
|
||||
{
|
||||
deserialization_packet packet = this->receive_raw();
|
||||
if (packet.empty())
|
||||
return {};
|
||||
if (size)
|
||||
size->fetch_add(packet.wire_size());
|
||||
|
||||
return packet.deserialize<ReceivedType>();
|
||||
}
|
||||
@@ -249,16 +241,16 @@ public:
|
||||
}
|
||||
|
||||
template <details::not_lvalue_reference T>
|
||||
void send(T && data)
|
||||
size_t send(T && data)
|
||||
{
|
||||
thread_local serialization_packet p;
|
||||
serialize(p, data);
|
||||
this->send_raw(std::move(p));
|
||||
return this->send_raw(std::move(p));
|
||||
}
|
||||
|
||||
void send(std::span<serialization_packet> packets)
|
||||
size_t send(std::span<serialization_packet> packets)
|
||||
{
|
||||
this->send_many_raw(packets);
|
||||
return this->send_many_raw(packets);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user