-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient_Core.cpp
More file actions
89 lines (73 loc) · 2.25 KB
/
Copy pathClient_Core.cpp
File metadata and controls
89 lines (73 loc) · 2.25 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Created by mc on 2/23/18.
//
#include "Client_Core.hpp"
void Client_Core::load_config(){
ifstream setting_file;
setting_file.open("client_settings.txt");
if (setting_file.good()){
// Basic config
setting_file >> listen_address >> listen_port >> password_confirm;
// Port address
string address;
int port;
string password;
while (setting_file >> address >> port >> password){
auto *setting = new Proxy_Peer(address, port, password);
interface_b.push_back(setting);
}
}else{
cout << "Cannot load settings" << endl;
exit(1);
}
}
void Client_Core::reconnect() {
for_each(interface_b.begin(), interface_b.end(), [this](Proxy_Peer *p) {
p->interface_connect = new Async_Connect(loop, p->address, p->port, DEFAULT_TIMEOUT);
p->interface_connect->connected_event = [this, p](int d){
auto * b = new Client_B(loop, p, d, this);
connection_b.push_back(b);
b->start();
};
p->interface_connect->failed_event = [this](){};
p->interface_connect->start();
});
}
void Client_Core::start() {
// FACE A
interface_a = new Async_Accept(loop, listen_address, listen_port);
interface_a->accepted_event = [this](int d, sockaddr_storage r, socklen_t rl){
int id = connection_a_count ++;
auto * a = new Client_A(loop, d, id, this);
connection_a[id] = a;
a->start();
};
interface_a->start();
// FACE B
// reconnect();
// Scheduler
watcher.start();
}
void Client_Core::operator()(ev::timer &w, int r) {
// Reconnect Peers
if (connection_b_available.size() < LOWEST_CONNECTION){
reconnect();
}
// Retransmit Package
for (const auto &kv : connection_a) {
if (kv.second == nullptr)continue;
kv.second->up_link_transmit();
kv.second->start_writer();
}
}
Client_Core::Client_Core(ev::default_loop *loop) {
// Copy
this->loop = loop;
// Initialize
this->connection_a_count = 0;
this->watcher.set(*loop);
this->watcher.set(this);
this->watcher.start(0, Encryption::get_random(RESEND_PERIOD + 1, RESEND_PERIOD));
// Load Configurations
load_config();
}