Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Server.h
Go to the documentation of this file.
1#ifndef joedb_asio_Server_declared
2#define joedb_asio_Server_declared
3
6
7#include <boost/asio/thread_pool.hpp>
8#include <boost/asio/local/stream_protocol.hpp>
9#include <boost/asio/signal_set.hpp>
10#include <boost/asio/awaitable.hpp>
11#include <boost/asio/strand.hpp>
12
13namespace joedb::asio
14{
15 /// Superclass for asio servers
16 ///
17 /// @ingroup asio
18 class Server
19 {
20 protected:
22 const int log_level;
23 void log(std::string_view s);
24
25 const int thread_count;
26 boost::asio::thread_pool thread_pool;
27 bool joined;
28
29 const std::string endpoint_path;
30 boost::asio::local::stream_protocol::endpoint endpoint;
31 boost::asio::local::stream_protocol::acceptor acceptor;
32 boost::asio::signal_set interrupt_signals;
33
34 class Session
35 {
36 public:
38 const int64_t id;
39 boost::asio::local::stream_protocol::socket socket;
40 boost::asio::strand<boost::asio::thread_pool::executor_type> strand;
42
43 boost::asio::awaitable<size_t> read_buffer(size_t offset, size_t size);
44 boost::asio::awaitable<void> write_buffer();
45
46 public:
48 (
50 boost::asio::local::stream_protocol::socket &&socket
51 );
52
53 void log(std::string_view s);
54
55 virtual boost::asio::awaitable<void> run() = 0;
56 virtual void cleanup() {}
57
58 virtual ~Session();
59 };
60
61 virtual std::unique_ptr<Session> new_session
62 (
63 boost::asio::local::stream_protocol::socket &&socket
64 ) = 0;
65
66 private:
67 int64_t session_id = 0;
68
69 boost::asio::awaitable<void> listener();
70
71 public:
72 Server
73 (
75 int log_level,
76 int thread_count,
77 std::string endpoint_path
78 );
79
80 int get_log_level() const
81 {
82 return log_level;
83 }
84
85 const std::string &get_endpoint_path() const
86 {
87 return endpoint_path;
88 }
89
90 bool is_joined() const {return joined;}
91 void stop();
92 void join();
93
94 virtual void cleanup_after_join();
95
96 virtual ~Server();
97 };
98}
99
100#endif
virtual boost::asio::awaitable< void > run()=0
virtual void cleanup()
Definition Server.h:56
void log(std::string_view s)
Definition Server.cpp:64
boost::asio::awaitable< void > write_buffer()
Definition Server.cpp:54
boost::asio::awaitable< size_t > read_buffer(size_t offset, size_t size)
Definition Server.cpp:37
boost::asio::strand< boost::asio::thread_pool::executor_type > strand
Definition Server.h:40
boost::asio::local::stream_protocol::socket socket
Definition Server.h:39
Superclass for asio servers.
Definition Server.h:19
boost::asio::local::stream_protocol::endpoint endpoint
Definition Server.h:30
const std::string endpoint_path
Definition Server.h:29
virtual std::unique_ptr< Session > new_session(boost::asio::local::stream_protocol::socket &&socket)=0
void log(std::string_view s)
Definition Server.cpp:12
Logger & logger
Definition Server.h:21
int get_log_level() const
Definition Server.h:80
boost::asio::local::stream_protocol::acceptor acceptor
Definition Server.h:31
const int thread_count
Definition Server.h:25
boost::asio::signal_set interrupt_signals
Definition Server.h:32
virtual ~Server()
Definition Server.cpp:178
boost::asio::thread_pool thread_pool
Definition Server.h:26
bool is_joined() const
Definition Server.h:90
const int log_level
Definition Server.h:22
const std::string & get_endpoint_path() const
Definition Server.h:85
virtual void cleanup_after_join()
Definition Server.cpp:174