mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
work
This commit is contained in:
@@ -40,29 +40,3 @@ void network::manager::send_commands(command_queue::commands_map commands)
|
||||
{
|
||||
client->send_commands(commands);
|
||||
}
|
||||
|
||||
void network::manager::request_train(std::string name)
|
||||
{
|
||||
if (server) {
|
||||
TTrain *train = simulation::Trains.find(name);
|
||||
if (train)
|
||||
return;
|
||||
|
||||
TDynamicObject *dynobj = simulation::Vehicles.find(name);
|
||||
if (!dynobj)
|
||||
return;
|
||||
|
||||
train = new TTrain();
|
||||
if (train->Init(dynobj)) {
|
||||
simulation::Trains.insert(train, name);
|
||||
server->notify_train(name);
|
||||
}
|
||||
else {
|
||||
delete train;
|
||||
train = nullptr;
|
||||
}
|
||||
}
|
||||
if (client) {
|
||||
client->request_train(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,5 @@ namespace network
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -144,12 +144,6 @@ std::shared_ptr<network::message> network::deserialize_message(std::istream &str
|
||||
m->deserialize(stream);
|
||||
msg = m;
|
||||
}
|
||||
else if (type == message::REQUEST_SPAWN_TRAIN || type == message::SPAWN_TRAIN) {
|
||||
auto m = std::make_shared<string_message>(type);
|
||||
m->type = type;
|
||||
m->deserialize(stream);
|
||||
msg = m;
|
||||
}
|
||||
else {
|
||||
msg = std::make_shared<message>(type);
|
||||
}
|
||||
|
||||
@@ -13,8 +13,6 @@ namespace network
|
||||
CONNECT_ACCEPT,
|
||||
STEP_INFO,
|
||||
CLIENT_COMMAND,
|
||||
REQUEST_SPAWN_TRAIN,
|
||||
SPAWN_TRAIN,
|
||||
TYPE_MAX
|
||||
};
|
||||
|
||||
|
||||
@@ -5,11 +5,15 @@
|
||||
#include "Timer.h"
|
||||
#include "application.h"
|
||||
|
||||
network::connection::connection(bool client) {
|
||||
is_client = client;
|
||||
}
|
||||
|
||||
void network::connection::connected()
|
||||
{
|
||||
WriteLog("net: socket connected", logtype::net);
|
||||
|
||||
if (!Global.network_conf.is_server) {
|
||||
if (is_client) {
|
||||
std::shared_ptr<message> hello = std::make_shared<message>(message::CONNECT_REQUEST);
|
||||
send_message(hello);
|
||||
}
|
||||
@@ -52,16 +56,6 @@ void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
delta_queue.push(std::make_pair(now, delta));
|
||||
}
|
||||
else if (msg->type == message::REQUEST_SPAWN_TRAIN)
|
||||
{
|
||||
auto req = std::dynamic_pointer_cast<string_message>(msg);
|
||||
Application.request_train(req->name);
|
||||
}
|
||||
else if (msg->type == message::SPAWN_TRAIN)
|
||||
{
|
||||
auto req = std::dynamic_pointer_cast<string_message>(msg);
|
||||
Application.spawn_train(req->name);
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<double, double, command_queue::commands_map> network::connection::get_next_delta()
|
||||
@@ -125,6 +119,9 @@ network::server::server()
|
||||
|
||||
void network::server::push_delta(double dt, double sync, command_queue::commands_map commands)
|
||||
{
|
||||
if (dt == 0.0 && commands.empty())
|
||||
return;
|
||||
|
||||
std::shared_ptr<delta_message> msg = std::make_shared<delta_message>();
|
||||
msg->dt = dt;
|
||||
msg->sync = sync;
|
||||
@@ -140,15 +137,6 @@ void network::server::push_delta(double dt, double sync, command_queue::commands
|
||||
c->send_message(msg);
|
||||
}
|
||||
|
||||
void network::server::notify_train(std::string name)
|
||||
{
|
||||
std::shared_ptr<string_message> msg = std::make_shared<string_message>(message::SPAWN_TRAIN);
|
||||
msg->name = name;
|
||||
|
||||
for (auto c : clients)
|
||||
c->send_message(msg);
|
||||
}
|
||||
|
||||
command_queue::commands_map network::server::pop_commands()
|
||||
{
|
||||
command_queue::commands_map map;
|
||||
@@ -178,11 +166,3 @@ void network::client::send_commands(command_queue::commands_map commands)
|
||||
|
||||
conn->send_message(msg);
|
||||
}
|
||||
|
||||
void network::client::request_train(std::string name)
|
||||
{
|
||||
std::shared_ptr<string_message> msg = std::make_shared<string_message>(message::REQUEST_SPAWN_TRAIN);
|
||||
msg->name = name;
|
||||
|
||||
conn->send_message(msg);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ namespace network
|
||||
std::shared_ptr<delta_message>>> delta_queue;
|
||||
|
||||
command_queue::commands_map client_commands_queue;
|
||||
bool is_client;
|
||||
|
||||
//std::chrono::high_resolution_clock::time_point last_time;
|
||||
//double accum = -1.0;
|
||||
@@ -30,6 +31,7 @@ namespace network
|
||||
void data_received(std::string &buffer);
|
||||
|
||||
public:
|
||||
connection(bool client = false);
|
||||
void send_message(std::shared_ptr<message> msg);
|
||||
virtual void connected();
|
||||
|
||||
@@ -47,7 +49,6 @@ namespace network
|
||||
server();
|
||||
void push_delta(double dt, double sync, command_queue::commands_map commands);
|
||||
command_queue::commands_map pop_commands();
|
||||
void notify_train(std::string name);
|
||||
};
|
||||
|
||||
class client
|
||||
@@ -58,6 +59,5 @@ namespace network
|
||||
public:
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#include "network/tcp.h"
|
||||
#include "Logs.h"
|
||||
#include "sn_utils.h"
|
||||
#include "Globals.h"
|
||||
|
||||
network::tcp_conn::tcp_conn(asio::io_context &io_ctx)
|
||||
: m_socket(io_ctx)
|
||||
network::tcp_conn::tcp_conn(asio::io_context &io_ctx, bool client)
|
||||
: connection(client), m_socket(io_ctx)
|
||||
{
|
||||
m_header_buffer.resize(8);
|
||||
}
|
||||
@@ -82,10 +83,9 @@ asio::ip::tcp::socket& network::tcp_conn::socket()
|
||||
network::tcp_server::tcp_server(asio::io_context &io_ctx)
|
||||
: m_acceptor(io_ctx)
|
||||
{
|
||||
auto endpoint = asio::ip::tcp::endpoint(asio::ip::tcp::v6(), 7424);
|
||||
auto endpoint = asio::ip::tcp::endpoint(asio::ip::address::from_string(Global.network_conf.server_host), Global.network_conf.server_port);
|
||||
m_acceptor.open(endpoint.protocol());
|
||||
m_acceptor.set_option(asio::socket_base::reuse_address(true));
|
||||
m_acceptor.set_option(asio::ip::v6_only(false));
|
||||
m_acceptor.set_option(asio::ip::tcp::no_delay(true));
|
||||
m_acceptor.bind(endpoint);
|
||||
m_acceptor.listen(10);
|
||||
@@ -117,11 +117,11 @@ void network::tcp_server::handle_accept(std::shared_ptr<tcp_conn> conn, const as
|
||||
|
||||
network::tcp_client::tcp_client(asio::io_context &io_ctx)
|
||||
{
|
||||
conn = std::make_shared<tcp_conn>(io_ctx);
|
||||
conn = std::make_shared<tcp_conn>(io_ctx, true);
|
||||
auto tcpconn = std::static_pointer_cast<tcp_conn>(conn);
|
||||
|
||||
asio::ip::tcp::endpoint endpoint(
|
||||
asio::ip::address::from_string("192.168.0.20"), 7424);
|
||||
asio::ip::address::from_string(Global.network_conf.client_host), Global.network_conf.client_port);
|
||||
tcpconn->socket().open(endpoint.protocol());
|
||||
tcpconn->socket().set_option(asio::ip::tcp::no_delay(true));
|
||||
tcpconn->socket().async_connect(endpoint,
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace network
|
||||
void send_data(std::shared_ptr<std::string> buffer) override;
|
||||
|
||||
public:
|
||||
tcp_conn(asio::io_context &io_ctx);
|
||||
tcp_conn(asio::io_context &io_ctx, bool client = false);
|
||||
asio::ip::tcp::socket& socket();
|
||||
|
||||
void connected() override;
|
||||
|
||||
Reference in New Issue
Block a user