mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
Merge remote-tracking branch 'tmj/master' into sim
This commit is contained in:
@@ -134,7 +134,7 @@ void network::tcp::connection::send_message(const message &msg)
|
||||
// -----------------
|
||||
|
||||
network::tcp::server::server(std::shared_ptr<std::istream> buf, asio::io_context &io_ctx, const std::string &host, uint32_t port)
|
||||
: network::server(buf), m_acceptor(io_ctx)
|
||||
: network::server(buf), m_acceptor(io_ctx), m_io_ctx(io_ctx)
|
||||
{
|
||||
auto endpoint = asio::ip::tcp::endpoint(asio::ip::address::from_string(host), port);
|
||||
m_acceptor.open(endpoint.protocol());
|
||||
@@ -147,11 +147,11 @@ network::tcp::server::server(std::shared_ptr<std::istream> buf, asio::io_context
|
||||
}
|
||||
|
||||
void network::tcp::server::accept_conn()
|
||||
{/*
|
||||
std::shared_ptr<connection> conn = std::make_shared<connection>(m_acceptor.get_executor().context());
|
||||
conn->set_handler(std::bind(&server::handle_message, this, conn, std::placeholders::_1));
|
||||
{
|
||||
std::shared_ptr<connection> conn = std::make_shared<connection>(m_io_ctx);
|
||||
conn->set_handler(std::bind(&server::handle_message, this, conn, std::placeholders::_1));
|
||||
|
||||
m_acceptor.async_accept(conn->m_socket, std::bind(&server::handle_accept, this, conn, std::placeholders::_1));*/
|
||||
m_acceptor.async_accept(conn->m_socket, std::bind(&server::handle_accept, this, conn, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void network::tcp::server::handle_accept(std::shared_ptr<connection> conn, const asio::error_code &err)
|
||||
@@ -210,7 +210,7 @@ void network::tcp::client::handle_accept(const asio::error_code &err)
|
||||
}
|
||||
|
||||
network::tcp::asio_manager::asio_manager() {
|
||||
backend_list.emplace("tcp", this);
|
||||
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) {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
#include "network/network.h"
|
||||
#ifdef DBG_NEW
|
||||
#undef new
|
||||
#endif
|
||||
|
||||
#include <asio.hpp>
|
||||
|
||||
#include "network/network.h"
|
||||
|
||||
namespace network::tcp
|
||||
{
|
||||
const uint32_t NETWORK_MAGIC = 0x37305545;
|
||||
@@ -13,7 +17,7 @@ namespace network::tcp
|
||||
friend class client;
|
||||
|
||||
public:
|
||||
connection(asio::io_context &io_ctx, bool client = false, size_t counter = 0);
|
||||
connection(asio::io_context &io_ctx, bool client = false, size_t counter = 0);
|
||||
~connection();
|
||||
|
||||
virtual void connected() override;
|
||||
@@ -41,6 +45,7 @@ namespace network::tcp
|
||||
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);
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "network/manager.h"
|
||||
#include "simulation.h"
|
||||
#include "Logs.h"
|
||||
|
||||
network::server_manager::server_manager()
|
||||
{
|
||||
@@ -36,8 +46,8 @@ void network::server_manager::push_delta(double render_dt, double dt, double syn
|
||||
|
||||
void network::server_manager::create_server(const std::string &backend, const std::string &conf)
|
||||
{
|
||||
auto it = backend_list.find(backend);
|
||||
if (it == backend_list.end()) {
|
||||
auto it = backend_list().find(backend);
|
||||
if (it == backend_list().end()) {
|
||||
ErrorLog("net: unknown backend: " + backend);
|
||||
return;
|
||||
}
|
||||
@@ -51,7 +61,7 @@ network::manager::manager()
|
||||
|
||||
void network::manager::update()
|
||||
{
|
||||
for (auto &backend : backend_list)
|
||||
for (auto &backend : backend_list())
|
||||
backend.second->update();
|
||||
|
||||
if (client)
|
||||
@@ -66,8 +76,8 @@ void network::manager::create_server(const std::string &backend, const std::stri
|
||||
|
||||
void network::manager::connect(const std::string &backend, const std::string &conf)
|
||||
{
|
||||
auto it = backend_list.find(backend);
|
||||
if (it == backend_list.end()) {
|
||||
auto it = backend_list().find(backend);
|
||||
if (it == backend_list().end()) {
|
||||
ErrorLog("net: unknown backend: " + backend);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -18,12 +18,16 @@ void network::server_hello::serialize(std::ostream &stream) const
|
||||
{
|
||||
sn_utils::ls_uint32(stream, seed);
|
||||
sn_utils::ls_int64(stream, timestamp);
|
||||
sn_utils::ls_int64(stream, config);
|
||||
sn_utils::s_str(stream, scenario);
|
||||
}
|
||||
|
||||
void network::server_hello::deserialize(std::istream &stream)
|
||||
{
|
||||
seed = sn_utils::ld_uint32(stream);
|
||||
timestamp = sn_utils::ld_int64(stream);
|
||||
config = sn_utils::ld_int64(stream);
|
||||
scenario = sn_utils::d_str(stream);
|
||||
}
|
||||
|
||||
void ::network::request_command::serialize(std::ostream &stream) const
|
||||
|
||||
@@ -40,6 +40,8 @@ struct server_hello : public message
|
||||
|
||||
uint32_t seed;
|
||||
int64_t timestamp;
|
||||
int64_t config;
|
||||
std::string scenario;
|
||||
|
||||
virtual void serialize(std::ostream &stream) const override;
|
||||
virtual void deserialize(std::istream &stream) override;
|
||||
|
||||
@@ -7,6 +7,18 @@
|
||||
#include "application.h"
|
||||
#include "Globals.h"
|
||||
|
||||
std::uint32_t const EU07_NETWORK_VERSION = 2;
|
||||
|
||||
namespace network {
|
||||
|
||||
backend_list_t& backend_list() {
|
||||
// HACK: static initialization order fiasco fix
|
||||
// NOTE: potential static deinitialization order fiasco
|
||||
static backend_list_t backend_list;
|
||||
return backend_list;
|
||||
}
|
||||
|
||||
}
|
||||
// connection
|
||||
|
||||
void network::connection::disconnect() {
|
||||
@@ -30,7 +42,7 @@ void network::connection::connected()
|
||||
|
||||
if (is_client) {
|
||||
client_hello msg;
|
||||
msg.version = 1;
|
||||
msg.version = EU07_NETWORK_VERSION;
|
||||
msg.start_packet = packet_counter;
|
||||
send_message(msg);
|
||||
}
|
||||
@@ -115,7 +127,7 @@ 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 // wrong version
|
||||
if (cmd.version != EU07_NETWORK_VERSION // wrong version
|
||||
|| !Global.ready_to_load) { // not ready yet
|
||||
conn->disconnect();
|
||||
return;
|
||||
@@ -124,6 +136,8 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
server_hello reply;
|
||||
reply.seed = Global.random_seed;
|
||||
reply.timestamp = Global.starting_timestamp;
|
||||
reply.config = 0; // TODO: pass bitfield with state of relevant setting switches
|
||||
reply.scenario = Global.SceneryFile;
|
||||
conn->state = connection::CATCHING_UP;
|
||||
conn->backbuffer = backbuffer;
|
||||
conn->backbuffer_pos = 0;
|
||||
@@ -234,6 +248,8 @@ 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.starting_timestamp = cmd.timestamp;
|
||||
// TODO: configure simulation settings according to received cmd.config
|
||||
Global.SceneryFile = cmd.scenario;
|
||||
Global.ready_to_load = true;
|
||||
} else if (Global.random_seed != cmd.seed) {
|
||||
ErrorLog("net: seed mismatch", logtype::net);
|
||||
@@ -257,5 +273,3 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
}
|
||||
|
||||
// --------------
|
||||
|
||||
std::unordered_map<std::string, network::backend_manager*> network::backend_list;
|
||||
|
||||
@@ -114,5 +114,8 @@ namespace network
|
||||
virtual void update() = 0;
|
||||
};
|
||||
|
||||
extern std::unordered_map<std::string, backend_manager*> backend_list;
|
||||
// HACK: static initialization order fiasco fix
|
||||
// extern std::unordered_map<std::string, backend_manager*> backend_list;
|
||||
using backend_list_t = std::unordered_map<std::string, backend_manager*>;
|
||||
backend_list_t& backend_list();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user