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

Razakhel / RaZ / 18755092380

23 Oct 2025 04:08PM UTC coverage: 74.018% (-0.3%) from 74.289%
18755092380

push

github

Razakhel
[Network/UdpServer] Added a UDP server class

4 of 43 new or added lines in 2 files covered. (9.3%)

3 existing lines in 1 file now uncovered.

8438 of 11400 relevant lines covered (74.02%)

1734.29 hits per line

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

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

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

6
namespace Raz {
7

8
struct UdpServer::Impl {
NEW
9
  Impl() : socket(context) {}
×
10

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

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

NEW
19
void UdpServer::start(unsigned short port) {
×
NEW
20
  Logger::debug("[UdpServer] Starting on port {}...", port);
×
21

NEW
22
  setup(port);
×
NEW
23
  receive();
×
24

NEW
25
  m_impl->context.run();
×
NEW
26
}
×
27

NEW
28
void UdpServer::stop() {
×
NEW
29
  Logger::debug("[UdpServer] Stopping...");
×
30
  // shutdown() shouldn't be called on UDP sockets
NEW
31
  m_impl->socket.close();
×
NEW
32
  m_impl->context.stop();
×
NEW
33
  Logger::debug("[UdpServer] Stopped");
×
NEW
34
}
×
35

NEW
36
UdpServer::~UdpServer() = default;
×
37

NEW
38
void UdpServer::setup(unsigned short port) {
×
NEW
39
  if (m_impl->context.stopped())
×
NEW
40
    m_impl->context.restart();
×
41

NEW
42
  const asio::ip::udp::endpoint endpoint(asio::ip::udp::v4(), port);
×
NEW
43
  m_impl->socket.open(endpoint.protocol());
×
NEW
44
  m_impl->socket.set_option(asio::socket_base::reuse_address(true));
×
NEW
45
  m_impl->socket.bind(endpoint);
×
NEW
46
}
×
47

NEW
48
void UdpServer::receive() {
×
NEW
49
  m_impl->socket.async_receive_from(asio::buffer(m_impl->data), m_impl->senderEndpoint, [this] (const asio::error_code& error, std::size_t bytesReceived) {
×
NEW
50
    if (error == asio::error::interrupted || error == asio::error::operation_aborted)
×
NEW
51
      return; // Server closed
×
52

NEW
53
    if (error) {
×
NEW
54
      Logger::error("[UdpServer] Error while receiving data: {}", error.message());
×
55
    } else {
NEW
56
      Logger::debug("[UdpServer] Received data from {}: {}", m_impl->senderEndpoint.address().to_string(), std::string_view(m_impl->data.data(), bytesReceived));
×
NEW
57
      echo(bytesReceived); // Replying with the same received data
×
58
    }
59

NEW
60
    receive();
×
61
  });
NEW
62
}
×
63

NEW
64
void UdpServer::echo(std::size_t length) {
×
NEW
65
  m_impl->socket.async_send_to(asio::buffer(m_impl->data, length), m_impl->senderEndpoint, [] (const asio::error_code& error, std::size_t) {
×
NEW
66
    if (error)
×
NEW
67
      Logger::error("[UdpServer] Error while echoing: {}", error.message());
×
NEW
68
  });
×
NEW
69
}
×
70

71
} // namespace Raz
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

© 2025 Coveralls, Inc