17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-18 05:39:19 +02:00
Files
maszyna/network/backend/asio.h
Hirek193 54d8b112b8 multiplayer stage 7: versioned snapshots instead of replaying the session
Joining meant replaying backbuffer.bin from the very first frame of the
session, so the longer a session ran, the longer it took to get in - and the
whole history had to be kept for it.

- network/snapshot: a chunked container with a version of its own. Every
  section carries its length, so a peer built before a section existed reads
  a newer snapshot and simply walks past what it does not know. Version 1
  carries the session (tick, clock, weather, the state of the random engine),
  the vehicles (position on the track, speed, direction, controls, brake
  pressures, whether the AI is driving) and the crew roster. Switches,
  memcells, signals and the event queue are the next sections
- joining is now: handshake -> load the scenario -> CLIENT_READY -> the server
  takes a snapshot on the spot and sends it -> the peer goes live. Because the
  snapshot is taken at the moment the peer becomes active there is no backlog
  to replay at all; the frame by frame catch-up is gone
- the server refuses a peer that loaded a different scenario, with a readable
  reason instead of a silent desync
- reconnecting into a session that is already loaded asks for a fresh snapshot
  straight away rather than waiting for a loader that will not run again
- a vehicle is only put back on the rails when it really is somewhere else; the
  arithmetic that turns the recorded bogie offset back into the nose distance
  place_on_track() expects mirrors what that function itself does
- the session log is still written out - it is useful when picking a desync
  apart - but nothing depends on replaying it any more
2026-09-06 19:37:28 +00:00

84 lines
2.2 KiB
C++

#ifdef DBG_NEW
#undef new
#endif
#define ASIO_DISABLE_VISIBILITY
#include <asio.hpp>
#include "network/network.h"
namespace network::tcp
{
const uint32_t NETWORK_MAGIC = 0x37305545;
// a snapshot of a busy scenario is comfortably larger than a command frame
const uint32_t MAX_MSG_SIZE = 8 * 1024 * 1024;
class connection : public network::connection
{
friend class server;
friend class client;
public:
connection(asio::io_context &io_ctx, bool client = false, size_t counter = 0);
~connection();
virtual void connected() override;
virtual void disconnect() override;
virtual void send_messages(const std::vector<std::shared_ptr<message> > &messages) override;
virtual void send_message(const message &msg) override;
asio::ip::tcp::socket m_socket;
private:
std::string m_header_buffer;
std::string m_body_buffer;
void write_message(const message &msg, std::ostream &stream);
void send_data(std::shared_ptr<std::string> buffer);
void read_header();
void handle_header(const asio::error_code &err, size_t bytes_transferred);
void handle_data(const asio::error_code &err, size_t bytes_transferred);
};
class server : public network::server
{
private:
void accept_conn();
void handle_accept(std::shared_ptr<connection> conn, const asio::error_code &err);
asio::ip::tcp::acceptor m_acceptor;
asio::io_context &m_io_ctx;
public:
server(std::shared_ptr<std::istream> buf, asio::io_context &io_ctx, const std::string &host, uint32_t port);
};
class client : public network::client
{
private:
void handle_accept(const asio::error_code &err);
asio::io_context &io_ctx;
std::string host;
uint32_t port;
protected:
virtual void connect() override;
public:
client(asio::io_context &io_ctx, const std::string &host, uint32_t port);
};
class asio_manager : public network::backend_manager {
asio::io_context io_context;
public:
asio_manager();
virtual std::shared_ptr<network::server> create_server(std::shared_ptr<std::fstream>, const std::string &conf) override;
virtual std::shared_ptr<network::client> create_client(const std::string &conf) override;
virtual void update() override;
};
asio_manager manager;
}