mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-18 00:49:19 +02:00
work
This commit is contained in:
@@ -23,6 +23,7 @@ file(GLOB HEADERS "*.h"
|
|||||||
"McZapkie/*.h"
|
"McZapkie/*.h"
|
||||||
"gl/*.h"
|
"gl/*.h"
|
||||||
"network/*.h"
|
"network/*.h"
|
||||||
|
"network/backend/*.h"
|
||||||
"widgets/*.h")
|
"widgets/*.h")
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
@@ -116,8 +117,8 @@ set(SOURCES
|
|||||||
|
|
||||||
"network/network.cpp"
|
"network/network.cpp"
|
||||||
"network/message.cpp"
|
"network/message.cpp"
|
||||||
"network/tcp.cpp"
|
|
||||||
"network/manager.cpp"
|
"network/manager.cpp"
|
||||||
|
"network/backend/asio.cpp"
|
||||||
|
|
||||||
"widgets/vehiclelist.cpp"
|
"widgets/vehiclelist.cpp"
|
||||||
|
|
||||||
|
|||||||
@@ -27,11 +27,7 @@ else()
|
|||||||
else()
|
else()
|
||||||
find_path(ASIO_ROOT include/asio.hpp
|
find_path(ASIO_ROOT include/asio.hpp
|
||||||
PATHS
|
PATHS
|
||||||
# Where asio lives relative to it's official repository
|
${CMAKE_CURRENT_LIST_DIR}/../ref/asio/asio
|
||||||
${CMAKE_CURRENT_LIST_DIR}/../../asio/asio
|
|
||||||
|
|
||||||
# Where asio should live
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/../../asio
|
|
||||||
)
|
)
|
||||||
if(NOT ASIO_ROOT)
|
if(NOT ASIO_ROOT)
|
||||||
if(ASIO_FIND_REQUIRED)
|
if(ASIO_FIND_REQUIRED)
|
||||||
|
|||||||
@@ -5973,7 +5973,7 @@ bool TTrain::Update( double const Deltatime )
|
|||||||
// screens
|
// screens
|
||||||
fScreenTimer += Deltatime;
|
fScreenTimer += Deltatime;
|
||||||
if( ( fScreenTimer > Global.PythonScreenUpdateRate * 0.001f )
|
if( ( fScreenTimer > Global.PythonScreenUpdateRate * 0.001f )
|
||||||
&& ( false == FreeFlyModeFlag ) ) { // don't bother if we're outside
|
&& !FreeFlyModeFlag && simulation::Train == this ) { // don't bother if we're outside
|
||||||
fScreenTimer = 0.f;
|
fScreenTimer = 0.f;
|
||||||
for( auto const &screen : m_screens ) {
|
for( auto const &screen : m_screens ) {
|
||||||
Application.request( { std::get<0>(screen), GetTrainState(), GfxRenderer.Texture( std::get<1>(screen) ).id } );
|
Application.request( { std::get<0>(screen), GetTrainState(), GfxRenderer.Texture( std::get<1>(screen) ).id } );
|
||||||
|
|||||||
@@ -182,10 +182,10 @@ eu07_application::run() {
|
|||||||
double slave_sync;
|
double slave_sync;
|
||||||
|
|
||||||
// if we're the server
|
// if we're the server
|
||||||
if (m_network && m_network->server)
|
if (m_network && m_network->servers)
|
||||||
{
|
{
|
||||||
// fetch from network layer command requests received from clients
|
// fetch from network layer command requests received from clients
|
||||||
command_queue::commands_map remote_commands = m_network->server->pop_commands();
|
command_queue::commands_map remote_commands = m_network->servers->pop_commands();
|
||||||
|
|
||||||
// push these into local queue
|
// push these into local queue
|
||||||
add_to_dequemap(local_commands, remote_commands);
|
add_to_dequemap(local_commands, remote_commands);
|
||||||
@@ -239,11 +239,11 @@ eu07_application::run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if we're the server
|
// if we're the server
|
||||||
if (m_network && m_network->server)
|
if (m_network && m_network->servers)
|
||||||
{
|
{
|
||||||
// send delta, sync, and commands we just executed to clients
|
// send delta, sync, and commands we just executed to clients
|
||||||
double delta = Timer::GetDeltaTime();
|
double delta = Timer::GetDeltaTime();
|
||||||
m_network->server->push_delta(delta, sync, commands_to_exec);
|
m_network->servers->push_delta(delta, sync, commands_to_exec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -689,10 +689,10 @@ bool eu07_application::init_network() {
|
|||||||
if (Global.network_conf.is_server || Global.network_conf.is_client) {
|
if (Global.network_conf.is_server || Global.network_conf.is_client) {
|
||||||
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(Global.network_conf.server_host, Global.network_conf.server_port);
|
||||||
}
|
}
|
||||||
if (Global.network_conf.is_client) {
|
if (Global.network_conf.is_client) {
|
||||||
m_network->connect();
|
m_network->connect(Global.network_conf.client_host, Global.network_conf.client_port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ image: Visual Studio 2017
|
|||||||
clone_depth: 1
|
clone_depth: 1
|
||||||
build_script:
|
build_script:
|
||||||
- ps: >-
|
- ps: >-
|
||||||
|
cd ref
|
||||||
|
|
||||||
|
git clone https://github.com/chriskohlhoff/asio --depth 1
|
||||||
|
|
||||||
|
cd ..
|
||||||
|
|
||||||
mkdir builds
|
mkdir builds
|
||||||
|
|
||||||
cd builds
|
cd builds
|
||||||
|
|||||||
@@ -379,6 +379,7 @@ driver_mode::on_event_poll() {
|
|||||||
|
|
||||||
void
|
void
|
||||||
driver_mode::update_camera( double const Deltatime ) {
|
driver_mode::update_camera( double const Deltatime ) {
|
||||||
|
Camera.Pos = Global.pCamera.Pos; // M7TODO
|
||||||
|
|
||||||
auto *controlled = (
|
auto *controlled = (
|
||||||
simulation::Train ?
|
simulation::Train ?
|
||||||
@@ -686,12 +687,14 @@ driver_mode::OnKeyDown(int cKey) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
TDynamicObject *dynamic = std::get<TDynamicObject *>( simulation::Region->find_vehicle( Global.pCamera.Pos, 50, true, false ) );
|
TDynamicObject *dynamic = std::get<TDynamicObject *>( simulation::Region->find_vehicle( Global.pCamera.Pos, 50, true, false ) );
|
||||||
TTrain *train = simulation::Trains.find(dynamic->name());
|
if (dynamic) {
|
||||||
if (train) {
|
TTrain *train = simulation::Trains.find(dynamic->name());
|
||||||
simulation::Train = train;
|
if (train) {
|
||||||
InOutKey();
|
simulation::Train = train;
|
||||||
} else {
|
InOutKey();
|
||||||
m_relay.post(user_command::entervehicle, 0.0, 0.0, GLFW_PRESS, 0);
|
} else {
|
||||||
|
m_relay.post(user_command::entervehicle, 0.0, 0.0, GLFW_PRESS, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
170
network/backend/asio.cpp
Normal file
170
network/backend/asio.cpp
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
#include "network/backend/asio.h"
|
||||||
|
#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)
|
||||||
|
{
|
||||||
|
m_header_buffer.resize(8);
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::tcp::connection::disconnect()
|
||||||
|
{
|
||||||
|
network::connection::disconnect();
|
||||||
|
m_socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::tcp::connection::connected()
|
||||||
|
{
|
||||||
|
network::connection::connected();
|
||||||
|
read_header();
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::tcp::connection::read_header()
|
||||||
|
{
|
||||||
|
asio::async_read(m_socket, asio::buffer(m_header_buffer),
|
||||||
|
std::bind(&connection::handle_header, this,
|
||||||
|
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);
|
||||||
|
if (m_header_buffer.size() != bytes_transferred) {
|
||||||
|
disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t sig = sn_utils::ld_uint32(header);
|
||||||
|
if (sig != NETWORK_MAGIC) {
|
||||||
|
disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t len = sn_utils::ld_uint32(header);
|
||||||
|
m_body_buffer.resize(len);
|
||||||
|
if (len > MAX_MSG_SIZE) {
|
||||||
|
disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
asio::async_read(m_socket, asio::buffer(m_body_buffer),
|
||||||
|
std::bind(&connection::handle_data, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::tcp::connection::handle_data(const asio::error_code &err, size_t bytes_transferred)
|
||||||
|
{
|
||||||
|
if (m_body_buffer.size() != bytes_transferred) {
|
||||||
|
disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::istringstream stream(m_body_buffer);
|
||||||
|
std::shared_ptr<message> msg = deserialize_message(stream);
|
||||||
|
if (message_handler)
|
||||||
|
message_handler(*msg);
|
||||||
|
|
||||||
|
read_header();
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::tcp::connection::send_message(const message &msg)
|
||||||
|
{
|
||||||
|
std::ostringstream stream;
|
||||||
|
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;
|
||||||
|
if (size > MAX_MSG_SIZE) {
|
||||||
|
ErrorLog("net: message too big", logtype::net);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stream.seekp(4, std::ios_base::beg);
|
||||||
|
sn_utils::ls_uint32(stream, size);
|
||||||
|
|
||||||
|
stream.flush();
|
||||||
|
|
||||||
|
std::shared_ptr<std::string> buf = std::make_shared<std::string>(stream.str());
|
||||||
|
send_data(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------
|
||||||
|
|
||||||
|
network::tcp::server::server(asio::io_context &io_ctx, const std::string &host, uint32_t port)
|
||||||
|
: m_acceptor(io_ctx)
|
||||||
|
{
|
||||||
|
auto endpoint = asio::ip::tcp::endpoint(asio::ip::address::from_string(host), port);
|
||||||
|
m_acceptor.open(endpoint.protocol());
|
||||||
|
m_acceptor.set_option(asio::socket_base::reuse_address(true));
|
||||||
|
m_acceptor.set_option(asio::ip::tcp::no_delay(true));
|
||||||
|
m_acceptor.bind(endpoint);
|
||||||
|
m_acceptor.listen(10);
|
||||||
|
|
||||||
|
accept_conn();
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::tcp::server::accept_conn()
|
||||||
|
{
|
||||||
|
std::shared_ptr<connection> conn = std::make_shared<connection>(m_acceptor.get_executor().context());
|
||||||
|
conn->set_handler(std::bind(&server::handle_message, this, std::ref(*conn.get()), std::placeholders::_1));
|
||||||
|
|
||||||
|
m_acceptor.async_accept(conn->m_socket, std::bind(&server::handle_accept, this, conn, std::placeholders::_1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::tcp::server::handle_accept(std::shared_ptr<connection> conn, const asio::error_code &err)
|
||||||
|
{
|
||||||
|
if (!err)
|
||||||
|
{
|
||||||
|
clients.emplace_back(conn);
|
||||||
|
conn->connected();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WriteLog(std::string("net: failed to accept client: " + err.message()), logtype::net);
|
||||||
|
}
|
||||||
|
|
||||||
|
accept_conn();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------
|
||||||
|
|
||||||
|
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);
|
||||||
|
conn->set_handler(std::bind(&client::handle_message, this, std::ref(*conn.get()), std::placeholders::_1));
|
||||||
|
|
||||||
|
asio::ip::tcp::endpoint endpoint(
|
||||||
|
asio::ip::address::from_string(host), port);
|
||||||
|
conn->m_socket.open(endpoint.protocol());
|
||||||
|
conn->m_socket.set_option(asio::ip::tcp::no_delay(true));
|
||||||
|
conn->m_socket.async_connect(endpoint,
|
||||||
|
std::bind(&client::handle_accept, this, std::placeholders::_1));
|
||||||
|
|
||||||
|
this->conn = conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::tcp::client::handle_accept(const asio::error_code &err)
|
||||||
|
{
|
||||||
|
if (!err)
|
||||||
|
{
|
||||||
|
conn->connected();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WriteLog(std::string("net: failed to connect: " + err.message()), logtype::net);
|
||||||
|
}
|
||||||
|
}
|
||||||
54
network/backend/asio.h
Normal file
54
network/backend/asio.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include "network/network.h"
|
||||||
|
#include <asio.hpp>
|
||||||
|
|
||||||
|
namespace network::tcp
|
||||||
|
{
|
||||||
|
const uint32_t NETWORK_MAGIC = 0x37305545;
|
||||||
|
const uint32_t MAX_MSG_SIZE = 10000;
|
||||||
|
|
||||||
|
class connection : public network::connection
|
||||||
|
{
|
||||||
|
friend class server;
|
||||||
|
friend class client;
|
||||||
|
|
||||||
|
public:
|
||||||
|
connection(asio::io_context &io_ctx, bool client = false);
|
||||||
|
|
||||||
|
virtual void connected() override;
|
||||||
|
virtual void disconnect() override;
|
||||||
|
virtual void send_data(std::shared_ptr<std::string> buffer) override;
|
||||||
|
virtual void send_message(const message &msg) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
asio::ip::tcp::socket m_socket;
|
||||||
|
|
||||||
|
std::string m_header_buffer;
|
||||||
|
std::string m_body_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);
|
||||||
|
};
|
||||||
|
|
||||||
|
class server : public network::server
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
void accept_conn();
|
||||||
|
void handle_accept(std::shared_ptr<connection> conn, const asio::error_code &err);
|
||||||
|
|
||||||
|
asio::ip::tcp::acceptor m_acceptor;
|
||||||
|
|
||||||
|
public:
|
||||||
|
server(asio::io_context &io_ctx, const std::string &host, uint32_t port);
|
||||||
|
};
|
||||||
|
|
||||||
|
class client : public network::client
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
void handle_accept(const asio::error_code &err);
|
||||||
|
|
||||||
|
public:
|
||||||
|
client(asio::io_context &io_ctx, const std::string &host, uint32_t port);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,42 +1,44 @@
|
|||||||
#include "network/manager.h"
|
#include "network/manager.h"
|
||||||
#include "network/tcp.h"
|
|
||||||
#include "simulation.h"
|
#include "simulation.h"
|
||||||
|
#include "network/backend/asio.h"
|
||||||
|
|
||||||
|
command_queue::commands_map network::server_manager::pop_commands()
|
||||||
|
{
|
||||||
|
command_queue::commands_map map;
|
||||||
|
|
||||||
|
for (auto srv : servers)
|
||||||
|
add_to_dequemap(map, srv->pop_commands());
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::server_manager::push_delta(double dt, double sync, const command_queue::commands_map &commands)
|
||||||
|
{
|
||||||
|
for (auto srv : servers)
|
||||||
|
srv->push_delta(dt, sync, commands);
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
network::manager::manager()
|
network::manager::manager()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::manager::create_server()
|
|
||||||
{
|
|
||||||
server = std::make_shared<tcp_server>(io_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::manager::poll()
|
void network::manager::poll()
|
||||||
{
|
{
|
||||||
io_context.poll();
|
io_context.poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::manager::connect()
|
void network::manager::create_server(const std::string &host, uint32_t port)
|
||||||
{
|
{
|
||||||
client = std::make_shared<tcp_client>(io_context);
|
servers.emplace();
|
||||||
|
servers->create_server(io_context, host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<double, double, command_queue::commands_map> network::manager::get_next_delta()
|
void network::manager::connect(const std::string &host, uint32_t port)
|
||||||
{
|
{
|
||||||
return client->get_next_delta();
|
client = std::make_shared<tcp::client>(io_context, host, port);
|
||||||
}
|
|
||||||
|
|
||||||
void network::manager::push_delta(double delta, double sync, command_queue::commands_map commands)
|
|
||||||
{
|
|
||||||
server->push_delta(delta, sync, commands);
|
|
||||||
}
|
|
||||||
|
|
||||||
command_queue::commands_map network::manager::pop_commands()
|
|
||||||
{
|
|
||||||
return server->pop_commands();
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::manager::send_commands(command_queue::commands_map commands)
|
|
||||||
{
|
|
||||||
client->send_commands(commands);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,17 @@
|
|||||||
|
|
||||||
namespace network
|
namespace network
|
||||||
{
|
{
|
||||||
|
class server_manager
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::vector<std::shared_ptr<server>> servers;
|
||||||
|
|
||||||
|
public:
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
|
||||||
class manager
|
class manager
|
||||||
{
|
{
|
||||||
asio::io_context io_context;
|
asio::io_context io_context;
|
||||||
@@ -13,16 +24,11 @@ namespace network
|
|||||||
public:
|
public:
|
||||||
manager();
|
manager();
|
||||||
|
|
||||||
std::shared_ptr<network::server> server;
|
std::optional<server_manager> servers;
|
||||||
std::shared_ptr<network::client> client;
|
std::shared_ptr<network::client> client;
|
||||||
|
|
||||||
void create_server();
|
void create_server(const std::string &host, uint32_t port);
|
||||||
void connect();
|
void connect(const std::string &host, uint32_t port);
|
||||||
void poll();
|
void poll();
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,35 @@
|
|||||||
#include "network/message.h"
|
#include "network/message.h"
|
||||||
#include "sn_utils.h"
|
#include "sn_utils.h"
|
||||||
|
|
||||||
void network::message::serialize(std::ostream &stream)
|
void network::client_hello::serialize(std::ostream &stream) const
|
||||||
{
|
{
|
||||||
|
sn_utils::ls_int32(stream, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::message::deserialize(std::istream &stream)
|
void network::client_hello::deserialize(std::istream &stream)
|
||||||
{
|
{
|
||||||
|
version = sn_utils::ld_int32(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::accept_message::serialize(std::ostream &stream)
|
void network::server_hello::serialize(std::ostream &stream) const
|
||||||
{
|
{
|
||||||
sn_utils::ls_uint32(stream, seed);
|
sn_utils::ls_uint32(stream, seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::accept_message::deserialize(std::istream &stream)
|
void network::server_hello::deserialize(std::istream &stream)
|
||||||
{
|
{
|
||||||
seed = sn_utils::ld_uint32(stream);
|
seed = sn_utils::ld_uint32(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t network::accept_message::get_size()
|
void ::network::request_command::serialize(std::ostream &stream) const
|
||||||
{
|
|
||||||
return message::get_size() + 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
void::network::command_message::serialize(std::ostream &stream)
|
|
||||||
{
|
{
|
||||||
sn_utils::ls_uint32(stream, commands.size());
|
sn_utils::ls_uint32(stream, commands.size());
|
||||||
for (auto const &kv : commands) {
|
for (auto const &kv : commands)
|
||||||
|
{
|
||||||
sn_utils::ls_uint32(stream, kv.first);
|
sn_utils::ls_uint32(stream, kv.first);
|
||||||
sn_utils::ls_uint32(stream, kv.second.size());
|
sn_utils::ls_uint32(stream, kv.second.size());
|
||||||
for (command_data const &data : kv.second) {
|
for (command_data const &data : kv.second)
|
||||||
|
{
|
||||||
sn_utils::ls_uint32(stream, (uint32_t)data.command);
|
sn_utils::ls_uint32(stream, (uint32_t)data.command);
|
||||||
sn_utils::ls_int32(stream, data.action);
|
sn_utils::ls_int32(stream, data.action);
|
||||||
sn_utils::ls_float64(stream, data.param1);
|
sn_utils::ls_float64(stream, data.param1);
|
||||||
@@ -45,15 +42,17 @@ void::network::command_message::serialize(std::ostream &stream)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::command_message::deserialize(std::istream &stream)
|
void network::request_command::deserialize(std::istream &stream)
|
||||||
{
|
{
|
||||||
uint32_t commands_size = sn_utils::ld_uint32(stream);
|
uint32_t commands_size = sn_utils::ld_uint32(stream);
|
||||||
for (uint32_t i = 0; i < commands_size; i++) {
|
for (uint32_t i = 0; i < commands_size; i++)
|
||||||
|
{
|
||||||
uint32_t recipient = sn_utils::ld_uint32(stream);
|
uint32_t recipient = sn_utils::ld_uint32(stream);
|
||||||
uint32_t sequence_size = sn_utils::ld_uint32(stream);
|
uint32_t sequence_size = sn_utils::ld_uint32(stream);
|
||||||
|
|
||||||
command_queue::commanddata_sequence sequence;
|
command_queue::commanddata_sequence sequence;
|
||||||
for (uint32_t i = 0; i < sequence_size; i++) {
|
for (uint32_t i = 0; i < sequence_size; i++)
|
||||||
|
{
|
||||||
command_data data;
|
command_data data;
|
||||||
data.command = (user_command)sn_utils::ld_uint32(stream);
|
data.command = (user_command)sn_utils::ld_uint32(stream);
|
||||||
data.action = sn_utils::ld_int32(stream);
|
data.action = sn_utils::ld_int32(stream);
|
||||||
@@ -71,54 +70,20 @@ void network::command_message::deserialize(std::istream &stream)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t network::command_message::get_size()
|
void network::frame_info::serialize(std::ostream &stream) const
|
||||||
{
|
|
||||||
size_t cmd_size = 4;
|
|
||||||
|
|
||||||
for (auto const &kv : commands) {
|
|
||||||
cmd_size += 8;
|
|
||||||
for (command_data const &data : kv.second) {
|
|
||||||
cmd_size += 8 + 3 * 8 + 2 + 4 * 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return message::get_size() + cmd_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t network::delta_message::get_size()
|
|
||||||
{
|
|
||||||
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, dt);
|
||||||
sn_utils::ls_float64(stream, sync);
|
sn_utils::ls_float64(stream, sync);
|
||||||
|
|
||||||
command_message::serialize(stream);
|
request_command::serialize(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::delta_message::deserialize(std::istream &stream)
|
void network::frame_info::deserialize(std::istream &stream)
|
||||||
{
|
{
|
||||||
dt = sn_utils::ld_float64(stream);
|
dt = sn_utils::ld_float64(stream);
|
||||||
sync = sn_utils::ld_float64(stream);
|
sync = sn_utils::ld_float64(stream);
|
||||||
|
|
||||||
command_message::deserialize(stream);
|
request_command::deserialize(stream);
|
||||||
}
|
|
||||||
|
|
||||||
void network::string_message::serialize(std::ostream &stream)
|
|
||||||
{
|
|
||||||
sn_utils::s_str(stream, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::string_message::deserialize(std::istream &stream)
|
|
||||||
{
|
|
||||||
name = sn_utils::d_str(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t network::string_message::get_size()
|
|
||||||
{
|
|
||||||
return message::get_size() + name.size() + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<network::message> network::deserialize_message(std::istream &stream)
|
std::shared_ptr<network::message> network::deserialize_message(std::istream &stream)
|
||||||
@@ -126,27 +91,23 @@ std::shared_ptr<network::message> network::deserialize_message(std::istream &str
|
|||||||
message::type_e type = (message::type_e)sn_utils::ld_uint16(stream);
|
message::type_e type = (message::type_e)sn_utils::ld_uint16(stream);
|
||||||
|
|
||||||
std::shared_ptr<message> msg;
|
std::shared_ptr<message> msg;
|
||||||
if (type == message::CONNECT_ACCEPT) {
|
|
||||||
auto m = std::make_shared<accept_message>();
|
if (type == message::CLIENT_HELLO)
|
||||||
m->type = type;
|
msg = std::make_shared<client_hello>();
|
||||||
m->deserialize(stream);
|
else if (type == message::SERVER_HELLO)
|
||||||
msg = m;
|
msg = std::make_shared<server_hello>();
|
||||||
}
|
else if (type == message::FRAME_INFO)
|
||||||
else if (type == message::STEP_INFO) {
|
msg = std::make_shared<frame_info>();
|
||||||
auto m = std::make_shared<delta_message>();
|
else if (type == message::REQUEST_COMMAND)
|
||||||
m->type = type;
|
msg = std::make_shared<request_command>();
|
||||||
m->deserialize(stream);
|
|
||||||
msg = m;
|
msg->deserialize(stream);
|
||||||
}
|
|
||||||
else if (type == message::CLIENT_COMMAND) {
|
|
||||||
auto m = std::make_shared<command_message>();
|
|
||||||
m->type = type;
|
|
||||||
m->deserialize(stream);
|
|
||||||
msg = m;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
msg = std::make_shared<message>(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void network::serialize_message(const message &msg, std::ostream &stream)
|
||||||
|
{
|
||||||
|
sn_utils::ls_uint16(stream, (uint16_t)msg.type);
|
||||||
|
msg.serialize(stream);
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,70 +5,66 @@
|
|||||||
|
|
||||||
namespace network
|
namespace network
|
||||||
{
|
{
|
||||||
struct message
|
struct message
|
||||||
|
{
|
||||||
|
enum type_e
|
||||||
{
|
{
|
||||||
enum type_e
|
CLIENT_HELLO = 0,
|
||||||
{
|
SERVER_HELLO,
|
||||||
CONNECT_REQUEST = 0,
|
FRAME_INFO,
|
||||||
CONNECT_ACCEPT,
|
REQUEST_COMMAND,
|
||||||
STEP_INFO,
|
TYPE_MAX
|
||||||
CLIENT_COMMAND,
|
|
||||||
TYPE_MAX
|
|
||||||
};
|
|
||||||
|
|
||||||
type_e type;
|
|
||||||
|
|
||||||
message(type_e t) : type(t) {}
|
|
||||||
virtual void serialize(std::ostream &stream);
|
|
||||||
virtual void deserialize(std::istream &stream);
|
|
||||||
virtual size_t get_size() { return 2; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct accept_message : public message
|
type_e type;
|
||||||
{
|
|
||||||
accept_message() : message(CONNECT_ACCEPT) {}
|
|
||||||
|
|
||||||
uint32_t seed;
|
message(type_e t) : type(t) {}
|
||||||
|
virtual void serialize(std::ostream &stream) const {}
|
||||||
|
virtual void deserialize(std::istream &stream) {}
|
||||||
|
};
|
||||||
|
|
||||||
virtual void serialize(std::ostream &stream) override;
|
struct client_hello : public message
|
||||||
virtual void deserialize(std::istream &stream) override;
|
{
|
||||||
virtual size_t get_size() override;
|
client_hello() : message(CLIENT_HELLO) {}
|
||||||
};
|
|
||||||
|
|
||||||
struct command_message : public message
|
virtual void serialize(std::ostream &stream) const override;
|
||||||
{
|
virtual void deserialize(std::istream &stream) override;
|
||||||
command_message() : message(CLIENT_COMMAND) {}
|
|
||||||
command_message(type_e type) : message(type) {}
|
|
||||||
|
|
||||||
command_queue::commands_map commands;
|
int32_t version;
|
||||||
|
};
|
||||||
|
|
||||||
virtual void serialize(std::ostream &stream) override;
|
struct server_hello : public message
|
||||||
virtual void deserialize(std::istream &stream) override;
|
{
|
||||||
virtual size_t get_size() override;
|
server_hello() : message(SERVER_HELLO) {}
|
||||||
};
|
|
||||||
|
|
||||||
struct delta_message : public command_message
|
uint32_t seed;
|
||||||
{
|
|
||||||
delta_message() : command_message(STEP_INFO) {}
|
|
||||||
|
|
||||||
double dt;
|
virtual void serialize(std::ostream &stream) const override;
|
||||||
double sync;
|
virtual void deserialize(std::istream &stream) override;
|
||||||
|
};
|
||||||
|
|
||||||
virtual void serialize(std::ostream &stream) override;
|
struct request_command : public message
|
||||||
virtual void deserialize(std::istream &stream) override;
|
{
|
||||||
virtual size_t get_size() override;
|
request_command(type_e type) : message(type) {}
|
||||||
};
|
request_command() : message(REQUEST_COMMAND) {}
|
||||||
|
|
||||||
struct string_message : public message
|
command_queue::commands_map commands;
|
||||||
{
|
|
||||||
string_message(type_e type) : message(type) {}
|
|
||||||
|
|
||||||
std::string name;
|
virtual void serialize(std::ostream &stream) const override;
|
||||||
|
virtual void deserialize(std::istream &stream) override;
|
||||||
|
};
|
||||||
|
|
||||||
virtual void serialize(std::ostream &stream) override;
|
struct frame_info : public request_command
|
||||||
virtual void deserialize(std::istream &stream) override;
|
{
|
||||||
virtual size_t get_size() override;
|
frame_info() : request_command(FRAME_INFO) {}
|
||||||
};
|
|
||||||
|
|
||||||
std::shared_ptr<message> deserialize_message(std::istream &stream);
|
double dt;
|
||||||
}
|
double sync;
|
||||||
|
|
||||||
|
virtual void serialize(std::ostream &stream) const override;
|
||||||
|
virtual void deserialize(std::istream &stream) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::shared_ptr<message> deserialize_message(std::istream &stream);
|
||||||
|
void serialize_message(const message &msg, std::ostream &stream);
|
||||||
|
} // namespace network
|
||||||
|
|||||||
@@ -5,8 +5,20 @@
|
|||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
#include "application.h"
|
#include "application.h"
|
||||||
|
|
||||||
|
// connection
|
||||||
|
|
||||||
|
void network::connection::disconnect() {
|
||||||
|
WriteLog("net: peer dropped", logtype::net);
|
||||||
|
state = DEAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
void network::connection::set_handler(std::function<void (const message &)> handler) {
|
||||||
|
message_handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
network::connection::connection(bool client) {
|
network::connection::connection(bool client) {
|
||||||
is_client = client;
|
is_client = client;
|
||||||
|
state = AWAITING_HELLO;
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::connection::connected()
|
void network::connection::connected()
|
||||||
@@ -14,50 +26,57 @@ void network::connection::connected()
|
|||||||
WriteLog("net: socket connected", logtype::net);
|
WriteLog("net: socket connected", logtype::net);
|
||||||
|
|
||||||
if (is_client) {
|
if (is_client) {
|
||||||
std::shared_ptr<message> hello = std::make_shared<message>(message::CONNECT_REQUEST);
|
client_hello msg;
|
||||||
send_message(hello);
|
msg.version = 1;
|
||||||
|
send_message(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::connection::message_received(std::shared_ptr<message> &msg)
|
/*
|
||||||
{
|
|
||||||
if (msg->type == message::TYPE_MAX)
|
if (msg->type == message::TYPE_MAX)
|
||||||
{
|
{
|
||||||
disconnect();
|
disconnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg->type == message::CONNECT_REQUEST)
|
if (is_client) {
|
||||||
{
|
if (msg->type == message::SERVER_HELLO) {
|
||||||
std::shared_ptr<accept_message> reply = std::make_shared<accept_message>();
|
auto cmd = std::dynamic_pointer_cast<server_hello>(msg);
|
||||||
reply->seed = Global.random_seed;
|
|
||||||
|
|
||||||
WriteLog("client accepted", logtype::net);
|
state = ACTIVE;
|
||||||
|
Global.random_seed = cmd->seed;
|
||||||
|
Global.random_engine.seed(Global.random_seed);
|
||||||
|
|
||||||
send_message(reply);
|
WriteLog("net: accept received", logtype::net);
|
||||||
}
|
}
|
||||||
else if (msg->type == message::CONNECT_ACCEPT)
|
else if (msg->type == message::FRAME_INFO) {
|
||||||
{
|
auto delta = std::dynamic_pointer_cast<frame_info>(msg);
|
||||||
auto cmd = std::dynamic_pointer_cast<accept_message>(msg);
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
delta_queue.push(std::make_pair(now, delta));
|
||||||
|
}
|
||||||
|
|
||||||
WriteLog("accept received", logtype::net);
|
} else {
|
||||||
Global.random_seed = cmd->seed;
|
if (msg->type == message::CLIENT_HELLO) {
|
||||||
Global.random_engine.seed(Global.random_seed);
|
server_hello reply;
|
||||||
}
|
reply.seed = Global.random_seed;
|
||||||
else if (msg->type == message::CLIENT_COMMAND)
|
state = ACTIVE;
|
||||||
{
|
|
||||||
auto cmd = std::dynamic_pointer_cast<command_message>(msg);
|
|
||||||
for (auto const &kv : cmd->commands)
|
|
||||||
client_commands_queue.emplace(kv);
|
|
||||||
}
|
|
||||||
else if (msg->type == message::STEP_INFO)
|
|
||||||
{
|
|
||||||
auto delta = std::dynamic_pointer_cast<delta_message>(msg);
|
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
|
||||||
delta_queue.push(std::make_pair(now, delta));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
send_message(reply);
|
||||||
|
|
||||||
|
WriteLog("net: client accepted", logtype::net);
|
||||||
|
}
|
||||||
|
else if (msg->type == message::REQUEST_COMMAND) {
|
||||||
|
auto cmd = std::dynamic_pointer_cast<request_command>(msg);
|
||||||
|
|
||||||
|
for (auto const &kv : cmd->commands)
|
||||||
|
client_commands_queue.emplace(kv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// --------------
|
||||||
|
|
||||||
|
/*
|
||||||
std::tuple<double, 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()) {
|
if (delta_queue.empty()) {
|
||||||
@@ -90,70 +109,83 @@ command_queue::commands_map network::connection::pop_commands()
|
|||||||
client_commands_queue.clear();
|
client_commands_queue.clear();
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
void network::connection::data_received(std::string &buffer)
|
// server
|
||||||
{
|
|
||||||
std::istringstream body(buffer);
|
|
||||||
std::shared_ptr<message> msg = deserialize_message(body);
|
|
||||||
|
|
||||||
message_received(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::connection::send_message(std::shared_ptr<message> msg)
|
|
||||||
{
|
|
||||||
std::ostringstream stream;
|
|
||||||
sn_utils::ls_uint32(stream, 0x37305545);
|
|
||||||
sn_utils::ls_uint32(stream, msg->get_size());
|
|
||||||
sn_utils::ls_uint16(stream, (uint16_t)msg->type);
|
|
||||||
msg->serialize(stream);
|
|
||||||
stream.flush();
|
|
||||||
|
|
||||||
std::shared_ptr<std::string> buf = std::make_shared<std::string>(stream.str());
|
|
||||||
send_data(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
network::server::server()
|
network::server::server()
|
||||||
{
|
{
|
||||||
recorder.open("recorder.bin", std::ios::trunc | std::ios::out | std::ios::binary);
|
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, const command_queue::commands_map &commands)
|
||||||
{
|
{
|
||||||
if (dt == 0.0 && commands.empty())
|
if (dt == 0.0 && commands.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::shared_ptr<delta_message> msg = std::make_shared<delta_message>();
|
frame_info msg;
|
||||||
msg->dt = dt;
|
msg.dt = dt;
|
||||||
msg->sync = sync;
|
msg.sync = sync;
|
||||||
msg->commands = commands;
|
msg.commands = commands;
|
||||||
|
|
||||||
sn_utils::ls_uint32(recorder, 0x37305545);
|
sn_utils::ls_uint32(recorder, 0x37305545);
|
||||||
sn_utils::ls_uint32(recorder, msg->get_size());
|
sn_utils::ls_uint32(recorder, 0);
|
||||||
sn_utils::ls_uint16(recorder, (uint16_t)msg->type);
|
sn_utils::ls_uint16(recorder, (uint16_t)msg.type);
|
||||||
msg->serialize(recorder);
|
msg.serialize(recorder);
|
||||||
recorder.flush();
|
recorder.flush();
|
||||||
|
|
||||||
for (auto c : clients)
|
for (auto c : clients)
|
||||||
c->send_message(msg);
|
if (c->state == connection::ACTIVE)
|
||||||
|
c->send_message(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
command_queue::commands_map network::server::pop_commands()
|
command_queue::commands_map network::server::pop_commands()
|
||||||
{
|
{
|
||||||
command_queue::commands_map map;
|
command_queue::commands_map map(client_commands_queue);
|
||||||
for (auto c : clients) {
|
client_commands_queue.clear();
|
||||||
command_queue::commands_map cmap = c->pop_commands();
|
|
||||||
for (auto const &kv : cmap) {
|
|
||||||
auto lookup = map.emplace(kv.first, command_queue::commanddata_sequence());
|
|
||||||
for (command_data const &data : kv.second)
|
|
||||||
lookup.first->second.emplace_back(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void network::server::handle_message(connection &conn, const message &msg)
|
||||||
|
{
|
||||||
|
if (msg.type == message::TYPE_MAX)
|
||||||
|
{
|
||||||
|
conn.disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == message::CLIENT_HELLO) {
|
||||||
|
server_hello reply;
|
||||||
|
reply.seed = Global.random_seed;
|
||||||
|
conn.state = connection::ACTIVE;
|
||||||
|
|
||||||
|
conn.send_message(reply);
|
||||||
|
|
||||||
|
WriteLog("net: client accepted", logtype::net);
|
||||||
|
}
|
||||||
|
else if (msg.type == message::REQUEST_COMMAND) {
|
||||||
|
auto cmd = dynamic_cast<const request_command&>(msg);
|
||||||
|
|
||||||
|
for (auto const &kv : cmd.commands)
|
||||||
|
client_commands_queue.emplace(kv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------
|
||||||
|
|
||||||
|
// client
|
||||||
|
|
||||||
std::tuple<double, 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();
|
if (delta_queue.empty()) {
|
||||||
|
return std::tuple<double, double,
|
||||||
|
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
||||||
|
}
|
||||||
|
|
||||||
|
auto entry = delta_queue.front();
|
||||||
|
delta_queue.pop();
|
||||||
|
|
||||||
|
return std::make_tuple(entry.second.dt, entry.second.sync, entry.second.commands);
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::client::send_commands(command_queue::commands_map commands)
|
void network::client::send_commands(command_queue::commands_map commands)
|
||||||
@@ -161,8 +193,34 @@ void network::client::send_commands(command_queue::commands_map commands)
|
|||||||
if (commands.empty())
|
if (commands.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::shared_ptr<command_message> msg = std::make_shared<command_message>();
|
request_command msg;
|
||||||
msg->commands = commands;
|
msg.commands = commands;
|
||||||
|
|
||||||
conn->send_message(msg);
|
conn->send_message(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void network::client::handle_message(connection &conn, const message &msg)
|
||||||
|
{
|
||||||
|
if (msg.type == message::TYPE_MAX)
|
||||||
|
{
|
||||||
|
conn.disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
WriteLog("net: accept received", logtype::net);
|
||||||
|
}
|
||||||
|
else if (msg.type == message::FRAME_INFO) {
|
||||||
|
auto delta = dynamic_cast<const frame_info&>(msg);
|
||||||
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
delta_queue.push(std::make_pair(now, delta));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <asio.hpp>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -10,52 +9,73 @@
|
|||||||
|
|
||||||
namespace network
|
namespace network
|
||||||
{
|
{
|
||||||
class connection : public std::enable_shared_from_this<connection>
|
class connection
|
||||||
{
|
{
|
||||||
|
friend class server;
|
||||||
|
friend class client;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void message_received(std::shared_ptr<message> &msg);
|
/*
|
||||||
std::queue<
|
std::queue<
|
||||||
std::pair<std::chrono::high_resolution_clock::time_point,
|
std::pair<std::chrono::high_resolution_clock::time_point,
|
||||||
std::shared_ptr<delta_message>>> delta_queue;
|
std::shared_ptr<frame_info>>> delta_queue;
|
||||||
|
|
||||||
command_queue::commands_map client_commands_queue;
|
command_queue::commands_map client_commands_queue;
|
||||||
bool is_client;
|
bool is_client;
|
||||||
|
|
||||||
//std::chrono::high_resolution_clock::time_point last_time;
|
//std::chrono::high_resolution_clock::time_point last_time;
|
||||||
//double accum = -1.0;
|
//double accum = -1.0;
|
||||||
|
*/
|
||||||
|
bool is_client;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
std::function<void(const message &msg)> message_handler;
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void connected() = 0;
|
||||||
|
virtual void send_message(const message &msg) = 0;
|
||||||
|
|
||||||
|
connection(bool client = false);
|
||||||
|
void set_handler(std::function<void(const message &msg)> handler);
|
||||||
|
|
||||||
virtual void disconnect() = 0;
|
virtual void disconnect() = 0;
|
||||||
virtual void send_data(std::shared_ptr<std::string> buffer) = 0;
|
virtual void send_data(std::shared_ptr<std::string> buffer) = 0;
|
||||||
|
|
||||||
void data_received(std::string &buffer);
|
enum peer_state {
|
||||||
|
AWAITING_HELLO,
|
||||||
public:
|
CATCHING_UP,
|
||||||
connection(bool client = false);
|
ACTIVE,
|
||||||
void send_message(std::shared_ptr<message> msg);
|
DEAD
|
||||||
virtual void connected();
|
};
|
||||||
|
peer_state state;
|
||||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
|
||||||
command_queue::commands_map pop_commands();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class server
|
class server
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
void handle_message(connection &conn, const message &msg);
|
||||||
|
|
||||||
std::vector<std::shared_ptr<connection>> clients;
|
std::vector<std::shared_ptr<connection>> clients;
|
||||||
std::fstream recorder;
|
std::fstream recorder;
|
||||||
|
|
||||||
|
command_queue::commands_map client_commands_queue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
server();
|
server();
|
||||||
void push_delta(double dt, double sync, 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();
|
||||||
};
|
};
|
||||||
|
|
||||||
class client
|
class client
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
void handle_message(connection &conn, const message &msg);
|
||||||
std::shared_ptr<connection> conn;
|
std::shared_ptr<connection> conn;
|
||||||
|
|
||||||
|
std::queue<
|
||||||
|
std::pair<std::chrono::high_resolution_clock::time_point,
|
||||||
|
frame_info>> delta_queue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
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);
|
||||||
|
|||||||
141
network/tcp.cpp
141
network/tcp.cpp
@@ -1,141 +0,0 @@
|
|||||||
#include "network/tcp.h"
|
|
||||||
#include "Logs.h"
|
|
||||||
#include "sn_utils.h"
|
|
||||||
#include "Globals.h"
|
|
||||||
|
|
||||||
network::tcp_conn::tcp_conn(asio::io_context &io_ctx, bool client)
|
|
||||||
: connection(client), m_socket(io_ctx)
|
|
||||||
{
|
|
||||||
m_header_buffer.resize(8);
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_conn::disconnect()
|
|
||||||
{
|
|
||||||
WriteLog("network: client dropped", logtype::net);
|
|
||||||
m_socket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_conn::connected()
|
|
||||||
{
|
|
||||||
connection::connected();
|
|
||||||
read_header();
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_conn::read_header()
|
|
||||||
{
|
|
||||||
asio::async_read(m_socket, asio::buffer(m_header_buffer),
|
|
||||||
std::bind(&tcp_conn::handle_header, this,
|
|
||||||
std::placeholders::_1, std::placeholders::_2));
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_conn::handle_send(std::shared_ptr<std::string> buf, const asio::error_code &err, size_t bytes_transferred)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_conn::send_data(std::shared_ptr<std::string> buffer)
|
|
||||||
{
|
|
||||||
asio::async_write(m_socket, asio::buffer(*buffer.get()), std::bind(&tcp_conn::handle_send, this, buffer,
|
|
||||||
std::placeholders::_1,
|
|
||||||
std::placeholders::_2));
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_conn::handle_header(const asio::error_code &err, size_t bytes_transferred)
|
|
||||||
{
|
|
||||||
std::istringstream header(m_header_buffer);
|
|
||||||
if (m_header_buffer.size() != bytes_transferred) {
|
|
||||||
disconnect();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t sig = sn_utils::ld_uint32(header);
|
|
||||||
if (sig != 0x37305545) {
|
|
||||||
disconnect();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t len = sn_utils::ld_uint32(header);
|
|
||||||
m_body_buffer.resize(len);
|
|
||||||
if (len > 10000) {
|
|
||||||
disconnect();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
asio::async_read(m_socket, asio::buffer(m_body_buffer),
|
|
||||||
std::bind(&tcp_conn::handle_data, this, std::placeholders::_1, std::placeholders::_2));
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_conn::handle_data(const asio::error_code &err, size_t bytes_transferred)
|
|
||||||
{
|
|
||||||
if (m_body_buffer.size() != bytes_transferred) {
|
|
||||||
disconnect();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
data_received(m_body_buffer);
|
|
||||||
read_header();
|
|
||||||
}
|
|
||||||
|
|
||||||
asio::ip::tcp::socket& network::tcp_conn::socket()
|
|
||||||
{
|
|
||||||
return m_socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
network::tcp_server::tcp_server(asio::io_context &io_ctx)
|
|
||||||
: m_acceptor(io_ctx)
|
|
||||||
{
|
|
||||||
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::tcp::no_delay(true));
|
|
||||||
m_acceptor.bind(endpoint);
|
|
||||||
m_acceptor.listen(10);
|
|
||||||
|
|
||||||
accept_conn();
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_server::accept_conn()
|
|
||||||
{
|
|
||||||
std::shared_ptr<tcp_conn> conn = std::make_shared<tcp_conn>(m_acceptor.get_executor().context());
|
|
||||||
|
|
||||||
m_acceptor.async_accept(conn->socket(), std::bind(&tcp_server::handle_accept, this, conn, std::placeholders::_1));
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_server::handle_accept(std::shared_ptr<tcp_conn> conn, const asio::error_code &err)
|
|
||||||
{
|
|
||||||
if (!err)
|
|
||||||
{
|
|
||||||
clients.emplace_back(conn);
|
|
||||||
conn->connected();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WriteLog(std::string("net: failed to accept client: " + err.message()), logtype::net);
|
|
||||||
}
|
|
||||||
|
|
||||||
accept_conn();
|
|
||||||
}
|
|
||||||
|
|
||||||
network::tcp_client::tcp_client(asio::io_context &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(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,
|
|
||||||
std::bind(&tcp_client::handle_accept, this, std::placeholders::_1));
|
|
||||||
}
|
|
||||||
|
|
||||||
void network::tcp_client::handle_accept(const asio::error_code &err)
|
|
||||||
{
|
|
||||||
if (!err)
|
|
||||||
{
|
|
||||||
conn->connected();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WriteLog(std::string("net: failed to connect: " + err.message()), logtype::net);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <asio.hpp>
|
|
||||||
#include <memory>
|
|
||||||
#include <array>
|
|
||||||
#include "network/network.h"
|
|
||||||
|
|
||||||
namespace network
|
|
||||||
{
|
|
||||||
class tcp_conn
|
|
||||||
: public connection
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
asio::ip::tcp::socket m_socket;
|
|
||||||
|
|
||||||
std::string m_header_buffer;
|
|
||||||
std::string m_body_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);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void disconnect() override;
|
|
||||||
void send_data(std::shared_ptr<std::string> buffer) override;
|
|
||||||
|
|
||||||
public:
|
|
||||||
tcp_conn(asio::io_context &io_ctx, bool client = false);
|
|
||||||
asio::ip::tcp::socket& socket();
|
|
||||||
|
|
||||||
void connected() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class tcp_server : public server
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
tcp_server(asio::io_context &io_ctx);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void accept_conn();
|
|
||||||
void handle_accept(std::shared_ptr<tcp_conn> conn, const asio::error_code &err);
|
|
||||||
|
|
||||||
asio::ip::tcp::acceptor m_acceptor;
|
|
||||||
};
|
|
||||||
|
|
||||||
class tcp_client : public client
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
void handle_accept(const asio::error_code &err);
|
|
||||||
|
|
||||||
public:
|
|
||||||
tcp_client(asio::io_context &io_ctx);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -20,7 +20,9 @@ void ui::vehiclelist_panel::render_contents() {
|
|||||||
else if (ImGui::TreeNode(vehicle, label.c_str())) {
|
else if (ImGui::TreeNode(vehicle, label.c_str())) {
|
||||||
vehicle = vehicle->Next();
|
vehicle = vehicle->Next();
|
||||||
while (vehicle) {
|
while (vehicle) {
|
||||||
ImGui::TextUnformatted(vehicle->name().c_str());
|
if (ImGui::Button(vehicle->name().c_str())) {
|
||||||
|
Global.pCamera.Pos = vehicle->GetPosition();
|
||||||
|
}
|
||||||
vehicle = vehicle->Next();
|
vehicle = vehicle->Next();
|
||||||
}
|
}
|
||||||
ImGui::TreePop();
|
ImGui::TreePop();
|
||||||
|
|||||||
Reference in New Issue
Block a user