Skip to content

Commit

Permalink
Merge pull request #40 from runvs/PerformanceImprovements
Browse files Browse the repository at this point in the history
Improved network synchronistation between players
  • Loading branch information
Semitalis authored Aug 4, 2023
2 parents 5e0a910 + 2dd552d commit b00126a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 7 additions & 2 deletions impl/game_common_lib/network_helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "network_helpers.hpp"
#include <asio/read.hpp>
#include <asio/write.hpp>
#include <performance_measurement.hpp>
#include <iomanip>
#include <sstream>

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" });
}
Expand All @@ -19,12 +22,14 @@ void NetworkHelpers::freeSendString(
// Note: keep payload alive
auto payload_ptr = std::make_shared<std::string>(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" });
}
});
Expand Down
5 changes: 2 additions & 3 deletions impl/game_server_lib/server_network_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -145,7 +144,7 @@ void ServerNetworkConnection::sendMessageToAll(const Message& m)
return;
}

sendStringTo(m_compressor.compress(str), *s);
sendStringTo(str, *s);
}
}

Expand Down

0 comments on commit b00126a

Please sign in to comment.