-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHub.hpp
129 lines (109 loc) · 3.36 KB
/
Hub.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef GAZELLEMQ_HUB_HPP
#define GAZELLEMQ_HUB_HPP
#include <vector>
#include <csignal>
#include <cstdio>
#include <liburing.h>
#include <netinet/in.h>
#include <cstring>
#include "MessagePublisher.hpp"
#include "MessageSubscriber.hpp"
#include "Consts.hpp"
#include "EventLoopObject.hpp"
#include "ServerConnection.hpp"
namespace gazellemq::server {
class Hub {
private:
static constexpr auto NB_EVENTS = 16;
ServerConnection serverConnection{};
struct io_uring ring{};
public:
void start(int port, size_t const queueDepth) {
std::cout << "Starting GazelleMQ server on port " << port << std::endl;
signal(SIGINT, sigintHandler);
getSubscriberService().go();
getPublisherService().go();
//getClientService().go();
io_uring_queue_init(queueDepth, &ring, 0);
serverConnection.setPort(port);
serverConnection.handleEvent(&ring, 0);
doEventLoop();
}
private:
/**
* Error handler
* @param msg
*/
static void printError(char const* msg) {
printf("Error: %s\n", msg);
}
/**
* Error handler
* @param msg
*/
static void printError(char const* msg, int err) {
printf("%s\n%s\n", msg, strerror(-err));
}
/**
* Error handler
* @param msg
*/
static void fatalError(char const* msg) {
printf("%s\n", msg);
exit(1);
}
/**
* Error handler
* @param msg
*/
static void fatalError(char const* msg, int err) {
printf("%s\n%s\n", msg, strerror(-err));
exit(1);
}
/**
* Signal handler
* @param signo
*/
static void sigintHandler(int signo) {
printf("^C pressed. Shutting down\n");
exit(0);
}
/**
* Does the event loop
*/
void doEventLoop() {
std::vector<io_uring_cqe *> cqes{};
cqes.reserve(NB_EVENTS);
cqes.insert(cqes.begin(), NB_EVENTS, nullptr);
__kernel_timespec ts{.tv_sec = 2, .tv_nsec = 0};
while (true) {
int ret = io_uring_wait_cqe_timeout(&ring, cqes.data(), &ts);
if (ret == -SIGILL) {
continue;
}
if (ret < 0) {
if (ret == TIMEOUT) {
serverConnection.doClientConnectionCleanUp();
continue;
} else {
printError("io_uring_wait_cqe_timeout(...)", ret);
return;
}
}
for (auto* cqe: cqes) {
if (cqe != nullptr) {
int res = cqe->res;
if (res == -EAGAIN) {
io_uring_cqe_seen(&ring, cqe);
continue;
}
auto* pObject = static_cast<EventLoopObject*>(io_uring_cqe_get_data(cqe));
pObject->handleEvent(&ring, res);
}
io_uring_cqe_seen(&ring, cqe);
}
}
}
};
}
#endif //GAZELLEMQ_HUB_HPP