• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Razakhel / RaZ / 18755655914

23 Oct 2025 04:45PM UTC coverage: 73.848% (-0.2%) from 74.018%
18755655914

push

github

Razakhel
[Network/UdpClient] Added a UDP client class

7 of 37 new or added lines in 2 files covered. (18.92%)

8446 of 11437 relevant lines covered (73.85%)

1728.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/RaZ/Network/UdpClient.cpp
1
#include "RaZ/Network/UdpClient.hpp"
2
#include "RaZ/Utils/Logger.hpp"
3

4
#include "asio/ip/udp.hpp"
5

6
namespace Raz {
7

8
struct UdpClient::Impl {
NEW
9
  Impl() : socket(context, asio::ip::udp::endpoint(asio::ip::udp::v4(), 0)) {}
×
10

11
  asio::io_context context;
12
  asio::ip::udp::socket socket;
13
  asio::ip::udp::endpoint serverEndpoint;
14
  std::array<char, 1024> data {};
15
};
16

NEW
17
UdpClient::UdpClient() : m_impl{ std::make_unique<Impl>() } {}
×
18

NEW
19
void UdpClient::setDestination(const std::string& host, unsigned short port) {
×
NEW
20
  asio::error_code error;
×
NEW
21
  asio::ip::udp::resolver resolver(m_impl->context);
×
NEW
22
  const asio::ip::udp::resolver::results_type endpoints = resolver.resolve(asio::ip::udp::v4(), host, std::to_string(port), error);
×
23

NEW
24
  if (error)
×
NEW
25
    throw std::invalid_argument(std::format("[UdpClient] Failed to set destination to {}:{}: {}", host, port, error.message()));
×
26

NEW
27
  m_impl->serverEndpoint = std::move(*endpoints.begin());
×
NEW
28
}
×
29

NEW
30
void UdpClient::send(const std::string& data) {
×
NEW
31
  Logger::debug("[UdpClient] Sending '{}'...", data);
×
NEW
32
  m_impl->socket.send_to(asio::buffer(data), m_impl->serverEndpoint);
×
NEW
33
}
×
34

NEW
35
std::size_t UdpClient::recoverAvailableByteCount() {
×
NEW
36
  asio::detail::io_control::bytes_readable command(true);
×
NEW
37
  m_impl->socket.io_control(command);
×
NEW
38
  return command.get();
×
39
}
40

NEW
41
std::string UdpClient::receive() {
×
NEW
42
  std::array<char, 1024> buffer {};
×
NEW
43
  asio::ip::udp::endpoint senderEndpoint;
×
NEW
44
  const size_t length = m_impl->socket.receive_from(asio::buffer(buffer), senderEndpoint);
×
NEW
45
  Logger::debug("[UdpClient] Received data from {}: {}", senderEndpoint.address().to_string(), std::string_view(buffer.data(), length));
×
NEW
46
  return std::string(buffer.data(), length);
×
47
}
48

NEW
49
void UdpClient::close() {
×
NEW
50
  Logger::debug("[UdpClient] Closing...");
×
51
  // shutdown() shouldn't be called on UDP sockets
NEW
52
  m_impl->socket.close();
×
NEW
53
  Logger::debug("[UdpClient] Closed");
×
NEW
54
}
×
55

NEW
56
UdpClient::~UdpClient() = default;
×
57

58
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc