-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaggligator.h
138 lines (108 loc) · 8.45 KB
/
aggligator.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma once
#include "stdafx.h"
namespace aggligator
{
static constexpr int AGGLIGATOR_RECONNECT_TIMEOUT = 5;
static constexpr int AGGLIGATOR_CONNECT_TIMEOUT = 5;
static constexpr int AGGLIGATOR_INACTIVE_TIMEOUT = 72;
class aggligator : public std::enable_shared_from_this<aggligator>
{
class server;
typedef std::shared_ptr<server> server_ptr;
class client;
typedef std::shared_ptr<client> client_ptr;
class connection;
typedef std::shared_ptr<connection> connection_ptr;
class convergence;
typedef std::shared_ptr<convergence> convergence_ptr;
typedef std::shared_ptr<aggligator> ptr;
struct send_packet
{
std::shared_ptr<Byte> packet;
int length;
};
public:
class information
{
public:
uint64_t rx;
uint64_t tx;
uint64_t rx_pps;
uint64_t tx_pps;
uint32_t client_count;
uint32_t connection_count;
uint32_t establish_count;
unordered_set<int> bind_ports;
unordered_set<boost::asio::ip::tcp::endpoint> server_endpoints;
};
public:
aggligator(boost::asio::io_context& context, const std::shared_ptr<Byte>& buffer, int buffer_size, int congestions) noexcept;
~aggligator() noexcept;
public:
std::function<void()> Exit;
std::function<void(uint64_t)> Tick;
public:
void close() noexcept;
bool server_open(const unordered_set<int>& bind_ports, const boost::asio::ip::address& destination_ip, int destination_port) noexcept;
bool client_open(
int connections,
int bind_port,
const unordered_set<boost::asio::ip::tcp::endpoint>& servers) noexcept;
uint64_t now() noexcept { return now_; }
void update(uint64_t now) noexcept;
bool info(information& i) noexcept;
bool server_mode() noexcept { return server_mode_; }
public:
enum link_status
{
link_status_none = 0,
link_status_unknown = 1,
link_status_connecting = 2,
link_status_reconnecting = 3,
link_status_established = 4,
};
link_status status() noexcept;
link_status status(information& i) noexcept;
public:
static void socket_flash_mode(bool value) noexcept;
static void socket_adjust(int sockfd, bool in4) noexcept;
static void deadline_timer_cancel(deadline_timer& t) noexcept;
static void socket_close(boost::asio::ip::udp::socket& socket) noexcept;
static void socket_close(boost::asio::ip::tcp::socket& socket) noexcept;
static bool socket_adjust(boost::asio::ip::tcp::socket& socket) noexcept;
static bool socket_adjust(boost::asio::ip::udp::socket& socket) noexcept;
static bool socket_adjust(boost::asio::ip::tcp::acceptor& socket) noexcept;
static bool ip_is_invalid(const boost::asio::ip::address& address) noexcept;
#if defined(JEMALLOC)
static void jemaillc_areans_set_default() noexcept;
#endif
static boost::asio::ip::udp::endpoint ip_v6_to_v4(const boost::asio::ip::udp::endpoint& ep) noexcept;
static boost::asio::ip::tcp::endpoint ip_v6_to_v4(const boost::asio::ip::tcp::endpoint& ep) noexcept;
static boost::asio::ip::udp::endpoint ip_v4_to_v6(const boost::asio::ip::udp::endpoint& ep) noexcept;
static boost::asio::ip::tcp::endpoint ip_v4_to_v6(const boost::asio::ip::tcp::endpoint& ep) noexcept;
virtual std::shared_ptr<Byte> make_shared_bytes(int length) noexcept;
private:
bool client_reopen(client* client) noexcept;
bool server_closed(client* client) noexcept;
bool server_accept(const acceptor& acceptor) noexcept;
bool server_accept(const std::shared_ptr<boost::asio::ip::tcp::socket>& socket, const boost::asio::yield_context& y) noexcept;
bool create_timeout() noexcept;
bool nawait_timeout() noexcept;
private:
boost::asio::io_context& context_;
std::shared_ptr<Byte> buffer_;
int buffer_size_;
int congestions_;
bool server_mode_;
uint32_t last_;
uint64_t now_;
uint64_t rx_;
uint64_t tx_;
uint64_t rx_pps_;
uint64_t tx_pps_;
server_ptr server_;
client_ptr client_;
deadline_timer reopen_;
deadline_timer timeout_;
};
}