-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
92 lines (74 loc) · 2.56 KB
/
main.cpp
File metadata and controls
92 lines (74 loc) · 2.56 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
//=============================================================================
// main.cpp
//
// Game Server entry point (console application).
// Runs a dedicated server with 32Hz tick rate using ENet networking.
//=============================================================================
#include "enet_server_network.h"
#include "game_server.h"
#include <cstdio>
#include <chrono>
#include <thread>
#include <csignal>
#include <cstring>
static volatile bool g_Running = true;
void SignalHandler(int)
{
g_Running = false;
}
int main(int argc, char* argv[])
{
// Handle Ctrl+C for graceful shutdown
std::signal(SIGINT, SignalHandler);
uint16_t port = 7777;
// Parse optional port argument: --port=XXXX
for (int i = 1; i < argc; i++)
{
if (strncmp(argv[i], "--port=", 7) == 0)
{
port = static_cast<uint16_t>(atoi(argv[i] + 7));
}
}
printf("========================================\n");
printf(" TriggerOn Game Server v2.0.1\n");
printf(" Tick Rate: 32Hz\n");
printf(" Port: %u\n", port);
printf("========================================\n");
// Initialize network
ENetServerNetwork network;
network.SetPort(port);
network.Initialize();
// Initialize game server logic
GameServer server;
server.Initialize(&network);
printf("[Server] Running. Press Ctrl+C to stop.\n\n");
// Server main loop
auto lastTime = std::chrono::high_resolution_clock::now();
uint32_t lastReportedTick = 0;
while (g_Running)
{
auto now = std::chrono::high_resolution_clock::now();
double dt = std::chrono::duration<double>(now - lastTime).count();
lastTime = now;
// Poll network events (receive packets, handle connections)
network.PollEvents();
// Run server tick(s) at 32Hz
server.Update(dt);
// Periodic status report (every ~1 second = every 32 ticks)
if (server.GetCurrentTick() - lastReportedTick >= 32)
{
lastReportedTick = server.GetCurrentTick();
printf("[Server] Tick: %u | Time: %.1fs | Clients: %zu | Players: %zu\n",
server.GetCurrentTick(),
server.GetServerTime(),
network.GetConnectedClientCount(),
server.GetPlayerCount());
}
// Sleep to avoid burning CPU (~1ms, well under tick duration of 31.25ms)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
printf("\n[Server] Shutting down...\n");
server.Finalize();
network.Finalize();
return 0;
}