3#include <boost/asio.hpp>
7#include "message_queue.h"
9#include "connection_map.h"
10#include "timeout_manager.h"
34 boost::asio::io_context io_context_;
35 boost::asio::ip::tcp::endpoint endpoint_;
36 boost::asio::ip::tcp::acceptor acceptor_;
39 std::thread workerThread_;
40 MessageQueue messageQueue_;
41 TimeoutManager timeoutManager_;
42 ConnectionMap conMap_;
43 uint64_t currentId_ = 0;
44 std::function<void()> callBack_;
45 bool keepAliveByHeartBeat;
49 void startAccepting(){
50 acceptor_.async_accept([
this](boost::system::error_code ec, boost::asio::ip::tcp::socket socket) {
52 handleConnection(std::move(socket));
56 if(ec == boost::asio::error::operation_aborted) {
return; }
59 log.
message =
"Critical error while listening for clients";
64 logger_.push(std::move(log));
66 std::cout <<
"Critcal error while listening for clients: " << ec.message() << std::endl;
71 void handleConnection(boost::asio::ip::tcp::socket socket){
72 conMap_.addAndGetConnection(currentId_, std::move(socket), messageQueue_, timeoutManager_, logger_, [
this,
id = currentId_](){
73 if(callBack_) { callBack_(); }
95 Server(std::uint16_t port, std::function<
void()> callback,
bool keepAliveByHeartBeat =
true,
bool sendHeartBeats =
false,
LoggerConfig loggerConfig = {}) :
96 endpoint_(boost::asio::ip::tcp::v4(), port),
97 acceptor_(io_context_, endpoint_),
98 logger_(io_context_, std::move(loggerConfig)),
100 keepAliveByHeartBeat(keepAliveByHeartBeat),
101 sendHeartBeat(sendHeartBeats)
113 workerThread_ = std::thread([
this]() {
114 if (io_context_.stopped()) { io_context_.restart(); }
124 boost::system::error_code ec;
127 messageQueue_.close();
130 if (workerThread_.joinable()) {
131 workerThread_.join();
134 logger_.unsafeFlushNow();
142 return messageQueue_.pop();
151 void send(uint64_t
id, std::string body){
152 auto con = conMap_.getConnection(
id);
154 con->send(std::move(body));
168 READ_BODY_TIMEOUT_MS = duration;
171 SEND_TIMEOUT_MS = duration;
174 SERVER_HEARTBEAT_TIMEOUT_MS = duration;
177 HEARTBEAT_RATE = duration;
Writes log entries to rotating files in the background. Used internally by Server and Client.
Definition logger.h:131
void start()
Starts accepting clients on the I/O thread.
Definition server.h:110
~Server()
Stops the server (see stop()) and releases its resources.
Definition server.h:105
Server(std::uint16_t port, std::function< void()> callback, bool keepAliveByHeartBeat=true, bool sendHeartBeats=false, LoggerConfig loggerConfig={})
Creates a server bound to a port on all IPv4 interfaces. Clients are not accepted until start() is ca...
Definition server.h:95
void send(uint64_t id, std::string body)
Queues a message to be sent to one client.
Definition server.h:151
void setTimeout(TimeoutType type, std::chrono::milliseconds duration)
Changes one of the timeouts. Timeouts are shared by every server and client in the program.
Definition server.h:162
void stop()
Stops accepting, closes every connection, wakes any thread blocked in receive() and joins the I/O thr...
Definition server.h:123
std::optional< acsf::Message > receive()
Blocks until a message arrives from any client.
Definition server.h:141
Everything in the framework lives in this namespace.
TimeoutType
Selects a timeout for Server::setTimeout().
Definition protocol.h:51
@ HeartbeatTimeout
Definition protocol.h:59
@ ReadBody
Definition protocol.h:55
@ Read
Definition protocol.h:53
@ HeartbeatRate
Definition protocol.h:61
@ Send
Definition protocol.h:57
@ ERROR
Definition logger.h:27
std::chrono::milliseconds READ_TIMEOUT_MS
Global timeouts in milliseconds, shared by every server and client. Change them with setTimeout().
Definition protocol.h:37
One log entry. Every field is optional and only the fields that are set appear in the output.
Definition logger.h:46
std::optional< std::string > errorMessage
Definition logger.h:61
std::optional< uint16_t > localPort
Definition logger.h:67
std::optional< int > errorValue
Definition logger.h:59
std::optional< std::string > localAddress
Definition logger.h:65
std::optional< std::string > message
Definition logger.h:55
std::optional< LogLevel > level
Definition logger.h:51
Logging settings passed to acsf::Server and acsf::Client.
Definition logger.h:118