mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
network improvements and fixes
This commit is contained in:
@@ -206,3 +206,36 @@ void network::tcp::client::handle_accept(const asio::error_code &err)
|
||||
conn->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
network::tcp::asio_manager::asio_manager() {
|
||||
backend_list.emplace("tcp", this);
|
||||
}
|
||||
|
||||
std::shared_ptr<network::server> network::tcp::asio_manager::create_server(std::shared_ptr<std::fstream> backbuffer, const std::string &conf) {
|
||||
std::istringstream stream(conf);
|
||||
|
||||
std::string host;
|
||||
std::getline(stream, host, ':');
|
||||
|
||||
int port;
|
||||
stream >> port;
|
||||
|
||||
return std::make_shared<tcp::server>(backbuffer, io_context, host, port);
|
||||
}
|
||||
|
||||
std::shared_ptr<network::client> network::tcp::asio_manager::create_client(const std::string &conf) {
|
||||
std::istringstream stream(conf);
|
||||
|
||||
std::string host;
|
||||
std::getline(stream, host, ':');
|
||||
|
||||
int port;
|
||||
stream >> port;
|
||||
|
||||
return std::make_shared<tcp::client>(io_context, host, port);
|
||||
}
|
||||
|
||||
void network::tcp::asio_manager::update() {
|
||||
io_context.restart();
|
||||
io_context.poll();
|
||||
}
|
||||
|
||||
@@ -60,4 +60,17 @@ namespace network::tcp
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "network/manager.h"
|
||||
#include "simulation.h"
|
||||
#include "network/backend/asio.h"
|
||||
|
||||
network::server_manager::server_manager()
|
||||
{
|
||||
@@ -33,9 +32,15 @@ void network::server_manager::push_delta(double dt, double sync, const command_q
|
||||
serialize_message(msg, *backbuffer.get());
|
||||
}
|
||||
|
||||
void network::server_manager::create_server(asio::io_context &ctx, const std::string &host, uint32_t port)
|
||||
void network::server_manager::create_server(const std::string &backend, const std::string &conf)
|
||||
{
|
||||
servers.emplace_back(std::make_shared<tcp::server>(backbuffer, ctx, host, port));
|
||||
auto it = backend_list.find(backend);
|
||||
if (it == backend_list.end()) {
|
||||
ErrorLog("net: unknown backend: " + backend);
|
||||
return;
|
||||
}
|
||||
|
||||
servers.emplace_back(it->second->create_server(backbuffer, conf));
|
||||
}
|
||||
|
||||
network::manager::manager()
|
||||
@@ -44,20 +49,28 @@ network::manager::manager()
|
||||
|
||||
void network::manager::update()
|
||||
{
|
||||
io_context.restart();
|
||||
io_context.poll();
|
||||
for (auto &backend : backend_list)
|
||||
backend.second->update();
|
||||
|
||||
if (client)
|
||||
client->update();
|
||||
}
|
||||
|
||||
void network::manager::create_server(const std::string &host, uint32_t port)
|
||||
void network::manager::create_server(const std::string &backend, const std::string &conf)
|
||||
{
|
||||
servers.emplace();
|
||||
servers->create_server(io_context, host, port);
|
||||
servers->create_server(backend, conf);
|
||||
}
|
||||
|
||||
void network::manager::connect(const std::string &host, uint32_t port)
|
||||
void network::manager::connect(const std::string &backend, const std::string &conf)
|
||||
{
|
||||
client = std::make_shared<tcp::client>(io_context, host, port);
|
||||
auto it = backend_list.find(backend);
|
||||
if (it == backend_list.end()) {
|
||||
ErrorLog("net: unknown backend: " + backend);
|
||||
return;
|
||||
}
|
||||
|
||||
client = it->second->create_client(conf);
|
||||
}
|
||||
|
||||
//std::unordered_map<std::string, std::shared_ptr<network::backend_manager>> network::backend_list;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include <asio.hpp>
|
||||
#include <memory>
|
||||
#include "network/network.h"
|
||||
#include "command.h"
|
||||
@@ -17,21 +16,19 @@ namespace network
|
||||
|
||||
void push_delta(double dt, double sync, const command_queue::commands_map &commands);
|
||||
command_queue::commands_map pop_commands();
|
||||
void create_server(asio::io_context &ctx, const std::string &host, uint32_t port);
|
||||
void create_server(const std::string &backend, const std::string &conf);
|
||||
};
|
||||
|
||||
class manager
|
||||
{
|
||||
asio::io_context io_context;
|
||||
|
||||
public:
|
||||
manager();
|
||||
|
||||
std::optional<server_manager> servers;
|
||||
std::shared_ptr<network::client> client;
|
||||
|
||||
void create_server(const std::string &host, uint32_t port);
|
||||
void connect(const std::string &host, uint32_t port);
|
||||
void create_server(const std::string &backend, const std::string &conf);
|
||||
void connect(const std::string &backend, const std::string &conf);
|
||||
void update();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -143,7 +143,8 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
if (msg.type == message::CLIENT_HELLO) {
|
||||
auto cmd = dynamic_cast<const client_hello&>(msg);
|
||||
|
||||
if (cmd.version != 1) {
|
||||
if (cmd.version != 1 // wrong version
|
||||
|| !Global.ready_to_load) { // not ready yet
|
||||
conn->disconnect();
|
||||
return;
|
||||
}
|
||||
@@ -227,6 +228,10 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
Global.random_seed = cmd.seed;
|
||||
Global.random_engine.seed(Global.random_seed);
|
||||
Global.ready_to_load = true;
|
||||
} else if (Global.random_seed != cmd.seed) {
|
||||
ErrorLog("net: seed mismatch", logtype::net);
|
||||
conn->disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
WriteLog("net: accept received", logtype::net);
|
||||
@@ -241,3 +246,5 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
}
|
||||
|
||||
// --------------
|
||||
|
||||
std::unordered_map<std::string, network::backend_manager*> network::backend_list;
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace network
|
||||
friend class client;
|
||||
|
||||
private:
|
||||
const int CATCHUP_PACKETS = 100;
|
||||
const int CATCHUP_PACKETS = 500;
|
||||
|
||||
/*
|
||||
std::queue<
|
||||
@@ -97,5 +97,21 @@ namespace network
|
||||
void update();
|
||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
||||
void send_commands(command_queue::commands_map commands);
|
||||
int get_frame_counter() {
|
||||
return resume_frame_counter;
|
||||
}
|
||||
int get_awaiting_frames() {
|
||||
return delta_queue.size();
|
||||
}
|
||||
};
|
||||
|
||||
class backend_manager
|
||||
{
|
||||
public:
|
||||
virtual std::shared_ptr<server> create_server(std::shared_ptr<std::fstream>, const std::string &conf) = 0;
|
||||
virtual std::shared_ptr<client> create_client(const std::string &conf) = 0;
|
||||
virtual void update() = 0;
|
||||
};
|
||||
|
||||
extern std::unordered_map<std::string, backend_manager*> backend_list;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user