@@ -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
250254void 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
362365bool 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 );
0 commit comments