-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.cpp
More file actions
331 lines (277 loc) · 14.4 KB
/
server.cpp
File metadata and controls
331 lines (277 loc) · 14.4 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// Created by Utsav Lal on 10/7/24.
//
#include <thread>
#include <memory>
#include <csignal>
#include <zmq.hpp>
#include "main.hpp"
#include "lib/game/GameManager.hpp"
#include "lib/model/components.hpp"
#include "lib/core/timeline.hpp"
#include "lib/ECS/coordinator.hpp"
#include "lib/helpers/colors.hpp"
#include "lib/helpers/constants.hpp"
#include "lib/helpers/random.hpp"
#include "lib/server/worker.hpp"
#include "lib/strategy/strategy_selector.hpp"
#include "lib/systems/kinematic.cpp"
#include "lib/systems/render.cpp"
#include "lib/systems/gravity.cpp"
#include "lib/systems/camera.cpp"
#include "lib/systems/client.hpp"
#include "lib/systems/collision.hpp"
#include "lib/systems/destroy.hpp"
#include "lib/systems/entity_created_handler.hpp"
#include "lib/systems/event_system.hpp"
#include "lib/systems/jump.hpp"
#include "lib/systems/keyboard_movement.cpp"
#include "lib/systems/move_between_2_point_system.hpp"
#include "lib/systems/position_update_handler.hpp"
#include "lib/systems/receiver.hpp"
// Since no anchor this will be global time. The TimeLine class counts in microseconds and hence tic_interval of 1000 ensures this class counts in milliseconds
void platform_movement(Timeline &timeline, MoveBetween2PointsSystem &moveBetween2PointsSystem) {
Timeline platformTimeline(&timeline, 1);
int64_t lastTime = platformTimeline.getElapsedTime();
while (GameManager::getInstance()->gameRunning) {
int64_t currentTime = platformTimeline.getElapsedTime();
float dT = (currentTime - lastTime) / 1000.f;
lastTime = currentTime;
moveBetween2PointsSystem.update(dT, platformTimeline);
auto elapsed_time = platformTimeline.getElapsedTime();
auto time_to_sleep = (1.0f / 60.0f) - (elapsed_time - currentTime); // Ensure float division
if (time_to_sleep > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(time_to_sleep * 1000)));
}
}
std::cout << "Kill platform thread" << std::endl;
}
void server_run(zmq::context_t &context, zmq::socket_ref frontend, zmq::socket_ref backend,
Send_Strategy *send_strategy) {
int max_threads = 5;
std::vector<std::unique_ptr<Worker> > workers;
std::vector<std::unique_ptr<std::thread> > threads;
std::unordered_set<std::string> clients;
std::shared_mutex clients_mutex;
for (int i = 0; i < max_threads; i++) {
workers.push_back(std::make_unique<Worker>(context, ZMQ_DEALER, "WORKER" + std::to_string(i)));
threads.push_back(std::make_unique<std::thread>(&Worker::work, workers[i].get(), send_strategy,
std::ref(clients), std::ref(clients_mutex)));
threads[i]->detach();
}
try {
zmq::proxy(zmq::socket_ref(frontend), zmq::socket_ref(backend), nullptr);
} catch (std::exception &e) {
std::cout << "Server error: " << e.what() << std::endl;
}
std::cout << "Kill server thread" << std::endl;
}
void catch_signals() {
std::signal(SIGINT, [](int signal) {
std::cout << "Caught SIGINT" << std::endl;
GameManager::getInstance()->gameRunning = false;
});
std::signal(SIGTERM, [](int signal) {
std::cout << "Caught SIGTERM" << std::endl;
GameManager::getInstance()->gameRunning = false;
});
std::signal(SIGSEGV, [](int signal) {
std::cout << "Caught SIGSEGV" << std::endl;
GameManager::getInstance()->gameRunning = false;
});
std::signal(SIGABRT, [](int signal) {
std::cout << "Caught SIGABRT" << std::endl;
GameManager::getInstance()->gameRunning = false;
});
}
int main(int argc, char *argv[]) {
// Register signals so that we can gracefully shutdown the server
std::cout << ENGINE_NAME << " v" << ENGINE_VERSION << " initializing server" << std::endl;
std::cout << "Created by Utsav and Jayesh" << std::endl;
std::cout << std::endl;
GameManager::getInstance()->gameRunning = true;
anchorTimeline.start();
std::unique_ptr<Send_Strategy> strategy = nullptr;
if (argv[1] != nullptr) {
strategy = Strategy::select_message_strategy(argv[1]);
} else {
strategy = Strategy::select_message_strategy("float");
}
Timeline gameTimeline(&anchorTimeline, 1);
gameTimeline.start();
gCoordinator.init();
gCoordinator.registerComponent<Transform>();
gCoordinator.registerComponent<Color>();
gCoordinator.registerComponent<CKinematic>();
gCoordinator.registerComponent<Camera>();
gCoordinator.registerComponent<Gravity>();
gCoordinator.registerComponent<KeyboardMovement>();
gCoordinator.registerComponent<Server>();
gCoordinator.registerComponent<MovingPlatform>();
gCoordinator.registerComponent<ServerEntity>();
gCoordinator.registerComponent<Destroy>();
gCoordinator.registerComponent<Collision>();
gCoordinator.registerComponent<Jump>();
gCoordinator.registerComponent<ClientEntity>();
gCoordinator.registerComponent<Receiver>();
gCoordinator.registerComponent<RigidBody>();
gCoordinator.registerComponent<Respawnable>();
gCoordinator.registerComponent<VerticalBoost>();
auto renderSystem = gCoordinator.registerSystem<RenderSystem>();
auto kinematicSystem = gCoordinator.registerSystem<KinematicSystem>();
auto gravitySystem = gCoordinator.registerSystem<GravitySystem>();
auto cameraSystem = gCoordinator.registerSystem<CameraSystem>();
auto keyboardMovementSystem = gCoordinator.registerSystem<KeyboardMovementSystem>();
auto moveBetween2PointsSystem = gCoordinator.registerSystem<MoveBetween2PointsSystem>();
auto destroySystem = gCoordinator.registerSystem<DestroySystem>();
auto collisionSystem = gCoordinator.registerSystem<CollisionSystem>();
auto jumpSystem = gCoordinator.registerSystem<JumpSystem>();
auto clientSystem = gCoordinator.registerSystem<ClientSystem>();
auto receiverSystem = gCoordinator.registerSystem<ReceiverSystem>();
auto eventSystem = gCoordinator.registerSystem<EventSystem>();
auto entityCreatedSystem = gCoordinator.registerSystem<EntityCreatedHandler>();
auto positionUpdateHandler = gCoordinator.registerSystem<PositionUpdateHandler>();
Signature clientEntitySignature;
clientEntitySignature.set(gCoordinator.getComponentType<ClientEntity>());
clientEntitySignature.set(gCoordinator.getComponentType<Transform>());
clientEntitySignature.set(gCoordinator.getComponentType<Color>());
gCoordinator.setSystemSignature<ClientSystem>(clientEntitySignature);
Signature clientSignature;
clientSignature.set(gCoordinator.getComponentType<Receiver>());
gCoordinator.setSystemSignature<ReceiverSystem>(clientSignature);
Signature movingPlatformSignature;
movingPlatformSignature.set(gCoordinator.getComponentType<Transform>());
movingPlatformSignature.set(gCoordinator.getComponentType<MovingPlatform>());
movingPlatformSignature.set(gCoordinator.getComponentType<CKinematic>());
movingPlatformSignature.set(gCoordinator.getComponentType<MovingPlatform>());
gCoordinator.setSystemSignature<MoveBetween2PointsSystem>(movingPlatformSignature);
Signature kinematicSignature;
kinematicSignature.set(gCoordinator.getComponentType<Transform>());
kinematicSignature.set(gCoordinator.getComponentType<CKinematic>());
gCoordinator.setSystemSignature<KinematicSystem>(kinematicSignature);
Signature serverEntitySignature;
serverEntitySignature.set(gCoordinator.getComponentType<ServerEntity>());
serverEntitySignature.set(gCoordinator.getComponentType<Transform>());
serverEntitySignature.set(gCoordinator.getComponentType<Color>());
Signature serverSig;
serverSig.set(gCoordinator.getComponentType<Server>());
serverSig.set(gCoordinator.getComponentType<Destroy>());
Signature destroySig;
destroySig.set(gCoordinator.getComponentType<Destroy>());
gCoordinator.setSystemSignature<DestroySystem>(destroySig);
Signature collisionSignature;
collisionSignature.set(gCoordinator.getComponentType<Transform>());
collisionSignature.set(gCoordinator.getComponentType<Collision>());
gCoordinator.setSystemSignature<CollisionSystem>(collisionSignature);
Signature jumpSignature;
jumpSignature.set(gCoordinator.getComponentType<Transform>());
jumpSignature.set(gCoordinator.getComponentType<CKinematic>());
jumpSignature.set(gCoordinator.getComponentType<Jump>());
gCoordinator.setSystemSignature<JumpSystem>(jumpSignature);
// create a platform
auto ground = gCoordinator.createEntity();
gCoordinator.addComponent(ground, Transform{0, SCREEN_HEIGHT - 100.f, 500.f, 300.f, 0});
gCoordinator.addComponent(ground, Color{shade_color::Green});
gCoordinator.addComponent(ground, ClientEntity{.synced = true});
gCoordinator.addComponent(ground, RigidBody{-1.f});
gCoordinator.addComponent(ground, Collision{true, false, CollisionLayer::OTHER});
gCoordinator.addComponent(ground, CKinematic{});
std::cout << "Ground1: " << gCoordinator.getEntityKey(ground) << std::endl;
auto ground2 = gCoordinator.createEntity();
gCoordinator.addComponent(ground2, Transform{800, SCREEN_HEIGHT - 100.f, 500.f, 1000.f, 0});
gCoordinator.addComponent(ground2, Color{shade_color::Green});
gCoordinator.addComponent(ground2, ClientEntity{.synced = true});
gCoordinator.addComponent(ground2, RigidBody{-1.f});
gCoordinator.addComponent(ground2, Collision{true, false, CollisionLayer::OTHER});
gCoordinator.addComponent(ground2, CKinematic{});
std::cout << "Ground2: " << gCoordinator.getEntityKey(ground2) << std::endl;
auto ground3 = gCoordinator.createEntity();
gCoordinator.addComponent(ground3, Transform{1000, SCREEN_HEIGHT / 2.f - 50.f, 50.f, 700.f, 0});
gCoordinator.addComponent(ground3, Color{shade_color::Black});
gCoordinator.addComponent(ground3, ClientEntity{0, true});
gCoordinator.addComponent(ground3, RigidBody{-1.f});
gCoordinator.addComponent(ground3, Collision{true, false, CollisionLayer::OTHER});
gCoordinator.addComponent(ground3, CKinematic{});
std::cout << "Ground3: " << gCoordinator.getEntityKey(ground3) << std::endl;
Entity platform = gCoordinator.createEntity();
gCoordinator.addComponent(platform, Transform{300, SCREEN_HEIGHT - 100.f, 50, 200});
gCoordinator.addComponent(platform, Color{shade_color::Red});
gCoordinator.addComponent(platform, CKinematic{0, 0, 0, 0});
gCoordinator.addComponent(platform, MovingPlatform{300, 800 - 200, TO, 2, HORIZONTAL});
gCoordinator.addComponent(platform, Destroy{});
gCoordinator.addComponent(platform, ClientEntity{0, true});
gCoordinator.addComponent(platform, RigidBody{-1.f});
gCoordinator.addComponent(platform, Collision{true, false, CollisionLayer::MOVING_PLATFORM});
std::cout << "Platform: " << gCoordinator.getEntityKey(platform) << std::endl;
Entity platform2 = gCoordinator.createEntity();
gCoordinator.addComponent(platform2, Transform{300, 500, 50, 200});
gCoordinator.addComponent(platform2, Color{shade_color::Cyan});
gCoordinator.addComponent(platform2, CKinematic{0, 0, 0, 0});
gCoordinator.addComponent(platform2, MovingPlatform{100, 400, TO, 2, VERTICAL});
gCoordinator.addComponent(platform2, Destroy{});
gCoordinator.addComponent(platform2, ClientEntity{.synced = true});
gCoordinator.addComponent(platform2, RigidBody{-1.f});
gCoordinator.addComponent(platform2, Collision{true, false, CollisionLayer::MOVING_PLATFORM});
auto trigger = gCoordinator.createEntity();
gCoordinator.addComponent(trigger, Transform{150.f, SCREEN_HEIGHT - 110.f, 32, 32, 0});
gCoordinator.addComponent(trigger, Color{shade_color::Black});
gCoordinator.addComponent(trigger, CKinematic{});
gCoordinator.addComponent(trigger, Destroy{});
gCoordinator.addComponent(trigger, RigidBody{0.f});
gCoordinator.addComponent(trigger, ClientEntity{0, true});
gCoordinator.addComponent(trigger, Collision{false, true, CollisionLayer::OTHER});
gCoordinator.addComponent(trigger, VerticalBoost{-200.f});
std::cout << "Platform2: " << gCoordinator.getEntityKey(platform2) << std::endl;
zmq::context_t context(1);
zmq::socket_t frontend(context, ZMQ_ROUTER);
zmq::socket_t backend(context, ZMQ_DEALER);
frontend.bind("tcp://*:5570");
backend.bind("tcp://*:5571");
std::thread server_thread(server_run, std::ref(context), zmq::socket_ref(frontend), zmq::socket_ref(backend),
strategy.get());
std::string identity = Random::generateRandomID(10);
std::cout << "Identity: " << identity << std::endl;
zmq::socket_t client_socket(context, ZMQ_DEALER);
client_socket.set(zmq::sockopt::routing_id, identity);
client_socket.connect("tcp://localhost:5570");
Entity server = gCoordinator.createEntity();
gCoordinator.addComponent(server, Server{7000, 7001});
auto last_time = gameTimeline.getElapsedTime();
std::thread platform_thread([&gameTimeline, &moveBetween2PointsSystem]() {
platform_movement(gameTimeline, *moveBetween2PointsSystem);
});
std::thread t2([&client_socket, &clientSystem, &strategy] {
while (GameManager::getInstance()->gameRunning) {
clientSystem->update(client_socket, strategy.get());
}
});
std::thread t1([receiverSystem, &context, &identity, &strategy]() {
zmq::socket_t socket(context, ZMQ_DEALER);
std::string id = identity + "R";
socket.set(zmq::sockopt::routing_id, id);
socket.connect("tcp://localhost:5570");
while (GameManager::getInstance()->gameRunning) {
receiverSystem->update(socket, strategy.get());
}
});
while (GameManager::getInstance()->gameRunning) {
auto current_time = gameTimeline.getElapsedTime();
auto dt = (current_time - last_time) / 1000.f;
last_time = current_time;
dt = std::max(dt, 1 / 60.f);
kinematicSystem->update(dt);
destroySystem->update();
eventSystem->update();
auto elapsed_time = gameTimeline.getElapsedTime();
auto time_to_sleep = (1.0f / 60.0f) - (elapsed_time - current_time); // Ensure float division
if (time_to_sleep > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(time_to_sleep * 1000)));
}
}
// Create 4 Rectangle instances
platform_thread.join();
t2.join();
server_thread.join();
std::cout << "Closing " << ENGINE_NAME << " Engine" << std::endl;
return 0;
}