-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.h
More file actions
74 lines (59 loc) · 1.36 KB
/
server.h
File metadata and controls
74 lines (59 loc) · 1.36 KB
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
#ifndef __SERVER_H
#define __SERVER_H
#include <string>
#include <unordered_map>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/event.h>
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#include <fcntl.h>
#include "log.h"
#include "parser.h"
namespace Http
{
class Server
{
public:
Server(
const char *addr = "0.0.0.0",
const int port = 8080,
const int backlog = 1000);
Server(Server &s) = delete;
Server(Server &&s) = delete;
~Server();
void onRead(struct kevent& event);
void onEOF(struct kevent& event);
int onClientConnect(struct kevent& event);
int onClientDisconnect(struct kevent& event);
void run();
private:
int listen();
int bind();
int shutdown();
int close();
int setupRun();
struct sockaddr_in m_address;
int m_sock_reuse;
int m_sock;
int m_backlog;
int m_kqueue;
struct kevent m_event_subs;
static const int EVENTS_MAX = 32;
struct kevent m_event_list[EVENTS_MAX];
static const int RECEIVE_MAX = 1024;
char m_receive_buf[RECEIVE_MAX];
enum SocketState {
INITIALIZED,
BOUND,
LISTENING,
CLOSED
};
SocketState m_sock_state;
std::unordered_map<std::string, std::string> m_clients;
};
} // namspace
#endif /** __SERVER_H **/