-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connection.hpp
84 lines (70 loc) · 2.47 KB
/
Connection.hpp
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
#ifndef CONNECTION_HPP
#define CONNECTION_HPP
#include <vector>
#include <memory>
#include <utility>
#include "Util.hpp"
#include "Packet_Header.hpp"
#include "Packet_Payload.hpp"
#include "Packet.hpp"
#include "Archiver.hpp"
#include "Receiver.hpp"
#include "Sender.hpp"
struct Connection_Address;
class Connection {
private:
std::shared_ptr<udp_socket> socket_ptr;
Receiver receiver;// = make_unique<Receiver>(io,socket_ptr);
Sender sender;
Seq sequence_number;// = 0;
Seqs received_seqs;
int received_seqs_lim;// = 40;
Seq received;// = -1;
Packets unacked_packets;
Tick tick;// = 0;
Instance_Id instance_id_;
std::deque<std::pair<long,std::vector<char>>> data_to_send;
public:
long fake_delay_us;
void toggle_fake_delay_us(const long& delay_us);
void close();
Instance_Id instance_id() const;
Connection(io_service& io,
const Connection_Address& connection_address,
Instance_Id instance_id,
int received_seqs_lim);
Connection(io_service& io,
std::string local_port, // port to listen on
std::string host, // host to connect to to send stuff to
std::string port, // port to connect to
Instance_Id instance_id,
int received_seqs_lim,
long fake_delay_us=0);
bool available();
Packet_Payloads receive();
void send(Packet_Payload& payload);
void set_fake_delay_us(const long& delay_us);
};
struct Connection_Address {
std::string local_port; // port to listen on
std::string remote_host; // remote host
std::string remote_port; // remote port
// used for clients as they need the remote/local ports swapped
Connection_Address(
std::string local_port,
std::string remote_host,
std::string remote_port) :
local_port(local_port),
remote_host(remote_host),
remote_port(remote_port) {}
Connection_Address clientify() const {
return Connection_Address(remote_port, remote_host, local_port);
}
Connection_Address static clientify(const Connection_Addresses& addrs, int index) {
auto addr = addrs[index].clientify();
assert(addrs.size() > 0 && "Should have a connection address to clientify");
addr.remote_host = addrs.front().remote_host;
return addr;
}
};
#endif