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

Razakhel / RaZ / 18244833421

04 Oct 2025 01:11PM UTC coverage: 73.672% (-0.05%) from 73.724%
18244833421

push

github

Razakhel
[Network/TcpServer] The port is given when starting the server

- Added move operations for TcpServer

1 of 14 new or added lines in 2 files covered. (7.14%)

8347 of 11330 relevant lines covered (73.67%)

1744.99 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
void runSession(asio::ip::tcp::socket socket) {
×
14
  try {
15
    while (true) {
16
      std::array<char, 1024> data {};
×
17
      std::error_code error;
×
18

19
      const size_t length = socket.read_some(asio::buffer(data), error);
×
20

21
      if (error == asio::error::eof) {
×
22
        Logger::debug("[TcpServer] Connection closed");
×
23
        break;
×
24
      }
25

26
      if (error)
×
27
        throw std::system_error(error);
×
28

29
      Logger::debug("[TcpServer] Received: {}", data.data());
×
30

31
      // Answering with the same received data
32
      asio::write(socket, asio::buffer(data, length));
×
33
    }
×
34
  } catch (const std::exception& exception) {
×
35
    Logger::error("[TcpServer] Exception occurred: {}", exception.what());
×
36
  }
×
37
}
×
38

39
}
40

41
struct TcpServer::Impl {
NEW
42
  Impl() : acceptor(context) {}
×
43

44
  asio::io_context context;
45
  asio::ip::tcp::acceptor acceptor;
46
};
47

NEW
48
TcpServer::TcpServer() : m_impl{ std::make_unique<Impl>() } {}
×
49

NEW
50
void TcpServer::start(unsigned short port) {
×
NEW
51
  Logger::debug("[TcpServer] Starting on port {}...", port);
×
52

NEW
53
  setup(port);
×
54

55
  while (true) {
NEW
56
    Logger::debug("[TcpServer] Awaiting connection on port {}...", port);
×
57

58
    std::error_code error;
×
59
    asio::ip::tcp::socket socket = m_impl->acceptor.accept(error);
×
60

61
    if (error == asio::error::interrupted)
×
62
      break; // Server closed
×
63

64
    Logger::debug("[TcpServer] Connected");
×
65
    std::thread(runSession, std::move(socket)).detach();
×
66
  }
×
67
}
×
68

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

76
TcpServer::~TcpServer() = default;
×
77

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

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