16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-22 12:49:18 +02:00
This commit is contained in:
milek7
2019-01-14 00:20:14 +01:00
parent cb106fb135
commit b2a9388fab
12 changed files with 150 additions and 120 deletions

View File

@@ -140,7 +140,7 @@ void TCamera::Update()
command_data command; command_data command;
// NOTE: currently we're only storing commands for local entity and there's no id system in place, // NOTE: currently we're only storing commands for local entity and there's no id system in place,
// so we're supplying 'default' entity id of 0 // so we're supplying 'default' entity id of 0
while( simulation::Commands->pop( command, static_cast<std::size_t>( command_target::entity ) | 0 ) ) { while( simulation::Commands.pop( command, static_cast<std::size_t>( command_target::entity ) | 0 ) ) {
OnCommand( command ); OnCommand( command );
} }

View File

@@ -788,6 +788,11 @@ global_settings::ConfigParse(cParser &Parser) {
Parser.getTokens(1); Parser.getTokens(1);
Parser >> network_conf.is_server; Parser >> network_conf.is_server;
} }
else if (token == "network.client")
{
Parser.getTokens(1);
Parser >> network_conf.is_client;
}
} while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile) } while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile)
// na koniec trochę zależności // na koniec trochę zależności
if (!bLoadTraction) // wczytywanie drutów i słupów if (!bLoadTraction) // wczytywanie drutów i słupów

View File

@@ -198,7 +198,8 @@ struct global_settings {
struct network_conf_t { struct network_conf_t {
bool enabled = false; bool enabled = false;
bool is_server; bool is_server = false;
bool is_client = false;
} network_conf; } network_conf;
// methods // methods

View File

@@ -4908,7 +4908,7 @@ bool TTrain::Update( double const Deltatime )
// eventually commands are going to be retrieved directly by the vehicle, filtered through active control stand // eventually commands are going to be retrieved directly by the vehicle, filtered through active control stand
// and ultimately executed, provided the stand allows it. // and ultimately executed, provided the stand allows it.
command_data commanddata; command_data commanddata;
while( simulation::Commands->pop( commanddata, (uint32_t)command_target::vehicle | id() )) { while( simulation::Commands.pop( commanddata, (uint32_t)command_target::vehicle | id() )) {
auto lookup = m_commandhandlers.find( commanddata.command ); auto lookup = m_commandhandlers.find( commanddata.command );
if( lookup != m_commandhandlers.end() ) { if( lookup != m_commandhandlers.end() ) {

View File

@@ -188,48 +188,89 @@ eu07_application::run() {
Timer::subsystem.mainloop_total.start(); Timer::subsystem.mainloop_total.start();
glfwPollEvents(); glfwPollEvents();
if (!m_network) // -------------------------------------------------------------------
// multiplayer command relaying logic can seem a bit complex
//
// we are simultaneously:
// => master (not client) OR slave (client)
// => server OR not
//
// trivia: being client and server is possible
bool nextloop = true;
while (nextloop)
{ {
command_queue::commands_map commands_to_exec;
command_queue::commands_map local_commands = simulation::Commands.pop_intercept_queue();
double slave_sync;
// if we're the server
if (m_network && m_network->server)
{
// fetch from network layer command requests received from clients
command_queue::commands_map remote_commands = m_network->server->pop_commands();
// push these into local queue
add_to_dequemap(local_commands, remote_commands);
}
// if we're slave
if (m_network && m_network->client)
{
// fetch frame info from network layer,
auto frame_info = m_network->client->get_next_delta();
// use delta and commands received from master
double delta = std::get<0>(frame_info);
Timer::set_delta_override(delta);
slave_sync = std::get<1>(frame_info);
add_to_dequemap(commands_to_exec, std::get<2>(frame_info));
// and send our local commands to master
m_network->client->send_commands(local_commands);
if (delta == 0.0)
nextloop = false;
}
// if we're master
else {
// just push local commands to execution
add_to_dequemap(commands_to_exec, local_commands);
nextloop = false;
}
// send commands to command queue
simulation::Commands.push_commands(commands_to_exec);
// do actual frame processing
if (!m_modes[ m_modestack.top() ]->update()) if (!m_modes[ m_modestack.top() ]->update())
break; goto die;
simulation::Commands->update();
}
else if (!Global.network_conf.is_server) {
command_queue_client *queue = dynamic_cast<command_queue_client*>(simulation::Commands.get());
double delta;
do {
auto tup = m_network->get_next_delta();
delta = std::get<0>(tup);
Timer::set_delta_override(delta);
queue->push_server_commands(std::get<2>(tup));
m_network->send_commands(queue->pop_queued_commands()); // update continuous commands
simulation::Commands.update();
if (!m_modes[ m_modestack.top() ]->update()) double sync = generate_sync();
break;
queue->update(); // if we're slave
if (m_network && m_network->client)
double sync = generate_sync(); {
if (sync != std::get<1>(tup)) { // verify sync
if (sync != slave_sync) {
WriteLog("net: DESYNC!", logtype::net); WriteLog("net: DESYNC!", logtype::net);
} }
} }
while (delta != 0.0);
// if we're the server
if (m_network && m_network->server)
{
// send delta, sync, and commands we just executed to clients
double delta = Timer::GetDeltaTime();
m_network->server->push_delta(delta, sync, commands_to_exec);
}
} }
else {
command_queue_server *queue = dynamic_cast<command_queue_server*>(simulation::Commands.get());
queue->push_client_commands(m_network->pop_commands());
auto commands = queue->pop_queued_commands();
if (!m_modes[ m_modestack.top() ]->update()) // -------------------------------------------------------------------
break;
queue->update();
double delta = Timer::GetDeltaTime();
m_network->push_delta(delta, generate_sync(), commands);
}
m_taskqueue.update(); m_taskqueue.update();
opengl_texture::reset_unit_cache(); opengl_texture::reset_unit_cache();
@@ -254,6 +295,7 @@ eu07_application::run() {
if (m_network) if (m_network)
m_network->poll(); m_network->poll();
} }
die:
return 0; return 0;
} }
@@ -671,15 +713,11 @@ bool eu07_application::init_network() {
m_network.emplace(); m_network.emplace();
if (Global.network_conf.is_server) { if (Global.network_conf.is_server) {
m_network->create_server(); m_network->create_server();
simulation::Commands = std::make_unique<command_queue_server>();
} }
else { if (Global.network_conf.is_client) {
m_network->connect(); m_network->connect();
simulation::Commands = std::make_unique<command_queue_client>();
} }
} }
else
simulation::Commands = std::make_unique<command_queue>();
return true; return true;
} }

View File

@@ -18,7 +18,7 @@ http://mozilla.org/MPL/2.0/.
namespace simulation { namespace simulation {
std::unique_ptr<command_queue> Commands; command_queue Commands;
commanddescription_sequence Commands_descriptions = { commanddescription_sequence Commands_descriptions = {
{ "aidriverenable", command_target::vehicle, command_mode::oneoff }, { "aidriverenable", command_target::vehicle, command_mode::oneoff },
@@ -231,9 +231,6 @@ commanddescription_sequence Commands_descriptions = {
} // simulation } // simulation
// --------------------
// command_queue
void command_queue::update() void command_queue::update()
{ {
double delta = Timer::GetDeltaTime(); double delta = Timer::GetDeltaTime();
@@ -249,6 +246,15 @@ void command_queue::update()
// posts specified command for specified recipient // posts specified command for specified recipient
void void
command_queue::push( command_data const &Command, uint32_t const Recipient ) { command_queue::push( command_data const &Command, uint32_t const Recipient ) {
if (is_network_target(Recipient)) {
auto lookup = m_intercept_queue.emplace(Recipient, commanddata_sequence());
lookup.first->second.emplace_back(Command);
} else {
push_direct(Command, Recipient);
}
}
void command_queue::push_direct(const command_data &Command, const uint32_t Recipient) {
auto const &desc = simulation::Commands_descriptions[ static_cast<std::size_t>( Command.command ) ]; auto const &desc = simulation::Commands_descriptions[ static_cast<std::size_t>( Command.command ) ];
if (desc.mode == command_mode::continuous) if (desc.mode == command_mode::continuous)
{ {
@@ -295,55 +301,18 @@ bool command_queue::is_network_target(uint32_t const Recipient) {
return true; return true;
} }
// -------------------- command_queue::commands_map command_queue::pop_intercept_queue() {
// command_queue_server commands_map map(m_intercept_queue);
m_intercept_queue.clear();
void command_queue_server::push(const command_data &Command, const uint32_t Recipient) {
if (is_network_target(Recipient)) {
auto lookup = network_queue.emplace(Recipient, commanddata_sequence());
lookup.first->second.emplace_back(Command);
}
command_queue::push(Command, Recipient);
}
command_queue_server::commands_map command_queue_server::pop_queued_commands() {
commands_map map(network_queue);
network_queue.clear();
return map; return map;
} }
void command_queue_server::push_client_commands(const commands_map &commands) { void command_queue::push_commands(const commands_map &commands) {
for (auto const &kv : commands) for (auto const &kv : commands)
for (command_data const &data : kv.second) for (command_data const &data : kv.second)
push(data, kv.first); push_direct(data, kv.first);
} }
// --------------------
// command_queue_client
void command_queue_client::push(const command_data &Command, const uint32_t Recipient) {
if (is_network_target(Recipient)) {
auto lookup = network_queue.emplace(Recipient, commanddata_sequence());
lookup.first->second.emplace_back(Command);
}
else
command_queue::push(Command, Recipient);
}
command_queue_client::commands_map command_queue_client::pop_queued_commands() {
commands_map map(network_queue);
network_queue.clear();
return map;
}
void command_queue_client::push_server_commands(const commands_map &commands) {
for (auto const &kv : commands)
for (command_data const &data : kv.second)
command_queue::push(data, kv.first);
}
// --------------------
void void
command_relay::post( user_command const Command, double const Param1, double const Param2, command_relay::post( user_command const Command, double const Param1, double const Param2,
int const Action, uint16_t Recipient) const { int const Action, uint16_t Recipient) const {
@@ -368,5 +337,5 @@ command_relay::post( user_command const Command, double const Param1, double con
uint32_t combined_recipient = static_cast<uint32_t>( command.target ) | Recipient; uint32_t combined_recipient = static_cast<uint32_t>( command.target ) | Recipient;
command_data commanddata({Command, Action, Param1, Param2, Timer::GetDeltaTime(), FreeFlyModeFlag, Global.pCamera.Pos }); command_data commanddata({Command, Action, Param1, Param2, Timer::GetDeltaTime(), FreeFlyModeFlag, Global.pCamera.Pos });
simulation::Commands->push(commanddata, combined_recipient); simulation::Commands.push(commanddata, combined_recipient);
} }

View File

@@ -275,20 +275,36 @@ public:
typedef std::unordered_map<uint32_t, commanddata_sequence> commands_map; typedef std::unordered_map<uint32_t, commanddata_sequence> commands_map;
// methods // methods
// posts specified command for specified recipient // posts specified command for specified recipient into m_intercept_queue
virtual void void
push( command_data const &Command, uint32_t const Recipient ); push( command_data const &Command, uint32_t const Recipient );
// retrieves oldest posted command for specified recipient, if any. returns: true on retrieval, false if there's nothing to retrieve // retrieves oldest posted command for specified recipient, if any. returns: true on retrieval, false if there's nothing to retrieve
bool bool
pop( command_data &Command, uint32_t const Recipient ); pop( command_data &Command, uint32_t const Recipient );
void update(); // generates active continuous commands
bool is_network_target(const uint32_t Recipient); void
update();
// checks if given command must be scheduled on server
bool
is_network_target(const uint32_t Recipient);
// pops commands from intercept queue
commands_map pop_intercept_queue();
// pushes commands into main queue
void push_commands(const commands_map &commands);
private: private:
// members // members
// main command queue // contains command ready to execution
commands_map m_commands; commands_map m_commands;
// contains intercepted commands to be read by application layer
commands_map m_intercept_queue;
void push_direct( command_data const &Command, uint32_t const Recipient );
// hash operator for m_active_continuous // hash operator for m_active_continuous
struct command_set_hash { struct command_set_hash {
uint64_t operator() (const std::pair<user_command, uint32_t> &pair) const { uint64_t operator() (const std::pair<user_command, uint32_t> &pair) const {
@@ -300,29 +316,14 @@ private:
std::unordered_set<std::pair<user_command, uint32_t>, command_set_hash> m_active_continuous; std::unordered_set<std::pair<user_command, uint32_t>, command_set_hash> m_active_continuous;
}; };
class command_queue_server : public command_queue { template<typename A, typename B>
public: void add_to_dequemap(std::unordered_map<A, std::deque<B>> &lhs, const std::unordered_map<A, std::deque<B>> &rhs) {
void push( command_data const &Command, uint32_t const Recipient ) override; for (auto const &kv : rhs) {
auto lookup = lhs.emplace(kv.first, std::deque<B>());
commands_map pop_queued_commands(); for (B const &data : kv.second)
void push_client_commands(const commands_map &commands); lookup.first->second.emplace_back(data);
}
private: }
// contains copies of commands to be sent to clients
commands_map network_queue;
};
class command_queue_client : public command_queue {
public:
void push( command_data const &Command, uint32_t const Recipient ) override;
commands_map pop_queued_commands();
void push_server_commands(const commands_map &commands);
private:
// contains intercepted commands to be sent for execution to server
commands_map network_queue;
};
// NOTE: simulation should be a (light) wrapper rather than namespace so we could potentially instance it, // NOTE: simulation should be a (light) wrapper rather than namespace so we could potentially instance it,
// but realistically it's not like we're going to run more than one simulation at a time // but realistically it's not like we're going to run more than one simulation at a time
@@ -330,7 +331,7 @@ namespace simulation {
typedef std::vector<command_description> commanddescription_sequence; typedef std::vector<command_description> commanddescription_sequence;
extern std::unique_ptr<command_queue> Commands; extern command_queue Commands;
// TODO: add name to command map, and wrap these two into helper object // TODO: add name to command map, and wrap these two into helper object
extern commanddescription_sequence Commands_descriptions; extern commanddescription_sequence Commands_descriptions;

View File

@@ -380,7 +380,6 @@ material_manager::create( std::string const &Filename, bool const Loadnow ) {
materialhandle = m_materials.size(); materialhandle = m_materials.size();
m_materialmappings.emplace( material.name, materialhandle ); m_materialmappings.emplace( material.name, materialhandle );
m_materials.emplace_back( std::move(material) ); m_materials.emplace_back( std::move(material) );
std::cout << m_materials.size() << std::endl;
} }
else { else {
// otherwise record our failure to process the resource, to speed up subsequent attempts // otherwise record our failure to process the resource, to speed up subsequent attempts

View File

@@ -9,12 +9,13 @@ namespace network
class manager class manager
{ {
asio::io_context io_context; asio::io_context io_context;
std::shared_ptr<network::server> server;
std::shared_ptr<network::client> client;
public: public:
manager(); manager();
std::shared_ptr<network::server> server;
std::shared_ptr<network::client> client;
void create_server(); void create_server();
void connect(); void connect();
void poll(); void poll();

View File

@@ -118,6 +118,11 @@ void network::connection::send_message(std::shared_ptr<message> msg)
send_data(buf); send_data(buf);
} }
network::server::server()
{
recorder.open("recorder.bin", std::ios::trunc | std::ios::out | std::ios::binary);
}
void network::server::push_delta(double dt, double sync, command_queue::commands_map commands) void network::server::push_delta(double dt, double sync, command_queue::commands_map commands)
{ {
std::shared_ptr<delta_message> msg = std::make_shared<delta_message>(); std::shared_ptr<delta_message> msg = std::make_shared<delta_message>();
@@ -125,6 +130,12 @@ void network::server::push_delta(double dt, double sync, command_queue::commands
msg->sync = sync; msg->sync = sync;
msg->commands = commands; msg->commands = commands;
sn_utils::ls_uint32(recorder, 0x37305545);
sn_utils::ls_uint32(recorder, msg->get_size());
sn_utils::ls_uint16(recorder, (uint16_t)msg->type);
msg->serialize(recorder);
recorder.flush();
for (auto c : clients) for (auto c : clients)
c->send_message(msg); c->send_message(msg);
} }
@@ -159,6 +170,9 @@ std::tuple<double, double, command_queue::commands_map> network::client::get_nex
void network::client::send_commands(command_queue::commands_map commands) void network::client::send_commands(command_queue::commands_map commands)
{ {
if (commands.empty())
return;
std::shared_ptr<command_message> msg = std::make_shared<command_message>(); std::shared_ptr<command_message> msg = std::make_shared<command_message>();
msg->commands = commands; msg->commands = commands;

View File

@@ -41,8 +41,10 @@ namespace network
{ {
protected: protected:
std::vector<std::shared_ptr<connection>> clients; std::vector<std::shared_ptr<connection>> clients;
std::fstream recorder;
public: public:
server();
void push_delta(double dt, double sync, command_queue::commands_map commands); void push_delta(double dt, double sync, command_queue::commands_map commands);
command_queue::commands_map pop_commands(); command_queue::commands_map pop_commands();
void notify_train(std::string name); void notify_train(std::string name);

View File

@@ -77,7 +77,7 @@ state_manager::update( double const Deltatime, int Iterationcount ) {
void state_manager::process_commands() { void state_manager::process_commands() {
command_data commanddata; command_data commanddata;
while( Commands->pop( commanddata, (uint32_t)command_target::simulation )) { while( Commands.pop( commanddata, (uint32_t)command_target::simulation )) {
if (commanddata.action == GLFW_RELEASE) if (commanddata.action == GLFW_RELEASE)
continue; continue;