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

Razakhel / RaZ / 18441942061

12 Oct 2025 09:08AM UTC coverage: 73.536% (-0.07%) from 73.601%
18441942061

push

github

Razakhel
[Network/TcpServer] Made the server asynchronous

0 of 32 new or added lines in 1 file covered. (0.0%)

8350 of 11355 relevant lines covered (73.54%)

1741.15 hits per line

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

0.0
/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:
NEW
15
  explicit TcpSession(asio::ip::tcp::socket socket) noexcept : m_socket{ std::move(socket) } {}
×
16

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

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

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

NEW
37
      self->receive();
×
38
    });
NEW
39
  }
×
40

NEW
41
  void echo(std::size_t length) {
×
NEW
42
    asio::async_write(m_socket, asio::buffer(m_data, length), [] (const asio::error_code& error, std::size_t) {
×
NEW
43
      if (error)
×
NEW
44
        Logger::error("[TcpSession] Error while echoing: {}", error.message());
×
NEW
45
    });
×
46
  }
×
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) {}
×
56

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

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

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

66
  setup(port);
×
NEW
67
  accept();
×
68

NEW
69
  m_impl->context.run();
×
70
}
×
71

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

79
TcpServer::~TcpServer() = default;
×
80

81
void TcpServer::setup(unsigned short port) {
×
82
  const asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port);
×
83
  m_impl->acceptor.open(endpoint.protocol());
×
84
  m_impl->acceptor.set_option(asio::socket_base::reuse_address(true));
×
85
  m_impl->acceptor.bind(endpoint);
×
86
  m_impl->acceptor.listen();
×
87
}
×
88

NEW
89
void TcpServer::accept() {
×
NEW
90
  Logger::debug("[TcpServer] Awaiting connection...");
×
91

NEW
92
  m_impl->acceptor.async_accept([this] (const asio::error_code& error, asio::ip::tcp::socket socket) {
×
NEW
93
    if (error == asio::error::interrupted || error == asio::error::operation_aborted)
×
NEW
94
      return; // Server closed
×
95

NEW
96
    if (error)
×
NEW
97
      Logger::error("[TcpServer] Error while accepting connection: {}", error.message());
×
98
    else
NEW
99
      std::make_shared<TcpSession>(std::move(socket))->run();
×
100

NEW
101
    accept();
×
102
  });
NEW
103
}
×
104

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