-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_server.h
86 lines (54 loc) · 1.75 KB
/
proxy_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// Created by root on 07.01.16.
//
#ifndef KIMBERLY_PROXY_SERVER_H
#define KIMBERLY_PROXY_SERVER_H
#include <string>
#include <vector>
#include <arpa/inet.h>
#include <mutex>
#include "buffer.h"
#include "concurrent_queue.h"
#include "hostname_resolver.h"
#include "file_descriptor.h"
class handler;
class client_handler;
class notifier;
#define BUFFER_SIZE 1024
#define TARGET_CONNECTIONS 1000
#define RESOLVER_THREADS 100
#define CONNECTION_TIMEOUT 3
//template <int TARGET_CONNECTIONS = 1000, int RESOLVER_THREADS = 20, int CONNECTION_TIMEOUT = 1, int BUFFER_SIZE = 1024>
class proxy_server {
friend class handler;
friend class notifier;
friend class client_handler;
friend class server_handler;
friend class hostname_resolver;
public:
proxy_server(std::string host, uint16_t port, int resolver_threads = RESOLVER_THREADS);
void loop();
void terminate();
protected:
void add_handler(std::shared_ptr<handler> h, const uint &events);
void modify_handler(const handler *h, uint events);
void remove_handler(const handler *h);
// returns true if large_buffer was totally sended to fd
bool write_chunk(handler *h, buffer &buf);
// returns true if recv returned 0
bool read_chunk(handler *h, buffer *buf);
void notify_epoll();
void add_resolver_task(int fd, std::string hostname, uint flags);
private:
uint16_t port;
in_addr_t host;
file_descriptor epfd;//main epoll
std::vector<std::shared_ptr<handler>> handlers;
concurrent_queue<std::function<void()>> to_run;
char temp_buffer[BUFFER_SIZE + 1];
std::shared_ptr<notifier> notifier_;
bool terminating = false;
hostname_resolver resolver;
//hostname_resolver resolver;
};
#endif //KIMBERLY_PROXY_SERVER_H