Use recvmmsg for UDP socket
This commit is contained in:
+55
-10
@@ -275,23 +275,68 @@ std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> xrt::driver
|
|||||||
return {deserialization_packet{std::move(buffer), std::span(tmp, received)}, addr};
|
return {deserialization_packet{std::move(buffer), std::span(tmp, received)}, addr};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::UDP::receive_pending()
|
||||||
|
{
|
||||||
|
if (messages.empty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto span = messages.back();
|
||||||
|
messages.pop_back();
|
||||||
|
return deserialization_packet{buffer, span};
|
||||||
|
}
|
||||||
|
|
||||||
xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::UDP::receive_raw()
|
xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::UDP::receive_raw()
|
||||||
{
|
{
|
||||||
size_t size = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
|
if (not messages.empty())
|
||||||
#if __cpp_lib_smart_ptr_for_overwrite >= 202002L
|
{
|
||||||
auto buffer = std::make_shared_for_overwrite<uint8_t[]>(size);
|
auto span = messages.back();
|
||||||
#else
|
messages.pop_back();
|
||||||
std::shared_ptr<uint8_t[]> buffer(new uint8_t[size]);
|
return deserialization_packet{buffer, span};
|
||||||
#endif
|
}
|
||||||
|
|
||||||
|
static const size_t message_size = 2048;
|
||||||
|
static const size_t num_messages = 20;
|
||||||
|
#if __cpp_lib_smart_ptr_for_overwrite >= 202002L
|
||||||
|
buffer = std::make_shared_for_overwrite<uint8_t[]>(message_size * num_messages);
|
||||||
|
#else
|
||||||
|
buffer.reset(new uint8_t[message_size * num_messages]);
|
||||||
|
#endif
|
||||||
|
std::vector<iovec> iovecs;
|
||||||
|
std::vector<mmsghdr> mmsgs;
|
||||||
|
iovecs.reserve(num_messages);
|
||||||
|
mmsgs.reserve(num_messages);
|
||||||
|
for (size_t i = 0; i < num_messages; ++i)
|
||||||
|
{
|
||||||
|
iovecs.push_back({
|
||||||
|
.iov_base = buffer.get() + message_size * i,
|
||||||
|
.iov_len = message_size,
|
||||||
|
});
|
||||||
|
mmsgs.push_back(
|
||||||
|
{
|
||||||
|
.msg_hdr = {
|
||||||
|
.msg_iov = &iovecs.back(),
|
||||||
|
.msg_iovlen = 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
int received = recvmmsg(fd, mmsgs.data(), num_messages, MSG_DONTWAIT, nullptr);
|
||||||
|
|
||||||
ssize_t received = recv(fd, buffer.get(), size, 0);
|
|
||||||
if (received < 0)
|
if (received < 0)
|
||||||
throw std::system_error{errno, std::generic_category()};
|
throw std::system_error{errno, std::generic_category()};
|
||||||
|
if (received == 0)
|
||||||
|
throw socket_shutdown();
|
||||||
|
|
||||||
bytes_received_ += received;
|
messages.reserve(received);
|
||||||
|
for (int i = received - 1; i > 0; --i)
|
||||||
|
{
|
||||||
|
messages.emplace_back(
|
||||||
|
(uint8_t *)iovecs[i].iov_base,
|
||||||
|
mmsgs[i].msg_len);
|
||||||
|
bytes_received_ += mmsgs[i].msg_len;
|
||||||
|
}
|
||||||
|
|
||||||
auto tmp = buffer.get();
|
return deserialization_packet{buffer, std::span(buffer.get(), mmsgs[0].msg_len)};
|
||||||
return deserialization_packet{std::move(buffer), std::span(tmp, received)};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void xrt::drivers::wivrn::UDP::send_raw(const std::vector<uint8_t> & data)
|
void xrt::drivers::wivrn::UDP::send_raw(const std::vector<uint8_t> & data)
|
||||||
|
|||||||
@@ -100,15 +100,15 @@ public:
|
|||||||
|
|
||||||
class UDP : public fd_base
|
class UDP : public fd_base
|
||||||
{
|
{
|
||||||
|
std::shared_ptr<uint8_t[]> buffer;
|
||||||
|
std::vector<std::span<uint8_t>> messages;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
UDP();
|
UDP();
|
||||||
explicit UDP(int fd);
|
explicit UDP(int fd);
|
||||||
|
|
||||||
deserialization_packet receive_raw();
|
deserialization_packet receive_raw();
|
||||||
deserialization_packet receive_pending()
|
deserialization_packet receive_pending();
|
||||||
{
|
|
||||||
return {};
|
|
||||||
};
|
|
||||||
std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
|
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<uint8_t> & data);
|
||||||
void send_raw(const std::vector<std::span<uint8_t>> & data);
|
void send_raw(const std::vector<std::span<uint8_t>> & data);
|
||||||
|
|||||||
Reference in New Issue
Block a user