-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network_Test.cpp
186 lines (162 loc) · 6.49 KB
/
Network_Test.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "Util.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <sstream>
#include <string>
#include <memory>
#include <algorithm>
#include <chrono>
#include <thread>
#include "Archiver.hpp"
#include "Force.hpp"
#include "Shot.hpp"
#include "P_State.hpp"
#include "Receiver.hpp"
#include "Sender.hpp"
#include "Packet_Header.hpp"
#include "Packet_Payload.hpp"
#include "Packet.hpp"
#include "Connection.hpp"
void static parse_args(
int argc,
char* argv[],
std::string& instance_type,
Instance_Id& instance_id,
Connection_Addresses& connection_addresses
);
static std::string instance_type; // server/client etc.
static Instance_Id instance_id; // 0-255, server usually 0
static const std::string type_server = "server";
static const std::string type_client = "client";
static const std::string type_local = "local"; // no networking
static io_service io;
static Connection_Addresses connection_addresses;
static int received_seqs_lim = 40;
int main(int argc, char* argv[]) {
parse_args(argc, argv,
instance_type,
instance_id,
connection_addresses);
int tps = 100;
int sleep_time = 1000000/tps;
auto big = 100;
auto gen_payload = [&] () -> Packet_Payload {
static Tick tick = 0;
Packet_Payload payload(Packet_Payload::Type::Input, tick++);
Forces fs;
fs.emplace_back(Force(tick++,v3(69.348284523f,722334.20000223f,-432.3425f),Force::Type::Force));
fs.emplace_back(Force(tick++,v3(69.348284523f,722334.20000223f,-432.3425f),Force::Type::Force));
fs.emplace_back(Force(tick++,v3(69.348284533f,722334.20000223f,-452.3425f),Force::Type::Force));
fs.emplace_back(Force(tick++,v3(69.348284523f,722334.20000223f,-432.3425f),Force::Type::Force));
fs.emplace_back(Force(tick++,v3(69.348284524f,722334.20000223f,-432.3425f),Force::Type::Force));
fs.emplace_back(Force(tick++,v3(69.348284524f,722334.20000223f,-411.3425f),Force::Type::Force));
fs.emplace_back(Force(tick++,v3(69.348284523f,722334.20000223f,-440.3425f),Force::Type::Force));
fs.emplace_back(Force(tick++,v3(69.348284489f,722334.20000223f,-432.3425f),Force::Type::Force));
payload.forces = fs;
return payload;
};
// if want printouts, uncomment the //// lines in Connection.cpp
Connections connections;
// setup connections
if (instance_type == type_server) {
for (int i=0; i<connection_addresses.size(); ++i) {
if (i == instance_id) {
continue;
// don't want server connecting to self
}
const auto& connection_address = connection_addresses[i];
//std::cout << "Making connection to " << connection_address.remote_port << "\n";
connections.emplace_back(io,
connection_address,
instance_id, received_seqs_lim);
//std::cout << "Success\n";
}
} else if (instance_type == type_client) {
// add server with ports swapped and remote_host as server address
connections.emplace_back(io,
Connection_Address::clientify(connection_addresses, instance_id),
instance_id, received_seqs_lim);
}
if (instance_type == type_server) {
//for (connst auto& connection_address: connections_addresses) {
std::cout << "Server has " << connections.size() << " connections\n";
for (int i=0; i<big; ++i) {
sleep_us(sleep_time);
for (auto& conn: connections) {
while (conn.available()) {
Packet_Payloads payloads = conn.receive();
//std::cout << int(conn.instance_id()) << " received ticks ("<<payloads.size() <<")\n";
for (const auto& payload: payloads) {
//std::cout << "Tick:" << payload.tick << "\n";
}
}
Packet_Payload payload = gen_payload();
conn.send(payload);
}
}
} else if (instance_type == type_client) {
assert(connections.size() == 1 && "Client should only have one connection");
for (int i=0; i<big; ++i) {
Connection& conn = connections[0];
sleep_us(sleep_time);
auto p = gen_payload();
conn.send(p);
conn.send(p);
//p = gen_payload();
//conn.send(p);
Packet_Payloads payloads = conn.receive();
//std::cout << int(conn.instance_id()) << " received ticks ("<<payloads.size() <<")\n";
for (const auto& payload: payloads) {
//std::cout << "Tick:" << payload.tick << "\n";
}
}
}
}
void static parse_args(
int argc,
char* argv[],
std::string& instance_type,
Instance_Id& instance_id,
Connection_Addresses& connection_addresses
) {
if (argc < 9) {
std::cerr << "Args of form: ./exec [client|server] id (local_port remote_host remote_port){2,}\n";
std::cerr << "instance_type is " << type_server << "," << type_client;
std::cerr << " or " << type_local << " and id 0-255" << "\n";
exit(1);
}
instance_type = std::string(argv[1]);
if (instance_type != type_server &&
instance_type != type_client &&
instance_type != type_local) {
std::cerr << "instance_type is " << type_server << "," << type_client;
std::cerr << " or " << type_local << "\n";
exit(1);
}
if (instance_type == type_local) {
} else {
int potential_id = std::stoi(argv[2]);
if (potential_id < 0 || potential_id > 255) {
std::cerr << "Id must be from 0 to num instances-1 and must be in byte range (it's an array index\n";
exit(1);
}
instance_id = potential_id;
int start_of_rest = 3;
if ((argc-start_of_rest) % 3 != 0 || (argc-start_of_rest) < 6) {
std::cerr << "Arguments at end should [local_port remote_host remote_port] triplets\n";
exit(1);
}
for (int i=start_of_rest; i<argc; i+=3) {
std::string local_port = argv[i+0];
std::string remote_host = argv[i+1];
std::string remote_port = argv[i+2];
connection_addresses.emplace_back(
Connection_Address(local_port, remote_host, remote_port));
}
}
std::cout << "\n";
}