mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-23 00:29:19 +02:00
network improvements and fixes
This commit is contained in:
@@ -2817,10 +2817,6 @@ bool TController::DecBrake()
|
|||||||
|
|
||||||
bool TController::IncSpeed()
|
bool TController::IncSpeed()
|
||||||
{ // zwiększenie prędkości; zwraca false, jeśli dalej się nie da zwiększać
|
{ // zwiększenie prędkości; zwraca false, jeśli dalej się nie da zwiększać
|
||||||
if( true == tsGuardSignal.is_playing() ) {
|
|
||||||
// jeśli gada, to nie jedziemy
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
bool OK = true;
|
bool OK = true;
|
||||||
if( ( iDrivigFlags & moveDoorOpened )
|
if( ( iDrivigFlags & moveDoorOpened )
|
||||||
&& ( VelDesired > 0.0 ) ) { // to prevent door shuffle on stop
|
&& ( VelDesired > 0.0 ) ) { // to prevent door shuffle on stop
|
||||||
|
|||||||
27
Globals.cpp
27
Globals.cpp
@@ -715,6 +715,11 @@ global_settings::ConfigParse(cParser &Parser) {
|
|||||||
Parser >> fpslimit;
|
Parser >> fpslimit;
|
||||||
minframetime = std::chrono::duration<float>(1.0f / fpslimit);
|
minframetime = std::chrono::duration<float>(1.0f / fpslimit);
|
||||||
}
|
}
|
||||||
|
else if (token == "randomseed")
|
||||||
|
{
|
||||||
|
Parser.getTokens(1);
|
||||||
|
Parser >> Global.random_seed;
|
||||||
|
}
|
||||||
else if (token == "gfx.envmap.enabled")
|
else if (token == "gfx.envmap.enabled")
|
||||||
{
|
{
|
||||||
Parser.getTokens(1);
|
Parser.getTokens(1);
|
||||||
@@ -805,23 +810,21 @@ global_settings::ConfigParse(cParser &Parser) {
|
|||||||
}
|
}
|
||||||
else if (token == "network.server")
|
else if (token == "network.server")
|
||||||
{
|
{
|
||||||
Parser.getTokens(1);
|
|
||||||
Parser >> network_conf.is_server;
|
|
||||||
if (network_conf.is_server) {
|
|
||||||
Parser.getTokens(2);
|
Parser.getTokens(2);
|
||||||
Parser >> network_conf.server_host;
|
|
||||||
Parser >> network_conf.server_port;
|
std::string backend;
|
||||||
}
|
std::string conf;
|
||||||
|
Parser >> backend >> conf;
|
||||||
|
|
||||||
|
network_servers.push_back(std::make_pair(backend, conf));
|
||||||
}
|
}
|
||||||
else if (token == "network.client")
|
else if (token == "network.client")
|
||||||
{
|
{
|
||||||
Parser.getTokens(1);
|
|
||||||
Parser >> network_conf.is_client;
|
|
||||||
if (network_conf.is_client) {
|
|
||||||
Parser.getTokens(2);
|
Parser.getTokens(2);
|
||||||
Parser >> network_conf.client_host;
|
|
||||||
Parser >> network_conf.client_port;
|
network_client.emplace();
|
||||||
}
|
Parser >> network_client->first;
|
||||||
|
Parser >> network_client->second;
|
||||||
}
|
}
|
||||||
} while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile)
|
} while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile)
|
||||||
// na koniec trochę zależności
|
// na koniec trochę zależności
|
||||||
|
|||||||
13
Globals.h
13
Globals.h
@@ -29,7 +29,7 @@ struct global_settings {
|
|||||||
std::mt19937 random_engine;
|
std::mt19937 random_engine;
|
||||||
std::mt19937 local_random_engine;
|
std::mt19937 local_random_engine;
|
||||||
bool ready_to_load { false };
|
bool ready_to_load { false };
|
||||||
uint32_t random_seed;
|
uint32_t random_seed = 0;
|
||||||
TCamera pCamera; // parametry kamery
|
TCamera pCamera; // parametry kamery
|
||||||
TCamera pDebugCamera;
|
TCamera pDebugCamera;
|
||||||
std::array<Math3D::vector3, 10> FreeCameraInit; // pozycje kamery
|
std::array<Math3D::vector3, 10> FreeCameraInit; // pozycje kamery
|
||||||
@@ -200,15 +200,8 @@ struct global_settings {
|
|||||||
bool gfx_shadergamma = false;
|
bool gfx_shadergamma = false;
|
||||||
bool gfx_usegles = false;
|
bool gfx_usegles = false;
|
||||||
|
|
||||||
struct network_conf_t {
|
std::vector<std::pair<std::string, std::string>> network_servers;
|
||||||
bool is_server = false;
|
std::optional<std::pair<std::string, std::string>> network_client;
|
||||||
std::string server_host;
|
|
||||||
uint32_t server_port;
|
|
||||||
|
|
||||||
bool is_client = false;
|
|
||||||
std::string client_host;
|
|
||||||
uint32_t client_port;
|
|
||||||
} network_conf;
|
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
void LoadIniFile( std::string asFileName );
|
void LoadIniFile( std::string asFileName );
|
||||||
|
|||||||
@@ -77,12 +77,12 @@ void UpdateTimers(bool pause)
|
|||||||
|
|
||||||
if (DeltaTime > 1.0)
|
if (DeltaTime > 1.0)
|
||||||
DeltaTime = 1.0;
|
DeltaTime = 1.0;
|
||||||
|
|
||||||
|
fSimulationTime += GetDeltaTime();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
DeltaTime = 0.0; // wszystko stoi, bo czas nie płynie
|
DeltaTime = 0.0; // wszystko stoi, bo czas nie płynie
|
||||||
|
|
||||||
fSimulationTime += GetDeltaTime();
|
|
||||||
|
|
||||||
oldCount = count;
|
oldCount = count;
|
||||||
// Keep track of the time lapse and frame count
|
// Keep track of the time lapse and frame count
|
||||||
#if __linux__
|
#if __linux__
|
||||||
|
|||||||
@@ -6915,7 +6915,6 @@ void TTrain::DynamicSet(TDynamicObject *d)
|
|||||||
|
|
||||||
void
|
void
|
||||||
TTrain::MoveToVehicle(TDynamicObject *target) {
|
TTrain::MoveToVehicle(TDynamicObject *target) {
|
||||||
ErrorLog("MoveToVehicle");
|
|
||||||
// > Ra: to nie może być tak robione, to zbytnia proteza jest
|
// > Ra: to nie może być tak robione, to zbytnia proteza jest
|
||||||
// indeed, too much hacks...
|
// indeed, too much hacks...
|
||||||
// TODO: cleanup
|
// TODO: cleanup
|
||||||
|
|||||||
@@ -174,8 +174,8 @@ eu07_application::run() {
|
|||||||
if (m_modes[m_modestack.top()]->is_command_processor()) {
|
if (m_modes[m_modestack.top()]->is_command_processor()) {
|
||||||
// active mode is doing real calculations (e.g. drivermode)
|
// active mode is doing real calculations (e.g. drivermode)
|
||||||
|
|
||||||
bool nextloop = true;
|
int loop_remaining = MAX_NETWORK_PER_FRAME;
|
||||||
while (nextloop)
|
while (--loop_remaining > 0)
|
||||||
{
|
{
|
||||||
command_queue::commands_map commands_to_exec;
|
command_queue::commands_map commands_to_exec;
|
||||||
command_queue::commands_map local_commands = simulation::Commands.pop_intercept_queue();
|
command_queue::commands_map local_commands = simulation::Commands.pop_intercept_queue();
|
||||||
@@ -207,14 +207,14 @@ eu07_application::run() {
|
|||||||
m_network->client->send_commands(local_commands);
|
m_network->client->send_commands(local_commands);
|
||||||
|
|
||||||
if (delta == 0.0)
|
if (delta == 0.0)
|
||||||
nextloop = false;
|
loop_remaining = -1;
|
||||||
}
|
}
|
||||||
// if we're master
|
// if we're master
|
||||||
else {
|
else {
|
||||||
// just push local commands to execution
|
// just push local commands to execution
|
||||||
add_to_dequemap(commands_to_exec, local_commands);
|
add_to_dequemap(commands_to_exec, local_commands);
|
||||||
|
|
||||||
nextloop = false;
|
loop_remaining = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// send commands to command queue
|
// send commands to command queue
|
||||||
@@ -242,7 +242,8 @@ eu07_application::run() {
|
|||||||
{
|
{
|
||||||
// verify sync
|
// verify sync
|
||||||
if (sync != slave_sync) {
|
if (sync != slave_sync) {
|
||||||
WriteLog("net: DESYNC!", logtype::net);
|
WriteLog("net: desync! calculated: " + std::to_string(sync)
|
||||||
|
+ ", received: " + std::to_string(slave_sync), logtype::net);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set total delta for rendering code
|
// set total delta for rendering code
|
||||||
@@ -250,6 +251,17 @@ eu07_application::run() {
|
|||||||
Timer::set_delta_override(totalDelta);
|
Timer::set_delta_override(totalDelta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!loop_remaining) {
|
||||||
|
// loop break forced by counter
|
||||||
|
float received = m_network->client->get_frame_counter();
|
||||||
|
float awaiting = m_network->client->get_awaiting_frames();
|
||||||
|
|
||||||
|
// TODO: don't meddle with mode progresbar
|
||||||
|
m_modes[m_modestack.top()]->set_progress(100.0f, 100.0f * (received - awaiting) / received);
|
||||||
|
} else {
|
||||||
|
m_modes[m_modestack.top()]->set_progress(0.0f, 0.0f);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// active mode is loader
|
// active mode is loader
|
||||||
|
|
||||||
@@ -714,17 +726,22 @@ eu07_application::init_modes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool eu07_application::init_network() {
|
bool eu07_application::init_network() {
|
||||||
if (Global.network_conf.is_server || Global.network_conf.is_client) {
|
if (!Global.network_servers.empty() || Global.network_client) {
|
||||||
|
// create network manager
|
||||||
m_network.emplace();
|
m_network.emplace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Global.network_conf.is_server) {
|
for (auto const &pair : Global.network_servers) {
|
||||||
m_network->create_server(Global.network_conf.server_host, Global.network_conf.server_port);
|
// create all servers
|
||||||
|
m_network->create_server(pair.first, pair.second);
|
||||||
}
|
}
|
||||||
if (Global.network_conf.is_client) {
|
|
||||||
m_network->connect(Global.network_conf.client_host, Global.network_conf.client_port);
|
if (Global.network_client) {
|
||||||
}
|
// create client
|
||||||
else {
|
m_network->connect(Global.network_client->first, Global.network_client->second);
|
||||||
|
} else {
|
||||||
|
// we're simulation master
|
||||||
|
if (!Global.random_seed)
|
||||||
Global.random_seed = std::random_device{}();
|
Global.random_seed = std::random_device{}();
|
||||||
Global.random_engine.seed(Global.random_seed);
|
Global.random_engine.seed(Global.random_seed);
|
||||||
Global.ready_to_load = true;
|
Global.ready_to_load = true;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "network/manager.h"
|
#include "network/manager.h"
|
||||||
|
|
||||||
class eu07_application {
|
class eu07_application {
|
||||||
|
const int MAX_NETWORK_PER_FRAME = 1000;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// types
|
// types
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ driver_ui::driver_ui() {
|
|||||||
push_back( &m_debugpanel );
|
push_back( &m_debugpanel );
|
||||||
push_back( &m_transcriptspanel );
|
push_back( &m_transcriptspanel );
|
||||||
|
|
||||||
push_back( &m_vehiclelist );
|
//push_back( &m_vehiclelist );
|
||||||
push_back( &m_logpanel );
|
push_back( &m_logpanel );
|
||||||
m_logpanel.is_open = false;
|
m_logpanel.is_open = false;
|
||||||
|
|
||||||
|
|||||||
@@ -206,3 +206,36 @@ void network::tcp::client::handle_accept(const asio::error_code &err)
|
|||||||
conn->disconnect();
|
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:
|
public:
|
||||||
client(asio::io_context &io_ctx, const std::string &host, uint32_t port);
|
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 "network/manager.h"
|
||||||
#include "simulation.h"
|
#include "simulation.h"
|
||||||
#include "network/backend/asio.h"
|
|
||||||
|
|
||||||
network::server_manager::server_manager()
|
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());
|
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()
|
network::manager::manager()
|
||||||
@@ -44,20 +49,28 @@ network::manager::manager()
|
|||||||
|
|
||||||
void network::manager::update()
|
void network::manager::update()
|
||||||
{
|
{
|
||||||
io_context.restart();
|
for (auto &backend : backend_list)
|
||||||
io_context.poll();
|
backend.second->update();
|
||||||
|
|
||||||
if (client)
|
if (client)
|
||||||
client->update();
|
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.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
|
#pragma once
|
||||||
#include <asio.hpp>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "network/network.h"
|
#include "network/network.h"
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
@@ -17,21 +16,19 @@ namespace network
|
|||||||
|
|
||||||
void push_delta(double dt, double sync, const command_queue::commands_map &commands);
|
void push_delta(double dt, double sync, const command_queue::commands_map &commands);
|
||||||
command_queue::commands_map pop_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
|
class manager
|
||||||
{
|
{
|
||||||
asio::io_context io_context;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
manager();
|
manager();
|
||||||
|
|
||||||
std::optional<server_manager> servers;
|
std::optional<server_manager> servers;
|
||||||
std::shared_ptr<network::client> client;
|
std::shared_ptr<network::client> client;
|
||||||
|
|
||||||
void create_server(const std::string &host, uint32_t port);
|
void create_server(const std::string &backend, const std::string &conf);
|
||||||
void connect(const std::string &host, uint32_t port);
|
void connect(const std::string &backend, const std::string &conf);
|
||||||
void update();
|
void update();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,7 +143,8 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
|
|||||||
if (msg.type == message::CLIENT_HELLO) {
|
if (msg.type == message::CLIENT_HELLO) {
|
||||||
auto cmd = dynamic_cast<const client_hello&>(msg);
|
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();
|
conn->disconnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -227,6 +228,10 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
|
|||||||
Global.random_seed = cmd.seed;
|
Global.random_seed = cmd.seed;
|
||||||
Global.random_engine.seed(Global.random_seed);
|
Global.random_engine.seed(Global.random_seed);
|
||||||
Global.ready_to_load = true;
|
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);
|
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;
|
friend class client;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const int CATCHUP_PACKETS = 100;
|
const int CATCHUP_PACKETS = 500;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
std::queue<
|
std::queue<
|
||||||
@@ -97,5 +97,21 @@ namespace network
|
|||||||
void update();
|
void update();
|
||||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
||||||
void send_commands(command_queue::commands_map commands);
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ scenarioloader_mode::update() {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (!state) {
|
if (!state) {
|
||||||
|
WriteLog("using simulation seed: " + std::to_string(Global.random_seed), logtype::generic);
|
||||||
|
|
||||||
WriteLog( "\nLoading scenario \"" + Global.SceneryFile + "\"..." );
|
WriteLog( "\nLoading scenario \"" + Global.SceneryFile + "\"..." );
|
||||||
|
|
||||||
timestart = std::chrono::system_clock::now();
|
timestart = std::chrono::system_clock::now();
|
||||||
|
|||||||
Reference in New Issue
Block a user