-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathevents.h
101 lines (73 loc) · 2.34 KB
/
events.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
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
#include <iostream>
#include <string>
#include <cstring>
// libev
#include <ev++.h>
// Sockets
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
/*
TODO find out what these do
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
*/
/*
We have three classes - the mother, the middlemen, and the worker
THE MOTHER
The mother is called when a client opens a connection to the server.
It creates a middleman for every new connection, which will be called
when its socket is ready for reading.
THE MIDDLEMEN
Each middleman hang around until data is written to its socket. It then
reads the data and sends it to the worker. When it gets the response, it
gets called to write its data back to the client.
THE WORKER
The worker gets data from the middleman, and returns the response. It
doesn't concern itself with silly things like sockets.
see worker.h for the worker.
*/
// THE MOTHER - Spawns connection middlemen
class connection_mother {
private:
int listen_socket;
sockaddr_in address;
socklen_t addr_len;
worker * work;
config * conf;
mysql * db;
ev::timer schedule_event;
unsigned long opened_connections;
unsigned int open_connections;
public:
connection_mother(worker * worker_obj, config * config_obj, mysql * db_obj);
void increment_open_connections() { open_connections++; }
void decrement_open_connections() { open_connections--; }
int get_open_connections() { return open_connections; }
int get_opened_connections() { return opened_connections; }
void handle_connect(ev::io &watcher, int events_flags);
~connection_mother();
};
// THE MIDDLEMAN
// Created by connection_mother
// Add their own watchers to see when sockets become readable
class connection_middleman {
private:
int connect_sock;
ev::io read_event;
ev::io write_event;
ev::timer timeout_event;
std::string response;
config * conf;
connection_mother * mother;
worker * work;
sockaddr_in client_addr;
public:
connection_middleman(int &listen_socket, sockaddr_in &address, socklen_t &addr_len, worker* work, connection_mother * mother_arg, config * config_obj);
~connection_middleman();
void handle_read(ev::io &watcher, int events_flags);
void handle_write(ev::io &watcher, int events_flags);
void handle_timeout(ev::timer &watcher, int events_flags);
};