diff --git a/client/wivrn_client.h b/client/wivrn_client.h index 4ba14f08..3cd0410e 100644 --- a/client/wivrn_client.h +++ b/client/wivrn_client.h @@ -46,6 +46,9 @@ private: control_socket_t control; stream_socket_t stream; + std::atomic bytes_sent_ = 0; + std::atomic bytes_received_ = 0; + template void handshake(T address, bool tcp_only, crypto::key & headset_keypair, std::function pin_enter); @@ -60,16 +63,16 @@ public: template void send_control(T && packet) { - control.send(std::forward(packet)); + bytes_sent_ += control.send(std::forward(packet)); } template void send_stream(T && packet) { if (stream) - stream.send(std::forward(packet)); + bytes_sent_ += stream.send(std::forward(packet)); else - control.send(std::forward(packet)); + bytes_sent_ += control.send(std::forward(packet)); } template @@ -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(visitor), std::move(*packet)); - while (auto packet = control.receive_pending()) + while (auto packet = control.receive_pending(&bytes_received_)) std::visit(std::forward(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(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(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_; } }; diff --git a/common/wivrn_serialization.h b/common/wivrn_serialization.h index 8e451e51..9b8c1ab5 100644 --- a/common/wivrn_serialization.h +++ b/common/wivrn_serialization.h @@ -288,6 +288,11 @@ public: return size; } + size_t wire_size() + { + return initial_buffer.size_bytes(); + } + template void deserialize(T & v) { diff --git a/common/wivrn_sockets.cpp b/common/wivrn_sockets.cpp index 4d4c7c81..b4c881af 100644 --- a/common/wivrn_sockets.cpp +++ b/common/wivrn_sockets.cpp @@ -266,8 +266,6 @@ std::pair 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 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 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 packets) +size_t wivrn::UDP::send_many_raw(std::span packets) { thread_local std::vector iovecs; thread_local std::vector mmsgs; thread_local std::vector iv_counters; if (packets.empty()) - return; + return 0; iovecs.clear(); mmsgs.clear(); @@ -423,6 +417,7 @@ void wivrn::UDP::send_many_raw(std::span packets) iv_counters.reserve(packets.size()); + size_t sent = 0; for (serialization_packet & packet: packets) { std::vector> & data = packet; @@ -444,7 +439,7 @@ void wivrn::UDP::send_many_raw(std::span 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 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 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 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 packets) +size_t wivrn::TCP::send_many_raw(std::span packets) { thread_local std::vector iovecs; thread_local std::vector sizes; thread_local std::vector> spans; if (packets.empty()) - return; + return 0; iovecs.clear(); sizes.clear(); @@ -652,6 +647,7 @@ void wivrn::TCP::send_many_raw(std::span 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 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 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; } diff --git a/common/wivrn_sockets.h b/common/wivrn_sockets.h index ebaeeb04..32aef19a 100644 --- a/common/wivrn_sockets.h +++ b/common/wivrn_sockets.h @@ -68,8 +68,6 @@ protected: int fd = -1; fd_base(const fd_base &) = delete; - std::atomic bytes_sent_ = 0; - std::atomic 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 receive_from_raw(); - void send_raw(serialization_packet && packet); - void send_many_raw(std::span packets); + size_t send_raw(serialization_packet && packet); + size_t send_many_raw(std::span 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 packets); + size_t send_raw(serialization_packet && packet); + size_t send_many_raw(std::span packets); void set_aes_key_and_ivs(std::span key, std::span recv_iv, std::span send_iv); }; @@ -220,20 +208,24 @@ public: Socket(std::forward(args)...) {} - std::optional receive_pending() + std::optional receive_pending(std::atomic * size = nullptr) { deserialization_packet packet = ((Socket *)this)->receive_pending(); if (packet.empty()) return {}; + if (size) + size->fetch_add(packet.wire_size()); return packet.deserialize(); } - std::optional receive() + std::optional receive(std::atomic * size = nullptr) { deserialization_packet packet = this->receive_raw(); if (packet.empty()) return {}; + if (size) + size->fetch_add(packet.wire_size()); return packet.deserialize(); } @@ -249,16 +241,16 @@ public: } template - 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 packets) + size_t send(std::span packets) { - this->send_many_raw(packets); + return this->send_many_raw(packets); } };