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

Razakhel / RaZ / 18631941614

19 Oct 2025 02:41PM UTC coverage: 74.263% (-0.03%) from 74.289%
18631941614

push

github

Razakhel
[CI] Added a step to deploy nightly releases

8434 of 11357 relevant lines covered (74.26%)

1740.86 hits per line

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

89.29
/src/RaZ/Network/TcpServer.cpp
1
#include "RaZ/Network/TcpServer.hpp"
2
#include "RaZ/Utils/Logger.hpp"
3

4
#include "asio/ip/tcp.hpp"
5
#include "asio/write.hpp"
6

7
#include <thread>
8

9
namespace Raz {
10

11
namespace {
12

13
class TcpSession : public std::enable_shared_from_this<TcpSession> {
14
public:
15
  explicit TcpSession(asio::ip::tcp::socket socket) noexcept : m_socket{ std::move(socket) } {}
1✔
16

17
  void run() {
1✔
18
    Logger::debug("[TcpSession] Connected to {}", m_socket.remote_endpoint().address().to_string());
1✔
19
    receive();
1✔
20
  }
1✔
21

22
private:
23
  void receive() {
3✔
24
    m_socket.async_read_some(asio::buffer(m_data), [self = shared_from_this()] (const asio::error_code& error, std::size_t length) {
3✔
25
      if (error == asio::error::eof || error == asio::error::connection_reset) {
2✔
26
        Logger::debug("[TcpSession] Connection with {} closed", self->m_socket.remote_endpoint().address().to_string());
×
27
        return;
×
28
      }
29

30
      if (error) {
2✔
31
        Logger::error("[TcpSession] Error while receiving data: {}", error.message());
×
32
      } else {
33
        Logger::debug("[TcpSession] Received: {}", std::string_view(self->m_data.data(), length));
4✔
34
        self->echo(length); // Replying with the same received data
2✔
35
      }
36

37
      self->receive();
2✔
38
    });
39
  }
3✔
40

41
  void echo(std::size_t length) {
2✔
42
    asio::async_write(m_socket, asio::buffer(m_data, length), [] (const asio::error_code& error, std::size_t) {
2✔
43
      if (error)
2✔
44
        Logger::error("[TcpSession] Error while echoing: {}", error.message());
×
45
    });
2✔
46
  }
2✔
47

48
  asio::ip::tcp::socket m_socket;
49
  std::array<char, 1024> m_data {};
50
};
51

52
} // namespace
53

54
struct TcpServer::Impl {
55
  Impl() : acceptor(context) {}
2✔
56

57
  asio::io_context context;
58
  asio::ip::tcp::acceptor acceptor;
59
};
60

61
TcpServer::TcpServer() : m_impl{ std::make_unique<Impl>() } {}
2✔
62

63
void TcpServer::start(unsigned short port) {
2✔
64
  Logger::debug("[TcpServer] Starting on port {}...", port);
2✔
65

66
  setup(port);
2✔
67
  accept();
2✔
68

69
  m_impl->context.run();
2✔
70
}
2✔
71

72
void TcpServer::stop() {
3✔
73
  Logger::debug("[TcpServer] Stopping...");
3✔
74
  m_impl->acceptor.close();
3✔
75
  m_impl->context.stop();
3✔
76
  Logger::debug("[TcpServer] Stopped");
3✔
77
}
3✔
78

79
TcpServer::~TcpServer() = default;
2✔
80

81
void TcpServer::setup(unsigned short port) {
2✔
82
  if (m_impl->context.stopped())
2✔
83
    m_impl->context.restart();
1✔
84

85
  const asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port);
2✔
86
  m_impl->acceptor.open(endpoint.protocol());
2✔
87
  m_impl->acceptor.set_option(asio::socket_base::reuse_address(true));
2✔
88
  m_impl->acceptor.bind(endpoint);
2✔
89
  m_impl->acceptor.listen();
2✔
90
}
2✔
91

92
void TcpServer::accept() {
3✔
93
  Logger::debug("[TcpServer] Awaiting connection...");
3✔
94

95
  m_impl->acceptor.async_accept([this] (const asio::error_code& error, asio::ip::tcp::socket socket) {
3✔
96
    if (error == asio::error::interrupted || error == asio::error::operation_aborted)
1✔
97
      return; // Server closed
×
98

99
    if (error)
1✔
100
      Logger::error("[TcpServer] Error while accepting connection: {}", error.message());
×
101
    else
102
      std::make_shared<TcpSession>(std::move(socket))->run();
1✔
103

104
    accept();
1✔
105
  });
106
}
3✔
107

108
} // 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