diff --git a/client/wivrn_client.h b/client/wivrn_client.h index f12ad112..fb6ee91d 100644 --- a/client/wivrn_client.h +++ b/client/wivrn_client.h @@ -64,6 +64,11 @@ public: fds[1].events = POLLIN; fds[1].fd = control.get_fd(); + while (auto packet = stream.receive_pending()) + std::visit(std::forward(visitor), std::move(*packet)); + while (auto packet = control.receive_pending()) + std::visit(std::forward(visitor), std::move(*packet)); + int r = ::poll(fds, std::size(fds), timeout.count()); if (r < 0) throw std::system_error(errno, std::system_category()); diff --git a/common/wivrn_sockets.cpp b/common/wivrn_sockets.cpp index 4b75216d..68875d3a 100644 --- a/common/wivrn_sockets.cpp +++ b/common/wivrn_sockets.cpp @@ -316,26 +316,34 @@ void xrt::drivers::wivrn::UDP::send_raw(const std::vector> & xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::TCP::receive_raw() { - size_t expected_size; + ssize_t expected_size; - if (buffer_size < sizeof(uint16_t)) + if (data.size_bytes() < sizeof(uint16_t)) { - expected_size = sizeof(uint16_t) - buffer_size; + expected_size = sizeof(uint16_t) - data.size_bytes(); } else { - uint32_t payload_size = *reinterpret_cast(buffer.get()); - expected_size = payload_size + sizeof(uint16_t) - buffer_size; + uint32_t payload_size = *reinterpret_cast(data.data()); + expected_size = payload_size + sizeof(uint16_t) - data.size_bytes(); } - auto old = std::move(buffer); + if (expected_size > capacity_left) + { + size_t new_size = std::max(data.size_bytes() + expected_size, + 4096); + auto old = std::move(buffer); #if __cpp_lib_smart_ptr_for_overwrite >= 202002L - buffer = std::make_shared_for_overwrite(buffer_size + expected_size); + buffer = std::make_shared_for_overwrite(new_size); #else - buffer.reset(new uint8_t[buffer_size + expected_size]); + buffer.reset(new uint8_t[new_size]); #endif + memcpy(buffer.get(), data.data(), data.size_bytes()); + data = std::span(buffer.get(), data.size()); + capacity_left = new_size - data.size_bytes(); + } - ssize_t received = recv(fd, buffer.get() + buffer_size, expected_size, MSG_DONTWAIT); + ssize_t received = recv(fd, &*data.end(), capacity_left, MSG_DONTWAIT); if (received < 0) throw std::system_error{errno, std::generic_category()}; @@ -345,19 +353,38 @@ xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::TCP::receive_ra bytes_received_ += received; - memcpy(buffer.get(), old.get(), buffer_size); - buffer_size += received; - if (buffer_size < sizeof(uint16_t)) + data = std::span(data.data(), data.size() + received); + capacity_left -= received; + if (data.size_bytes() < sizeof(uint16_t)) return {}; - uint32_t payload_size = *reinterpret_cast(buffer.get()); - if (buffer_size < sizeof(uint16_t) + payload_size) + uint32_t payload_size = *reinterpret_cast(data.data()); + if (payload_size == 0) + throw std::runtime_error("Invalid packet: 0 size"); + + if (data.size_bytes() < sizeof(uint16_t) + payload_size) return {}; - assert(buffer_size == sizeof(uint16_t) + payload_size); - auto span = std::span(buffer.get() + sizeof(uint16_t), buffer_size); - buffer_size = 0; - return deserialization_packet{std::move(buffer), span}; + auto span = data.subspan(sizeof(uint16_t), payload_size); + data = data.subspan(sizeof(uint16_t) + payload_size); + return deserialization_packet{buffer, span}; +} + +xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::TCP::receive_pending() +{ + if (data.size_bytes() < sizeof(uint16_t)) + return {}; + + uint32_t payload_size = *reinterpret_cast(data.data()); + if (payload_size == 0) + throw std::runtime_error("Invalid packet: 0 size"); + + if (data.size_bytes() < sizeof(uint16_t) + payload_size) + return {}; + + auto span = data.subspan(sizeof(uint16_t), payload_size); + data = data.subspan(sizeof(uint16_t) + payload_size); + return deserialization_packet{buffer, span}; } void xrt::drivers::wivrn::TCP::send_raw(const std::vector> & spans) diff --git a/common/wivrn_sockets.h b/common/wivrn_sockets.h index dcaef6c8..1e1a6950 100644 --- a/common/wivrn_sockets.h +++ b/common/wivrn_sockets.h @@ -105,6 +105,10 @@ public: explicit UDP(int fd); deserialization_packet receive_raw(); + deserialization_packet receive_pending() + { + return {}; + }; std::pair receive_from_raw(); void send_raw(const std::vector & data); void send_raw(const std::vector> & data); @@ -122,7 +126,8 @@ public: class TCP : public fd_base { std::shared_ptr buffer; - size_t buffer_size = 0; + ssize_t capacity_left = 0; + std::span data; std::unique_ptr mutex; void init(); @@ -133,6 +138,7 @@ public: explicit TCP(int fd); deserialization_packet receive_raw(); + deserialization_packet receive_pending(); void send_raw(const std::vector> & data); }; @@ -186,6 +192,15 @@ public: Socket(std::forward(args)...) {} + std::optional receive_pending() + { + deserialization_packet packet = ((Socket *)this)->receive_pending(); + if (packet.empty()) + return {}; + + return packet.deserialize(); + } + std::optional receive() { deserialization_packet packet = this->receive_raw(); diff --git a/server/driver/wivrn_connection.h b/server/driver/wivrn_connection.h index d2830352..b7f3f116 100644 --- a/server/driver/wivrn_connection.h +++ b/server/driver/wivrn_connection.h @@ -87,6 +87,11 @@ public: fds[1].events = POLLIN; fds[1].fd = control.get_fd(); + while (auto packet = stream.receive_pending()) + std::visit(std::forward(visitor), std::move(*packet)); + while (auto packet = control.receive_pending()) + std::visit(std::forward(visitor), std::move(*packet)); + int r = ::poll(fds, std::size(fds), timeout); if (r < 0) throw std::system_error(errno, std::system_category());