mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
work
This commit is contained in:
@@ -21,14 +21,14 @@ void network::manager::connect()
|
||||
client = std::make_shared<tcp_client>(io_context);
|
||||
}
|
||||
|
||||
std::tuple<double, command_queue::commands_map> network::manager::get_next_delta()
|
||||
std::tuple<double, double, command_queue::commands_map> network::manager::get_next_delta()
|
||||
{
|
||||
return client->get_next_delta();
|
||||
}
|
||||
|
||||
void network::manager::push_delta(double delta, command_queue::commands_map commands)
|
||||
void network::manager::push_delta(double delta, double sync, command_queue::commands_map commands)
|
||||
{
|
||||
server->push_delta(delta, commands);
|
||||
server->push_delta(delta, sync, commands);
|
||||
}
|
||||
|
||||
command_queue::commands_map network::manager::pop_commands()
|
||||
|
||||
@@ -19,8 +19,8 @@ namespace network
|
||||
void connect();
|
||||
void poll();
|
||||
|
||||
std::tuple<double, command_queue::commands_map> get_next_delta();
|
||||
void push_delta(double delta, command_queue::commands_map commands);
|
||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
||||
void push_delta(double delta, double sync, command_queue::commands_map commands);
|
||||
command_queue::commands_map pop_commands();
|
||||
void send_commands(command_queue::commands_map commands);
|
||||
void request_train(std::string name);
|
||||
|
||||
@@ -11,6 +11,21 @@ void network::message::deserialize(std::istream &stream)
|
||||
|
||||
}
|
||||
|
||||
void network::accept_message::serialize(std::ostream &stream)
|
||||
{
|
||||
sn_utils::ls_uint32(stream, seed);
|
||||
}
|
||||
|
||||
void network::accept_message::deserialize(std::istream &stream)
|
||||
{
|
||||
seed = sn_utils::ld_uint32(stream);
|
||||
}
|
||||
|
||||
size_t network::accept_message::get_size()
|
||||
{
|
||||
return message::get_size() + 4;
|
||||
}
|
||||
|
||||
void::network::command_message::serialize(std::ostream &stream)
|
||||
{
|
||||
sn_utils::ls_uint32(stream, commands.size());
|
||||
@@ -66,12 +81,13 @@ size_t network::command_message::get_size()
|
||||
|
||||
size_t network::delta_message::get_size()
|
||||
{
|
||||
return command_message::get_size() + 8;
|
||||
return command_message::get_size() + 16;
|
||||
}
|
||||
|
||||
void network::delta_message::serialize(std::ostream &stream)
|
||||
{
|
||||
sn_utils::ls_float64(stream, dt);
|
||||
sn_utils::ls_float64(stream, sync);
|
||||
|
||||
command_message::serialize(stream);
|
||||
}
|
||||
@@ -79,6 +95,7 @@ void network::delta_message::serialize(std::ostream &stream)
|
||||
void network::delta_message::deserialize(std::istream &stream)
|
||||
{
|
||||
dt = sn_utils::ld_float64(stream);
|
||||
sync = sn_utils::ld_float64(stream);
|
||||
|
||||
command_message::deserialize(stream);
|
||||
}
|
||||
@@ -103,7 +120,13 @@ std::shared_ptr<network::message> network::deserialize_message(std::istream &str
|
||||
message::type_e type = (message::type_e)sn_utils::ld_uint16(stream);
|
||||
|
||||
std::shared_ptr<message> msg;
|
||||
if (type == message::STEP_INFO) {
|
||||
if (type == message::CONNECT_ACCEPT) {
|
||||
auto m = std::make_shared<accept_message>();
|
||||
m->type = type;
|
||||
m->deserialize(stream);
|
||||
msg = m;
|
||||
}
|
||||
else if (type == message::STEP_INFO) {
|
||||
auto m = std::make_shared<delta_message>();
|
||||
m->type = type;
|
||||
m->deserialize(stream);
|
||||
|
||||
@@ -26,6 +26,17 @@ namespace network
|
||||
virtual size_t get_size() { return 2; }
|
||||
};
|
||||
|
||||
struct accept_message : public message
|
||||
{
|
||||
accept_message() : message(CONNECT_ACCEPT) {}
|
||||
|
||||
uint32_t seed;
|
||||
|
||||
virtual void serialize(std::ostream &stream) override;
|
||||
virtual void deserialize(std::istream &stream) override;
|
||||
virtual size_t get_size() override;
|
||||
};
|
||||
|
||||
struct command_message : public message
|
||||
{
|
||||
command_message() : message(CLIENT_COMMAND) {}
|
||||
@@ -43,6 +54,7 @@ namespace network
|
||||
delta_message() : command_message(STEP_INFO) {}
|
||||
|
||||
double dt;
|
||||
double sync;
|
||||
|
||||
virtual void serialize(std::ostream &stream) override;
|
||||
virtual void deserialize(std::istream &stream) override;
|
||||
|
||||
@@ -9,8 +9,10 @@ void network::connection::connected()
|
||||
{
|
||||
WriteLog("net: socket connected", logtype::net);
|
||||
|
||||
std::shared_ptr<message> hello = std::make_shared<message>(message::CONNECT_REQUEST);
|
||||
send_message(hello);
|
||||
if (!Global.network_conf.is_server) {
|
||||
std::shared_ptr<message> hello = std::make_shared<message>(message::CONNECT_REQUEST);
|
||||
send_message(hello);
|
||||
}
|
||||
}
|
||||
|
||||
void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
@@ -23,7 +25,8 @@ void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
|
||||
if (msg->type == message::CONNECT_REQUEST)
|
||||
{
|
||||
std::shared_ptr<message> reply = std::make_shared<message>(message::CONNECT_ACCEPT);
|
||||
std::shared_ptr<accept_message> reply = std::make_shared<accept_message>();
|
||||
reply->seed = Global.random_seed;
|
||||
|
||||
WriteLog("client accepted", logtype::net);
|
||||
|
||||
@@ -31,7 +34,9 @@ void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
}
|
||||
else if (msg->type == message::CONNECT_ACCEPT)
|
||||
{
|
||||
auto cmd = std::dynamic_pointer_cast<accept_message>(msg);
|
||||
WriteLog("accept received", logtype::net);
|
||||
//Global.random_engine.seed(cmd->seed);
|
||||
}
|
||||
else if (msg->type == message::CLIENT_COMMAND)
|
||||
{
|
||||
@@ -57,11 +62,11 @@ void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<double, command_queue::commands_map> network::connection::get_next_delta()
|
||||
std::tuple<double, double, command_queue::commands_map> network::connection::get_next_delta()
|
||||
{
|
||||
if (delta_queue.empty()) {
|
||||
return std::tuple<double,
|
||||
command_queue::commands_map>(0.0, command_queue::commands_map());
|
||||
return std::tuple<double, double,
|
||||
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
||||
}
|
||||
|
||||
///auto now = std::chrono::high_resolution_clock::now();
|
||||
@@ -76,7 +81,7 @@ std::tuple<double, command_queue::commands_map> network::connection::get_next_de
|
||||
//accum -= remote_dt;
|
||||
|
||||
delta_queue.pop();
|
||||
return std::make_tuple(entry.second->dt, entry.second->commands);
|
||||
return std::make_tuple(entry.second->dt, entry.second->sync, entry.second->commands);
|
||||
//}
|
||||
//else {
|
||||
//return 0.0;
|
||||
@@ -111,10 +116,11 @@ void network::connection::send_message(std::shared_ptr<message> msg)
|
||||
send_data(buf);
|
||||
}
|
||||
|
||||
void network::server::push_delta(double dt, 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>();
|
||||
msg->dt = dt;
|
||||
msg->sync = sync;
|
||||
msg->commands = commands;
|
||||
|
||||
for (auto c : clients)
|
||||
@@ -144,7 +150,7 @@ command_queue::commands_map network::server::pop_commands()
|
||||
return map;
|
||||
}
|
||||
|
||||
std::tuple<double, command_queue::commands_map> network::client::get_next_delta()
|
||||
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta()
|
||||
{
|
||||
return conn->get_next_delta();
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace network
|
||||
void send_message(std::shared_ptr<message> msg);
|
||||
virtual void connected();
|
||||
|
||||
std::tuple<double, command_queue::commands_map> get_next_delta();
|
||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
||||
command_queue::commands_map pop_commands();
|
||||
};
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace network
|
||||
std::vector<std::shared_ptr<connection>> clients;
|
||||
|
||||
public:
|
||||
void push_delta(double dt, command_queue::commands_map commands);
|
||||
void push_delta(double dt, double sync, command_queue::commands_map commands);
|
||||
command_queue::commands_map pop_commands();
|
||||
void notify_train(std::string name);
|
||||
};
|
||||
@@ -54,7 +54,7 @@ namespace network
|
||||
std::shared_ptr<connection> conn;
|
||||
|
||||
public:
|
||||
std::tuple<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 request_train(std::string name);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user