Working on server side stuff

This commit is contained in:
Vladislav Khorev committed 2026-11-17 15:28:21 +06:00
1 parent a784da9fb4
commit 542fc191fa
12 files changed
+757 -64

No files matched your search

+26 -32
View File
@@ -1,5 +1,6 @@
#include "WebsocketGameClient.h"
#include "NetworkProtocol.h"
#include "LockstepApply.h"
#include "../utils/Utils.h"
#ifdef __EMSCRIPTEN__
#include <emscripten/websocket.h>
@@ -408,8 +409,14 @@ private:
buffer_,
[this](beast::error_code error, std::size_t) {
if (error) {
connected_.store(false);
if (error != websocket::error::closed) fail("read", error);
// A clean close is also a lost connection for the game.
if (error == websocket::error::closed) {
connected_.store(false);
connectionFailed_.store(true);
std::cerr << "[network] Server closed the connection\n";
} else {
fail("read", error);
}
return;
}
std::string message = beast::buffers_to_string(buffer_.data());
@@ -497,18 +504,18 @@ void WebsocketGameClient::submit(const GameCommand& command)
if (!initialized_) return;
if (const auto* destination =
std::get_if<SetCharacterDestinationCommand>(&command)) {
impl_->send(makeDestinationInputMessage(
{nextClientSequence_++, destination->target}));
impl_->send(signInputMessage(makeDestinationInputMessage(
{nextClientSequence_++, destination->target}), sessionSecret_));
} else if (const auto* target =
std::get_if<SetCharacterTargetCommand>(&command)) {
impl_->send(makeCharacterActionInputMessage(
{nextClientSequence_++, "set_target", target->targetId}));
impl_->send(signInputMessage(makeCharacterActionInputMessage(
{nextClientSequence_++, "set_target", target->targetId}), sessionSecret_));
} else if (std::holds_alternative<ClearCharacterAttackTargetCommand>(command)) {
impl_->send(makeCharacterActionInputMessage(
{nextClientSequence_++, "clear_attack_target", {}}));
impl_->send(signInputMessage(makeCharacterActionInputMessage(
{nextClientSequence_++, "clear_attack_target", {}}), sessionSecret_));
} else if (std::holds_alternative<TryCharacterAttackComboCommand>(command)) {
impl_->send(makeCharacterActionInputMessage(
{nextClientSequence_++, "try_attack_combo", {}}));
impl_->send(signInputMessage(makeCharacterActionInputMessage(
{nextClientSequence_++, "try_attack_combo", {}}), sessionSecret_));
}
}
@@ -533,12 +540,15 @@ void WebsocketGameClient::update(std::int64_t)
logger() << "[network] Incompatible server content: expected "
<< expectedCompatibilityFingerprint_ << ", received "
<< initial.compatibilityFingerprint << '\n';
rejected_ = true;
continue;
}
try {
replica_.load(initial.state);
replica_.setLocalPlayerId(initial.assignedPlayerId);
nextExpectedTick_ = initial.snapshotTick + 1;
sessionSecret_ = std::move(initial.sessionSecret);
nextClientSequence_ = 1;
compatibilityFingerprint_ =
std::move(initial.compatibilityFingerprint);
pendingTicks_.clear();
@@ -570,8 +580,9 @@ void WebsocketGameClient::update(std::int64_t)
if (parseErrorMessage(text, errorCode, errorDetail)) {
logger() << "[network] Server rejected request (" << errorCode
<< "): " << errorDetail << '\n';
if (errorCode == "server_full" ||
errorCode == "incompatible")
// Everything except a recoverable malformed-input report ends the
// session (server_full, incompatible, rate_limited, kicked, ...).
if (errorCode != "invalid_input")
rejected_ = true;
continue;
}
@@ -587,25 +598,7 @@ void WebsocketGameClient::update(std::int64_t)
const std::string locationId =
replica_.snapshot()->currentLocationId;
for (const ScheduledCommand& scheduled : tick.commands) {
std::visit([&](const auto& command) {
using T = std::decay_t<decltype(command)>;
if constexpr (std::is_same_v<T, ScheduledDestinationCommand>) {
replica_.submit(SetCharacterDestinationCommand{
locationId, command.playerId, command.target, {}});
} else if constexpr (std::is_same_v<T, ScheduledCharacterEnabledCommand>) {
replica_.submit(SetCharacterEnabledCommand{
command.locationId, command.characterId, command.enabled});
} else if (command.action == "set_target") {
replica_.submit(SetCharacterTargetCommand{
locationId, command.playerId, command.targetId});
} else if (command.action == "clear_attack_target") {
replica_.submit(ClearCharacterAttackTargetCommand{
locationId, command.playerId});
} else if (command.action == "try_attack_combo") {
replica_.submit(TryCharacterAttackComboCommand{
locationId, command.playerId});
}
}, scheduled);
applyScheduledCommand(replica_, locationId, scheduled);
}
replica_.update(kSimulationTickMs);
completedSimulationTicks_.push_back({
@@ -655,7 +648,8 @@ bool WebsocketGameClient::connected() const
bool WebsocketGameClient::connectionFailed() const
{
return impl_->connectionFailed();
// Rejection (server full / incompatible content) is also a failed connection.
return rejected_ || impl_->connectionFailed();
}
} // namespace FRG