-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.hpp
70 lines (54 loc) · 1.9 KB
/
server.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once
// included first
#include "environment.hpp"
#include <asio.hpp>
#include <memory>
#include <string>
#include "beauty_common.hpp"
#include "connection.hpp"
#include "connection_manager.hpp"
#include "i_file_io.hpp"
#include "request_handler.hpp"
namespace beauty {
class Server {
public:
Server(const Server &) = delete;
Server &operator=(const Server &) = delete;
// Simple constructor, use for ESP32.
explicit Server(asio::io_context &ioContext,
uint16_t port,
IFileIO *fileIO,
HttpPersistence options,
size_t maxContentSize = 1024);
// Advanced constructor use for OS:s supporting signal_set.
explicit Server(asio::io_context &ioContext,
const std::string &address,
const std::string &port,
IFileIO *fileIO,
HttpPersistence options,
size_t maxContentSize = 1024);
uint16_t getBindedPort() const;
// Handlers to be optionally implemented.
void addRequestHandler(const handlerCallback &cb);
void setFileNotFoundHandler(const handlerCallback &cb);
void setDebugMsgHandler(const debugMsgCallback &cb);
private:
void doAccept();
void doAwaitStop();
void doTick();
std::shared_ptr<asio::signal_set> signals_;
asio::ip::tcp::acceptor acceptor_;
ConnectionManager connectionManager_;
RequestHandler requestHandler_;
// Unique Id for each connection.
unsigned connectionId_ = 0;
// Timer to handle connection status.
asio::steady_timer timer_;
// The max buffer size when reading/writing socket.
const size_t maxContentSize_;
// Callback to handle post file access, e.g. a custom not found handler.
handlerCallback fileNotFoundCb_;
// Callback to handle debug messages.
debugMsgCallback debugMsgCb_;
};
} // namespace beauty