From 2dd552dcbb2e1e671b4b3d97b3d7749db9064b39 Mon Sep 17 00:00:00 2001 From: Semitalis Date: Fri, 4 Aug 2023 18:59:12 +0200 Subject: [PATCH] Improved network synchronistation between players --- impl/game_common_lib/network_helpers.cpp | 9 +++++++-- impl/game_server_lib/server_network_connection.cpp | 5 ++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/impl/game_common_lib/network_helpers.cpp b/impl/game_common_lib/network_helpers.cpp index 6e9ec34a..34473cbc 100644 --- a/impl/game_common_lib/network_helpers.cpp +++ b/impl/game_common_lib/network_helpers.cpp @@ -1,12 +1,15 @@ #include "network_helpers.hpp" #include #include +#include #include #include void NetworkHelpers::freeSendString( std::string const& str, asio::ip::tcp::socket& socket, jt::LoggerInterface& logger) { + auto const start = std::chrono::system_clock::now(); + if (!socket.is_open()) { logger.error("socket closed", { "NetworkHelpers", "freeSendString" }); } @@ -19,12 +22,14 @@ void NetworkHelpers::freeSendString( // Note: keep payload alive auto payload_ptr = std::make_shared(buffer.str()); asio::async_write(socket, asio::buffer(*payload_ptr, payload_ptr->size()), - [payload_ptr, &logger, &socket](auto const& error, auto size) { + [payload_ptr, &logger, &socket, start](auto const& error, auto size) { + auto const duration_of_send = jt::getDurationInSecondsSince(start); if (error) { logger.error(error.message(), { "network", "freeSendString" }); socket.close(); } else { - logger.debug("message sent with size: " + std::to_string(size), + logger.debug("message sent with size: " + std::to_string(size) + ", took " + + std::to_string(duration_of_send) + "s", { "network", "freeSendString" }); } }); diff --git a/impl/game_server_lib/server_network_connection.cpp b/impl/game_server_lib/server_network_connection.cpp index b4869a92..13181635 100644 --- a/impl/game_server_lib/server_network_connection.cpp +++ b/impl/game_server_lib/server_network_connection.cpp @@ -134,9 +134,8 @@ void ServerNetworkConnection::handleMessage( void ServerNetworkConnection::sendMessageToAll(const Message& m) { - nlohmann::json const j = m; - auto const str = j.dump(); + auto const str = m_compressor.compress(j.dump()); for (auto& s : m_sockets) { if (!s || !s->is_open()) { @@ -145,7 +144,7 @@ void ServerNetworkConnection::sendMessageToAll(const Message& m) return; } - sendStringTo(m_compressor.compress(str), *s); + sendStringTo(str, *s); } }