-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.h
71 lines (61 loc) · 1.96 KB
/
Server.h
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
//
// Created by marcin on 11/19/22.
//
#ifndef EPOLL_WORK_QUEUE_SERVER_H
#define EPOLL_WORK_QUEUE_SERVER_H
#include "Client.h"
#include "utils.h"
#include <chrono>
#include <functional>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>
using Callback = std::function<WorkerAction(ClientEvent)>;
class Server {
public:
Server(){};
Server(Callback callback);
~Server();
// Does the server event loop.
// This call will terminate once stop() is called
bool run();
// Marks the server as ready for run()
void start(std::string port);
// Lets the server exit the run() loop
void stop();
private:
static const constexpr auto EPOLL_MAX_EVENTS = 64;
static const constexpr auto EPOLL_TIMEOUT = std::chrono::seconds(1);
static const constexpr auto CLIENT_TIMEOUT = std::chrono::seconds(5);
// Are we running?
bool running;
// Sequence for client IDs
unsigned int client_id;
// File descriptor of the TCP server socket
int tcp_fd;
// File descriptor of the epoll queue
int epoll_fd;
// Mapping of client FDs to clients
std::unordered_map<int, std::shared_ptr<Client>> clients_by_fd;
// Mapping of client heartbeat timer FDs to clients
std::unordered_map<int, std::shared_ptr<Client>> clients_by_timer_fd;
// The callback
Callback callback;
// Accept new client when ready
std::vector<std::shared_ptr<Client>> accept_clients();
// Remove existing client: close the socket, remove from epoll and maps
void remove_client(Client client);
// Handle event arriving from the client
std::optional<std::vector<char>> read_from_client(const Client& client);
// Handle the response to a worker event
void handle_worker_action(const Client& client, WorkerAction action);
// Cleanup the clients and sockets
void cleanup();
};
#endif //EPOLL_WORK_QUEUE_SERVER_H