Receive larger buffers for TCP socket

This commit is contained in:
Patrick Nicolas
2024-08-31 14:34:36 +02:00
parent 0aa5284165
commit ca1f478299
4 changed files with 71 additions and 19 deletions
+5
View File
@@ -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<T>(visitor), std::move(*packet));
while (auto packet = control.receive_pending())
std::visit(std::forward<T>(visitor), std::move(*packet));
int r = ::poll(fds, std::size(fds), timeout.count());
if (r < 0)
throw std::system_error(errno, std::system_category());
+45 -18
View File
@@ -316,26 +316,34 @@ void xrt::drivers::wivrn::UDP::send_raw(const std::vector<std::span<uint8_t>> &
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<uint16_t *>(buffer.get());
expected_size = payload_size + sizeof(uint16_t) - buffer_size;
uint32_t payload_size = *reinterpret_cast<uint16_t *>(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<size_t>(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<uint8_t[]>(buffer_size + expected_size);
buffer = std::make_shared_for_overwrite<uint8_t[]>(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<uint16_t *>(buffer.get());
if (buffer_size < sizeof(uint16_t) + payload_size)
uint32_t payload_size = *reinterpret_cast<uint16_t *>(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<uint16_t *>(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<std::span<uint8_t>> & spans)
+16 -1
View File
@@ -105,6 +105,10 @@ public:
explicit UDP(int fd);
deserialization_packet receive_raw();
deserialization_packet receive_pending()
{
return {};
};
std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
void send_raw(const std::vector<uint8_t> & data);
void send_raw(const std::vector<std::span<uint8_t>> & data);
@@ -122,7 +126,8 @@ public:
class TCP : public fd_base
{
std::shared_ptr<uint8_t[]> buffer;
size_t buffer_size = 0;
ssize_t capacity_left = 0;
std::span<uint8_t> data;
std::unique_ptr<std::mutex> 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<std::span<uint8_t>> & data);
};
@@ -186,6 +192,15 @@ public:
Socket(std::forward<Args>(args)...)
{}
std::optional<ReceivedType> receive_pending()
{
deserialization_packet packet = ((Socket *)this)->receive_pending();
if (packet.empty())
return {};
return packet.deserialize<ReceivedType>();
}
std::optional<ReceivedType> receive()
{
deserialization_packet packet = this->receive_raw();
+5
View File
@@ -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<T>(visitor), std::move(*packet));
while (auto packet = control.receive_pending())
std::visit(std::forward<T>(visitor), std::move(*packet));
int r = ::poll(fds, std::size(fds), timeout);
if (r < 0)
throw std::system_error(errno, std::system_category());