IRSOL
C++ code implementing socket server for interacting with Baumer camera.
acceptor.cpp
Go to the documentation of this file.
2
3#include "irsol/assert.hpp"
4#include "irsol/logging.hpp"
6#include "irsol/utils.hpp"
7
8namespace irsol {
9namespace server {
10namespace internal {
13 OnNewClientCallback_t onNewClientCallback)
14 : m_port(port), m_onNewClientCallback(onNewClientCallback)
15{
17}
18
19std::string
21{
22 IRSOL_ASSERT_ERROR(!isOpen(), "Acceptor is not in error mode");
23 return m_isOpen.error().message();
24}
25
26bool
28{
29 return bool(m_isOpen);
30}
31
32void
34{
35 if(!m_running) {
36 IRSOL_LOG_DEBUG("Accept loop already stopped");
37 return;
38 }
39 m_running.store(false);
40 m_acceptor.close();
41}
42
43void
45{
46 if(!isOpen()) {
47 IRSOL_LOG_ERROR("Failed to open acceptor on port {}: {}", m_port, error());
48 throw std::runtime_error("Failed to open acceptor");
49 }
50 if(m_running) {
51 IRSOL_LOG_ERROR("Accept loop already running");
52 return;
53 }
54 m_running.store(true);
55
56 IRSOL_LOG_INFO("Accept loop started");
57 while(m_running) {
58
59 // Set non-blocking mode to avoid blocking indefinitely on accept
60 m_acceptor.set_non_blocking(true);
61
62 auto sockResult = m_acceptor.accept();
63
64 // Check if we should exit the loop
65 if(!m_running) {
66 IRSOL_LOG_DEBUG("Accept loop stopped");
67 break;
68 }
69
70 if(!sockResult) {
71 if(m_running) {
72 sockpp::error_code err{sockResult.error()};
73 // These errors are expected in non-blocking mode when no connection is available
74 bool isExpectedError =
75 (err == std::errc::resource_unavailable_try_again ||
76 err == std::errc::operation_would_block || err == std::errc::timed_out);
77
78 if(isExpectedError) {
79 // Sleep for a short time to avoid busy waiting when no connections are available
80 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
81 } else {
82 // Log unexpected errors
83 IRSOL_LOG_WARN("Failed to accept connection: {}", sockResult.error_message());
84 }
85 }
86 continue;
87 }
88
89 // Generate a unique ID for this client session
91 IRSOL_LOG_DEBUG("Generated client ID: {}", clientId);
92 m_onNewClientCallback(clientId, sockResult.release());
93 }
94 IRSOL_LOG_INFO("Accept loop ended");
95}
96}
97}
98}
Accepts and forwards incoming client connections to the session handler.
Assertion macros and utilities based on the PPK_ASSERT library.
OnNewClientCallback_t m_onNewClientCallback
Callback to handle accepted client connections.
Definition acceptor.hpp:103
ClientSessionAcceptor(irsol::types::port_t port, OnNewClientCallback_t onNewClientCallback)
Constructs the acceptor.
Definition acceptor.cpp:11
const irsol::types::port_t m_port
Port on which to accept incoming TCP connections.
Definition acceptor.hpp:98
irsol::types::acceptor_t m_acceptor
TCP acceptor socket bound to m_port.
Definition acceptor.hpp:108
std::atomic< bool > m_running
Flag indicating whether the acceptor should continue running.
Definition acceptor.hpp:93
bool isOpen() const
Checks if the acceptor is actively listening.
Definition acceptor.cpp:27
std::string error() const
Gets the current error message, if any.
Definition acceptor.cpp:20
void run()
Starts the accept loop.
Definition acceptor.cpp:44
irsol::types::connection_result_t m_isOpen
Result of attempting to bind and open the acceptor socket.
Definition acceptor.hpp:113
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94
#define IRSOL_LOG_WARN(...)
Logs a warning-level message using the default logger.
Definition logging.hpp:93
#define IRSOL_LOG_DEBUG(...)
Logs a debug-level message using the default logger.
Definition logging.hpp:91
std::function< void(irsol::types::client_id_t, irsol::types::socket_t &&)> OnNewClientCallback_t
Callback function for handling new client connections.
Definition acceptor.hpp:39
Logging utilities and configuration for the irsol library.
std::string client_id_t
Represents a unique client identifier. Typically used to identify connected clients by string IDs.
Definition types.hpp:55
uint16_t port_t
Represents a network port number. Typically used to specify TCP or UDP ports.
Definition types.hpp:48
sockpp::inet_address inet_address_t
Alias for an IPv4/IPv6 internet address.
Definition types.hpp:79
std::string uuid()
Generates a new UUID string.
Definition utils.cpp:16
Declaration of the ClientSession class.
General utility functions used throughout the irsol library.