mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
work
This commit is contained in:
@@ -119,7 +119,6 @@ set(SOURCES
|
||||
"network/message.cpp"
|
||||
"network/manager.cpp"
|
||||
"network/backend/asio.cpp"
|
||||
"network/backend/file.cpp"
|
||||
|
||||
"widgets/vehiclelist.cpp"
|
||||
|
||||
|
||||
@@ -813,12 +813,6 @@ global_settings::ConfigParse(cParser &Parser) {
|
||||
bEnableTraction = false; // false = pantograf się nie połamie
|
||||
bLiveTraction = false; // false = pantografy zawsze zbierają 95% MaxVoltage
|
||||
}
|
||||
if (iMultiplayer > 0)
|
||||
{
|
||||
bInactivePause = false; // okno "w tle" nie może pauzować, jeśli włączona komunikacja
|
||||
// pauzowanie jest zablokowane dla (iMultiplayer&2)>0, więc iMultiplayer=1 da się zapauzować
|
||||
// (tryb instruktora)
|
||||
}
|
||||
/*
|
||||
fFpsMin = fFpsAverage -
|
||||
fFpsDeviation; // dolna granica FPS, przy której promień scenerii będzie zmniejszany
|
||||
|
||||
@@ -57,12 +57,7 @@ extern WNDPROC BaseWindowProc;
|
||||
// user input callbacks
|
||||
|
||||
void focus_callback( GLFWwindow *window, int focus ) {
|
||||
if( Global.bInactivePause ) {// jeśli ma być pauzowanie okna w tle
|
||||
if( focus )
|
||||
Global.iPause &= ~4; // odpauzowanie, gdy jest na pierwszym planie
|
||||
else
|
||||
Global.iPause |= 4; // włączenie pauzy, gdy nieaktywy
|
||||
}
|
||||
Application.on_focus_change(focus != 0);
|
||||
}
|
||||
|
||||
void window_resize_callback( GLFWwindow *window, int w, int h ) {
|
||||
@@ -329,7 +324,6 @@ eu07_application::render_ui() {
|
||||
|
||||
bool
|
||||
eu07_application::pop_mode() {
|
||||
|
||||
if( m_modestack.empty() ) { return false; }
|
||||
|
||||
m_modes[ m_modestack.top() ]->exit();
|
||||
@@ -434,6 +428,13 @@ void eu07_application::on_char(unsigned int c) {
|
||||
return;
|
||||
}
|
||||
|
||||
void eu07_application::on_focus_change(bool focus) {
|
||||
if( Global.bInactivePause && !m_network->client) {// jeśli ma być pauzowanie okna w tle
|
||||
command_relay relay;
|
||||
relay.post(user_command::focuspauseset, focus ? 1.0 : 0.0, 0.0, GLFW_PRESS, 0);
|
||||
}
|
||||
}
|
||||
|
||||
GLFWwindow *
|
||||
eu07_application::window( int const Windowindex ) {
|
||||
|
||||
|
||||
@@ -71,7 +71,10 @@ public:
|
||||
on_mouse_button( int const Button, int const Action, int const Mods );
|
||||
void
|
||||
on_scroll( double const Xoffset, double const Yoffset );
|
||||
void on_char(unsigned int c);
|
||||
void
|
||||
on_char(unsigned int c);
|
||||
void
|
||||
on_focus_change(bool focus);
|
||||
// gives access to specified window, creates a new window if index == -1
|
||||
GLFWwindow *
|
||||
window( int const Windowindex = 0 );
|
||||
|
||||
@@ -227,6 +227,7 @@ commanddescription_sequence Commands_descriptions = {
|
||||
{ "vehiclemovebackwards", command_target::vehicle, command_mode::oneoff },
|
||||
{ "vehicleboost", command_target::vehicle, command_mode::oneoff },
|
||||
{ "debugtoggle", command_target::simulation, command_mode::oneoff },
|
||||
{ "focuspauseset", command_target::simulation, command_mode::oneoff },
|
||||
{ "pausetoggle", command_target::simulation, command_mode::oneoff },
|
||||
{ "entervehicle", command_target::simulation, command_mode::oneoff },
|
||||
{ "queueevent", command_target::simulation, command_mode::oneoff },
|
||||
|
||||
@@ -221,6 +221,7 @@ enum class user_command {
|
||||
vehiclemovebackwards,
|
||||
vehicleboost,
|
||||
debugtoggle,
|
||||
focuspauseset,
|
||||
pausetoggle,
|
||||
entervehicle,
|
||||
queueevent,
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
#include "sn_utils.h"
|
||||
#include "Logs.h"
|
||||
|
||||
network::tcp::connection::connection(asio::io_context &io_ctx, bool client)
|
||||
: network::connection(client), m_socket(io_ctx)
|
||||
network::tcp::connection::connection(asio::io_context &io_ctx, bool client, size_t counter)
|
||||
: network::connection(client, counter), m_socket(io_ctx)
|
||||
{
|
||||
m_header_buffer.resize(8);
|
||||
}
|
||||
@@ -16,9 +16,7 @@ void network::tcp::connection::disconnect()
|
||||
|
||||
void network::tcp::connection::send_data(std::shared_ptr<std::string> buffer)
|
||||
{
|
||||
asio::async_write(m_socket, asio::buffer(*buffer.get()), std::bind(&connection::handle_send, this, buffer,
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
asio::async_write(m_socket, asio::buffer(*buffer.get()), std::bind(&connection::send_complete, this, buffer));
|
||||
}
|
||||
|
||||
void network::tcp::connection::connected()
|
||||
@@ -34,12 +32,6 @@ void network::tcp::connection::read_header()
|
||||
std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
void network::tcp::connection::handle_send(
|
||||
std::shared_ptr<std::string> buf, const asio::error_code &err, size_t bytes_transferred)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void network::tcp::connection::handle_header(const asio::error_code &err, size_t bytes_transferred)
|
||||
{
|
||||
std::istringstream header(m_header_buffer);
|
||||
@@ -80,32 +72,52 @@ void network::tcp::connection::handle_data(const asio::error_code &err, size_t b
|
||||
read_header();
|
||||
}
|
||||
|
||||
void network::tcp::connection::send_message(const message &msg)
|
||||
void network::tcp::connection::write_message(const message &msg, std::ostream &stream)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
size_t beg = (size_t)stream.tellp();
|
||||
|
||||
sn_utils::ls_uint32(stream, NETWORK_MAGIC);
|
||||
sn_utils::ls_uint32(stream, 0);
|
||||
|
||||
serialize_message(msg, stream);
|
||||
|
||||
size_t size = (size_t)stream.tellp() - 8;
|
||||
size_t size = (size_t)stream.tellp() - beg - 8;
|
||||
if (size > MAX_MSG_SIZE) {
|
||||
ErrorLog("net: message too big", logtype::net);
|
||||
return;
|
||||
}
|
||||
stream.seekp(4, std::ios_base::beg);
|
||||
stream.seekp(beg + 4, std::ios_base::beg);
|
||||
sn_utils::ls_uint32(stream, size);
|
||||
|
||||
stream.flush();
|
||||
stream.seekp(0, std::ios_base::end);
|
||||
}
|
||||
|
||||
std::shared_ptr<std::string> buf = std::make_shared<std::string>(stream.str());
|
||||
send_data(buf);
|
||||
void network::tcp::connection::send_messages(const std::vector<std::shared_ptr<message> > &messages)
|
||||
{
|
||||
if (messages.size() == 0)
|
||||
return;
|
||||
|
||||
std::ostringstream stream;
|
||||
for (auto const &msg : messages)
|
||||
write_message(*msg.get(), stream);
|
||||
|
||||
stream.flush();
|
||||
send_data(std::make_shared<std::string>(stream.str()));
|
||||
}
|
||||
|
||||
void network::tcp::connection::send_message(const message &msg)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
write_message(msg, stream);
|
||||
|
||||
stream.flush();
|
||||
send_data(std::make_shared<std::string>(stream.str()));
|
||||
}
|
||||
|
||||
// -----------------
|
||||
|
||||
network::tcp::server::server(asio::io_context &io_ctx, const std::string &host, uint32_t port)
|
||||
: m_acceptor(io_ctx)
|
||||
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)
|
||||
{
|
||||
auto endpoint = asio::ip::tcp::endpoint(asio::ip::address::from_string(host), port);
|
||||
m_acceptor.open(endpoint.protocol());
|
||||
@@ -144,7 +156,7 @@ void network::tcp::server::handle_accept(std::shared_ptr<connection> conn, const
|
||||
|
||||
network::tcp::client::client(asio::io_context &io_ctx, const std::string &host, uint32_t port)
|
||||
{
|
||||
std::shared_ptr<connection> conn = std::make_shared<connection>(io_ctx, true);
|
||||
std::shared_ptr<connection> conn = std::make_shared<connection>(io_ctx, true, messages_counter);
|
||||
conn->set_handler(std::bind(&client::handle_message, this, std::ref(*conn.get()), std::placeholders::_1));
|
||||
|
||||
asio::ip::tcp::endpoint endpoint(
|
||||
|
||||
@@ -12,11 +12,11 @@ namespace network::tcp
|
||||
friend class client;
|
||||
|
||||
public:
|
||||
connection(asio::io_context &io_ctx, bool client = false);
|
||||
connection(asio::io_context &io_ctx, bool client = false, size_t counter = 0);
|
||||
|
||||
virtual void connected() override;
|
||||
virtual void disconnect() override;
|
||||
virtual void send_data(std::shared_ptr<std::string> buffer) 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;
|
||||
@@ -25,8 +25,9 @@ namespace network::tcp
|
||||
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_send(std::shared_ptr<std::string> buf, const asio::error_code &err, size_t bytes_transferred);
|
||||
void handle_header(const asio::error_code &err, size_t bytes_transferred);
|
||||
void handle_data(const asio::error_code &err, size_t bytes_transferred);
|
||||
};
|
||||
@@ -40,7 +41,7 @@ namespace network::tcp
|
||||
asio::ip::tcp::acceptor m_acceptor;
|
||||
|
||||
public:
|
||||
server(asio::io_context &io_ctx, const std::string &host, uint32_t port);
|
||||
server(std::shared_ptr<std::istream> buf, asio::io_context &io_ctx, const std::string &host, uint32_t port);
|
||||
};
|
||||
|
||||
class client : public network::client
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#include "network/network.h"
|
||||
|
||||
namespace network::file
|
||||
{
|
||||
class server : public network::server
|
||||
{
|
||||
private:
|
||||
std::fstream stream;
|
||||
|
||||
public:
|
||||
server();
|
||||
};
|
||||
|
||||
class client : public network::client
|
||||
{
|
||||
private:
|
||||
std::fstream stream;
|
||||
|
||||
public:
|
||||
client();
|
||||
};
|
||||
}
|
||||
@@ -2,6 +2,11 @@
|
||||
#include "simulation.h"
|
||||
#include "network/backend/asio.h"
|
||||
|
||||
network::server_manager::server_manager()
|
||||
{
|
||||
backbuffer = std::make_shared<std::fstream>("backbuffer.bin", std::ios::out | std::ios::in | std::ios::trunc | std::ios::binary);
|
||||
}
|
||||
|
||||
command_queue::commands_map network::server_manager::pop_commands()
|
||||
{
|
||||
command_queue::commands_map map;
|
||||
@@ -14,13 +19,29 @@ command_queue::commands_map network::server_manager::pop_commands()
|
||||
|
||||
void network::server_manager::push_delta(double dt, double sync, const command_queue::commands_map &commands)
|
||||
{
|
||||
if (dt == 0.0 && commands.empty())
|
||||
return;
|
||||
|
||||
for (auto srv : servers)
|
||||
srv->push_delta(dt, sync, commands);
|
||||
|
||||
frame_info msg;
|
||||
msg.dt = dt;
|
||||
msg.sync = sync;
|
||||
msg.commands = commands;
|
||||
|
||||
serialize_message(msg, *backbuffer.get());
|
||||
}
|
||||
|
||||
void network::server_manager::update()
|
||||
{
|
||||
for (auto srv : servers)
|
||||
srv->update();
|
||||
}
|
||||
|
||||
void network::server_manager::create_server(asio::io_context &ctx, const std::string &host, uint32_t port)
|
||||
{
|
||||
servers.emplace_back(std::make_shared<tcp::server>(ctx, host, port));
|
||||
servers.emplace_back(std::make_shared<tcp::server>(backbuffer, ctx, host, port));
|
||||
}
|
||||
|
||||
network::manager::manager()
|
||||
@@ -30,6 +51,7 @@ network::manager::manager()
|
||||
void network::manager::poll()
|
||||
{
|
||||
io_context.poll();
|
||||
servers->update();
|
||||
}
|
||||
|
||||
void network::manager::create_server(const std::string &host, uint32_t port)
|
||||
|
||||
@@ -10,11 +10,15 @@ namespace network
|
||||
{
|
||||
private:
|
||||
std::vector<std::shared_ptr<server>> servers;
|
||||
std::shared_ptr<std::fstream> backbuffer;
|
||||
|
||||
public:
|
||||
server_manager();
|
||||
|
||||
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 update();
|
||||
};
|
||||
|
||||
class manager
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
void network::client_hello::serialize(std::ostream &stream) const
|
||||
{
|
||||
sn_utils::ls_int32(stream, version);
|
||||
sn_utils::ls_uint32(stream, start_packet);
|
||||
}
|
||||
|
||||
void network::client_hello::deserialize(std::istream &stream)
|
||||
{
|
||||
version = sn_utils::ld_int32(stream);
|
||||
start_packet = sn_utils::ld_uint32(stream);
|
||||
}
|
||||
|
||||
void network::server_hello::serialize(std::ostream &stream) const
|
||||
|
||||
@@ -31,6 +31,7 @@ struct client_hello : public message
|
||||
virtual void deserialize(std::istream &stream) override;
|
||||
|
||||
int32_t version;
|
||||
uint32_t start_packet;
|
||||
};
|
||||
|
||||
struct server_hello : public message
|
||||
|
||||
@@ -16,7 +16,8 @@ void network::connection::set_handler(std::function<void (const message &)> hand
|
||||
message_handler = handler;
|
||||
}
|
||||
|
||||
network::connection::connection(bool client) {
|
||||
network::connection::connection(bool client, size_t counter) {
|
||||
packet_counter = counter;
|
||||
is_client = client;
|
||||
state = AWAITING_HELLO;
|
||||
}
|
||||
@@ -28,10 +29,47 @@ void network::connection::connected()
|
||||
if (is_client) {
|
||||
client_hello msg;
|
||||
msg.version = 1;
|
||||
msg.start_packet = packet_counter;
|
||||
send_message(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void network::connection::catch_up()
|
||||
{
|
||||
backbuffer->seekg(backbuffer_pos);
|
||||
|
||||
std::vector<std::shared_ptr<message>> messages;
|
||||
|
||||
for (size_t i = 0; i < CATCHUP_PACKETS; i++) {
|
||||
if (backbuffer->peek() == EOF) {
|
||||
send_messages(messages);
|
||||
backbuffer->seekg(0, std::ios_base::end);
|
||||
state = ACTIVE;
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet_counter) {
|
||||
packet_counter--;
|
||||
continue;
|
||||
}
|
||||
|
||||
messages.push_back(deserialize_message(*backbuffer.get()));
|
||||
}
|
||||
|
||||
send_messages(messages);
|
||||
|
||||
backbuffer_pos = backbuffer->tellg();
|
||||
|
||||
backbuffer->seekg(0, std::ios_base::end);
|
||||
}
|
||||
|
||||
void network::connection::send_complete(std::shared_ptr<std::string> buf)
|
||||
{
|
||||
if (!is_client && state == CATCHING_UP) {
|
||||
catch_up();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if (msg->type == message::TYPE_MAX)
|
||||
{
|
||||
@@ -113,11 +151,13 @@ command_queue::commands_map network::connection::pop_commands()
|
||||
|
||||
// server
|
||||
|
||||
network::server::server(std::shared_ptr<std::istream> buf) : backbuffer(buf)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void network::server::push_delta(double dt, double sync, const command_queue::commands_map &commands)
|
||||
{
|
||||
if (dt == 0.0 && commands.empty())
|
||||
return;
|
||||
|
||||
frame_info msg;
|
||||
msg.dt = dt;
|
||||
msg.sync = sync;
|
||||
@@ -128,6 +168,11 @@ void network::server::push_delta(double dt, double sync, const command_queue::co
|
||||
c->send_message(msg);
|
||||
}
|
||||
|
||||
void network::server::update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
command_queue::commands_map network::server::pop_commands()
|
||||
{
|
||||
command_queue::commands_map map(client_commands_queue);
|
||||
@@ -144,9 +189,14 @@ void network::server::handle_message(connection &conn, const message &msg)
|
||||
}
|
||||
|
||||
if (msg.type == message::CLIENT_HELLO) {
|
||||
auto cmd = dynamic_cast<const client_hello&>(msg);
|
||||
|
||||
server_hello reply;
|
||||
reply.seed = Global.random_seed;
|
||||
conn.state = connection::ACTIVE;
|
||||
conn.state = connection::CATCHING_UP;
|
||||
conn.backbuffer = backbuffer;
|
||||
conn.backbuffer_pos = 0;
|
||||
conn.packet_counter = cmd.start_packet;
|
||||
|
||||
conn.send_message(reply);
|
||||
|
||||
@@ -190,16 +240,18 @@ void network::client::send_commands(command_queue::commands_map commands)
|
||||
|
||||
void network::client::handle_message(connection &conn, const message &msg)
|
||||
{
|
||||
if (msg.type == message::TYPE_MAX)
|
||||
if (msg.type >= message::TYPE_MAX)
|
||||
{
|
||||
conn.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
messages_counter++;
|
||||
|
||||
if (msg.type == message::SERVER_HELLO) {
|
||||
auto cmd = dynamic_cast<const server_hello&>(msg);
|
||||
|
||||
conn.state = connection::ACTIVE;
|
||||
|
||||
Global.random_seed = cmd.seed;
|
||||
Global.random_engine.seed(Global.random_seed);
|
||||
|
||||
|
||||
@@ -9,9 +9,15 @@
|
||||
|
||||
namespace network
|
||||
{
|
||||
//m7todo: separate client/server connection class?
|
||||
class connection
|
||||
{
|
||||
friend class server;
|
||||
friend class client;
|
||||
|
||||
private:
|
||||
const size_t CATCHUP_PACKETS = 100;
|
||||
|
||||
/*
|
||||
std::queue<
|
||||
std::pair<std::chrono::high_resolution_clock::time_point,
|
||||
@@ -26,18 +32,24 @@ namespace network
|
||||
bool is_client;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<std::istream> backbuffer;
|
||||
size_t backbuffer_pos;
|
||||
size_t packet_counter;
|
||||
|
||||
void send_complete(std::shared_ptr<std::string> buf);
|
||||
void catch_up();
|
||||
|
||||
public:
|
||||
std::function<void(const message &msg)> message_handler;
|
||||
|
||||
virtual void connected() = 0;
|
||||
virtual void send_message(const message &msg) = 0;
|
||||
virtual void send_messages(const std::vector<std::shared_ptr<message>> &messages) = 0;
|
||||
|
||||
connection(bool client = false);
|
||||
connection(bool client = false, size_t counter = 0);
|
||||
void set_handler(std::function<void(const message &msg)> handler);
|
||||
|
||||
virtual void disconnect() = 0;
|
||||
virtual void send_data(std::shared_ptr<std::string> buffer) = 0;
|
||||
|
||||
enum peer_state {
|
||||
AWAITING_HELLO,
|
||||
@@ -50,6 +62,9 @@ namespace network
|
||||
|
||||
class server
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<std::istream> backbuffer;
|
||||
|
||||
protected:
|
||||
void handle_message(connection &conn, const message &msg);
|
||||
|
||||
@@ -58,8 +73,10 @@ namespace network
|
||||
command_queue::commands_map client_commands_queue;
|
||||
|
||||
public:
|
||||
server(std::shared_ptr<std::istream> buf);
|
||||
void push_delta(double dt, double sync, const command_queue::commands_map &commands);
|
||||
command_queue::commands_map pop_commands();
|
||||
void update();
|
||||
};
|
||||
|
||||
class client
|
||||
@@ -67,6 +84,7 @@ namespace network
|
||||
protected:
|
||||
void handle_message(connection &conn, const message &msg);
|
||||
std::shared_ptr<connection> conn;
|
||||
size_t messages_counter = 0;
|
||||
|
||||
std::queue<
|
||||
std::pair<std::chrono::high_resolution_clock::time_point,
|
||||
|
||||
@@ -89,6 +89,13 @@ void state_manager::process_commands() {
|
||||
}
|
||||
}
|
||||
|
||||
if (commanddata.command == user_command::focuspauseset) {
|
||||
if( commanddata.param1 == 1.0 )
|
||||
Global.iPause &= ~4; // odpauzowanie, gdy jest na pierwszym planie
|
||||
else
|
||||
Global.iPause |= 4; // włączenie pauzy, gdy nieaktywy
|
||||
}
|
||||
|
||||
if (commanddata.command == user_command::entervehicle) {
|
||||
// przesiadka do innego pojazdu
|
||||
if (!commanddata.freefly)
|
||||
|
||||
Reference in New Issue
Block a user