Skip to content

Commit 497d040

Browse files
committed
update docs
1 parent a2d2927 commit 497d040

5 files changed

Lines changed: 42 additions & 43 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Multiplayer C++ Lobby Server that starts a websocket server with scripting in Lu
99

1010
- [addon_tiny_lobby_client](https://github.com/appsinacup/addon_tiny_lobby_client): Godot Tiny Lobby Client
1111

12+
<img src="logo.png" height="128"/>
13+
1214
## Usage
1315

1416
Run locally by downloading latest [GitHub Release](https://github.com/appsinacup/tiny_lobby/releases) and running it in terminal:

example_game_config.ini

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
[00000000-0000-0000-0000-000000000001]
2-
lobby_control=lua
3-
sendrate=100
4-
folder=echo
5-
6-
[00000000-0000-0000-0000-000000000000]
7-
lobby_control=relay
8-
sendrate=0
1+
[game]
2+
tickrate=100 # default 0
3+
sendrate=100 # default 0
4+
max_afk_time=300 # default 0, disabled

logo.png

39.5 KB
Loading

src/game/game_thread.cpp

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ void GameThread::load_games() {
5252
}
5353
std::string section = folder_name;
5454
std::string lobby_control = "relay";
55-
INIReader config_reader(
56-
BasePath::instance().file(scripts_folder + "/" + folder_name + "/config.ini"));
55+
INIReader config_reader(scripts_folder + "/" + folder_name + "/config.ini");
5756
int tickrate = config_reader.GetInteger("game", "tickrate", 0);
5857
if (tickrate < 16) {
5958
tickrate = 0;
@@ -148,26 +147,51 @@ void GameThread::run() {
148147
int64_t last_listing = now;
149148
int64_t last_disconnect = now;
150149
int64_t last_timers = now;
150+
int64_t last_time = now;
151151
int64_t last_stats = now;
152152
int64_t disconnect_interval = max_reconnection_time / 10;
153153
int64_t timer_interval = 500;
154154
int64_t stats_interval = 10000;
155-
int min_process_size = 100;
156-
int64_t last_time = 0;
155+
int max_process_size = 50;
157156
while (!stop) {
158-
// Process min_process_size messages
157+
// Process max_process_size messages at once
159158
int messages_processed = 0;
160-
while (receive_queue.size_approx() > 0 && messages_processed < min_process_size) {
159+
while (messages_processed < max_process_size) {
161160
messages_processed++;
162161
if (!handle_events()) {
163162
break;
164163
}
165164
}
165+
last_time = now;
166+
handle_tick();
167+
handle_send();
168+
// if time has not changed, sleep for a bit
166169
if (last_time == now) {
167-
std::this_thread::sleep_for(std::chrono::milliseconds(check_time / 2));
168-
continue;
170+
std::this_thread::sleep_for(std::chrono::milliseconds(5));
171+
//continue;
169172
}
170-
last_time = now;
173+
// handle disconnects
174+
if (now - last_disconnect > disconnect_interval) {
175+
last_disconnect = now;
176+
handle_disconnects();
177+
handle_afk();
178+
}
179+
// handle listing
180+
if (now - last_listing > listing_interval) {
181+
last_listing = now;
182+
handle_lobby_list();
183+
}
184+
// handle timers
185+
if (now - last_timers > timer_interval) {
186+
last_timers = now;
187+
handle_timers();
188+
}
189+
// handle file watchers
190+
std::string folder_reloaded;
191+
if (file_watcher_queue.try_dequeue(folder_reloaded)) {
192+
reload_game(folder_reloaded);
193+
}
194+
// handle stats
171195
if (last_stats + stats_interval < now && logger.verbose) {
172196
last_stats = now;
173197
// open stats file and append to it. use date format YYYY-MM-DD for filename
@@ -224,39 +248,18 @@ void GameThread::run() {
224248
messages_received = 0;
225249
messages_sent = 0;
226250
}
227-
handle_tick();
228-
handle_send();
229-
if (now - last_disconnect > disconnect_interval) {
230-
last_disconnect = now;
231-
handle_disconnects();
232-
handle_afk();
233-
}
234-
if (now - last_listing > listing_interval) {
235-
last_listing = now;
236-
handle_lobby_list();
237-
}
238-
if (now - last_timers > timer_interval) {
239-
last_timers = now;
240-
handle_timers();
241-
}
242-
std::string folder_reloaded;
243-
if (file_watcher_queue.try_dequeue(folder_reloaded)) {
244-
reload_game(folder_reloaded);
245-
}
246-
// handle tick if/when needed
247251
}
248252
}
249253

250254
void GameThread::time_run() {
251255
using clock = std::chrono::system_clock;
252256
using ms = std::chrono::milliseconds;
253-
int64_t check_time = 5;
254257
while (!stop) {
255258
auto now_local = clock::now();
256259
auto now_ms = std::chrono::time_point_cast<ms>(now_local);
257260
auto since_epoch = now_ms.time_since_epoch();
258261
int64_t next_tick_ms =
259-
since_epoch.count() + (check_time - (since_epoch.count() % check_time));
262+
since_epoch.count() + (check_time / 2 - (since_epoch.count() % check_time / 2));
260263
auto wake_time = clock::time_point(ms(next_tick_ms));
261264

262265
std::this_thread::sleep_until(wake_time);
@@ -361,7 +364,7 @@ void GameThread::handle_timers() {
361364

362365
bool GameThread::handle_events() {
363366
WebSocketReceivedMessage message;
364-
if (!receive_queue.wait_dequeue_timed(message, check_time * 500)) {
367+
if (!receive_queue.try_dequeue(message)) {
365368
return false;
366369
}
367370
messages_received++;
@@ -820,7 +823,7 @@ void GameThread::on_error(GameData &game, std::string command_id, std::string pe
820823
logger.error_log("[GameThread] on_error ", command_id, " ", peer_id, " ", message);
821824
}
822825
if (logical_error) {
823-
logger.debug_log("[GameThread] on_logical_error ", command_id, " ", peer_id, " ", message);
826+
logger.error_log("[GameThread] on_logical_error ", command_id, " ", peer_id, " ", message);
824827
}
825828
if (close) {
826829
send(game, peer_id, message, uWS::OpCode::CLOSE);

src/game/game_thread.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <boost/uuid/uuid_generators.hpp>
99
#include <boost/uuid/uuid_io.hpp>
1010
#include <efsw/efsw.hpp>
11-
#include <mutex>
1211

1312
#include "../common/any_type.h"
1413
#include "../common/server_logger.h"
@@ -48,7 +47,6 @@ class GameThread {
4847
GamesListener games_listener;
4948
efsw::FileWatcher file_watcher;
5049
moodycamel::ReaderWriterQueue<std::string> file_watcher_queue;
51-
std::mutex mutex;
5250
moodycamel::BlockingReaderWriterQueue<DatabaseReceivedMessage> &database_queue;
5351
bool db_enabled;
5452
DatabaseThread database_thread;

0 commit comments

Comments
 (0)