mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
network fixes and improvements, code refactoring
This commit is contained in:
@@ -9,10 +9,14 @@ network::tcp::connection::connection(asio::io_context &io_ctx, bool client, size
|
||||
m_header_buffer.resize(8);
|
||||
}
|
||||
|
||||
network::tcp::connection::~connection()
|
||||
{
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
void network::tcp::connection::disconnect()
|
||||
{
|
||||
network::connection::disconnect();
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
void network::tcp::connection::send_data(std::shared_ptr<std::string> buffer)
|
||||
@@ -176,8 +180,6 @@ void network::tcp::client::connect()
|
||||
if (this->conn)
|
||||
return;
|
||||
|
||||
std::cout << "create sock" << std::endl;
|
||||
|
||||
std::shared_ptr<connection> conn = std::make_shared<connection>(io_ctx, true, resume_frame_counter);
|
||||
conn->set_handler(std::bind(&client::handle_message, this, conn, std::placeholders::_1));
|
||||
|
||||
@@ -187,7 +189,6 @@ void network::tcp::client::connect()
|
||||
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));
|
||||
std::cout << "connect start" <<std::endl;
|
||||
|
||||
this->conn = conn;
|
||||
|
||||
@@ -195,7 +196,6 @@ void network::tcp::client::connect()
|
||||
|
||||
void network::tcp::client::handle_accept(const asio::error_code &err)
|
||||
{
|
||||
std::cout << "connect handl" <<std::endl;
|
||||
if (!err)
|
||||
{
|
||||
conn->connected();
|
||||
@@ -203,6 +203,6 @@ void network::tcp::client::handle_accept(const asio::error_code &err)
|
||||
else
|
||||
{
|
||||
WriteLog(std::string("net: failed to connect: " + err.message()), logtype::net);
|
||||
conn->state = connection::DEAD;
|
||||
conn->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace network::tcp
|
||||
|
||||
public:
|
||||
connection(asio::io_context &io_ctx, bool client = false, size_t counter = 0);
|
||||
~connection();
|
||||
|
||||
virtual void connected() override;
|
||||
virtual void disconnect() override;
|
||||
|
||||
@@ -22,14 +22,14 @@ void network::server_manager::push_delta(double dt, double sync, const command_q
|
||||
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;
|
||||
|
||||
for (auto srv : servers)
|
||||
srv->push_delta(msg);
|
||||
|
||||
serialize_message(msg, *backbuffer.get());
|
||||
}
|
||||
|
||||
@@ -42,10 +42,13 @@ network::manager::manager()
|
||||
{
|
||||
}
|
||||
|
||||
void network::manager::poll()
|
||||
void network::manager::update()
|
||||
{
|
||||
io_context.restart();
|
||||
io_context.poll();
|
||||
|
||||
if (client)
|
||||
client->update();
|
||||
}
|
||||
|
||||
void network::manager::create_server(const std::string &host, uint32_t port)
|
||||
|
||||
@@ -32,6 +32,6 @@ namespace network
|
||||
|
||||
void create_server(const std::string &host, uint32_t port);
|
||||
void connect(const std::string &host, uint32_t port);
|
||||
void poll();
|
||||
void update();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -40,27 +40,29 @@ void network::connection::catch_up()
|
||||
|
||||
std::vector<std::shared_ptr<message>> messages;
|
||||
|
||||
for (size_t i = 0; i < CATCHUP_PACKETS; i++) {
|
||||
for (int i = 0; i < CATCHUP_PACKETS; i++) {
|
||||
if (backbuffer->peek() == EOF) {
|
||||
send_messages(messages);
|
||||
backbuffer->seekg(0, std::ios_base::end);
|
||||
state = ACTIVE;
|
||||
backbuffer->seekg(0, std::ios_base::end);
|
||||
return;
|
||||
}
|
||||
|
||||
auto msg = deserialize_message(*backbuffer.get());
|
||||
|
||||
if (packet_counter) {
|
||||
packet_counter--;
|
||||
i--; // TODO: it would be better to skip frames in chunks
|
||||
continue;
|
||||
}
|
||||
|
||||
messages.push_back(deserialize_message(*backbuffer.get()));
|
||||
messages.push_back(msg);
|
||||
}
|
||||
|
||||
send_messages(messages);
|
||||
|
||||
backbuffer_pos = backbuffer->tellg();
|
||||
|
||||
backbuffer->seekg(0, std::ios_base::end);
|
||||
|
||||
send_messages(messages);
|
||||
}
|
||||
|
||||
void network::connection::send_complete(std::shared_ptr<std::string> buf)
|
||||
@@ -108,13 +110,8 @@ 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)
|
||||
void network::server::push_delta(const frame_info &msg)
|
||||
{
|
||||
frame_info msg;
|
||||
msg.dt = dt;
|
||||
msg.sync = sync;
|
||||
msg.commands = commands;
|
||||
|
||||
for (auto it = clients.begin(); it != clients.end(); ) {
|
||||
if ((*it)->state == connection::DEAD) {
|
||||
it = clients.erase(it);
|
||||
@@ -146,6 +143,11 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
if (msg.type == message::CLIENT_HELLO) {
|
||||
auto cmd = dynamic_cast<const client_hello&>(msg);
|
||||
|
||||
if (cmd.version != 1) {
|
||||
conn->disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
server_hello reply;
|
||||
reply.seed = Global.random_seed;
|
||||
conn->state = connection::CATCHING_UP;
|
||||
@@ -167,20 +169,24 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
|
||||
// ------------
|
||||
|
||||
// client
|
||||
int zzz = 20;
|
||||
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta()
|
||||
void network::client::update()
|
||||
{
|
||||
if (conn && conn->state == connection::DEAD) {
|
||||
conn.reset();
|
||||
}
|
||||
|
||||
if (!conn) {
|
||||
zzz--;
|
||||
if (zzz < 0)
|
||||
if (!reconnect_delay) {
|
||||
connect();
|
||||
reconnect_delay = RECONNECT_DELAY_FRAMES;
|
||||
}
|
||||
reconnect_delay--;
|
||||
}
|
||||
}
|
||||
|
||||
// client
|
||||
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta()
|
||||
{
|
||||
if (delta_queue.empty()) {
|
||||
return std::tuple<double, double,
|
||||
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
||||
@@ -189,6 +195,7 @@ std::tuple<double, double, command_queue::commands_map> network::client::get_nex
|
||||
auto entry = delta_queue.front();
|
||||
delta_queue.pop();
|
||||
|
||||
auto &delta = entry.second;
|
||||
return std::make_tuple(entry.second.dt, entry.second.sync, entry.second.commands);
|
||||
}
|
||||
|
||||
@@ -216,9 +223,11 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
auto cmd = dynamic_cast<const server_hello&>(msg);
|
||||
conn->state = connection::ACTIVE;
|
||||
|
||||
Global.random_seed = cmd.seed;
|
||||
Global.random_engine.seed(Global.random_seed);
|
||||
Global.ready_to_load = true;
|
||||
if (!Global.ready_to_load) {
|
||||
Global.random_seed = cmd.seed;
|
||||
Global.random_engine.seed(Global.random_seed);
|
||||
Global.ready_to_load = true;
|
||||
}
|
||||
|
||||
WriteLog("net: accept received", logtype::net);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace network
|
||||
friend class client;
|
||||
|
||||
private:
|
||||
const size_t CATCHUP_PACKETS = 100;
|
||||
const int CATCHUP_PACKETS = 100;
|
||||
|
||||
/*
|
||||
std::queue<
|
||||
@@ -74,7 +74,7 @@ namespace network
|
||||
|
||||
public:
|
||||
server(std::shared_ptr<std::istream> buf);
|
||||
void push_delta(double dt, double sync, const command_queue::commands_map &commands);
|
||||
void push_delta(const frame_info &msg);
|
||||
command_queue::commands_map pop_commands();
|
||||
};
|
||||
|
||||
@@ -85,12 +85,16 @@ namespace network
|
||||
void handle_message(std::shared_ptr<connection> conn, const message &msg);
|
||||
std::shared_ptr<connection> conn;
|
||||
size_t resume_frame_counter = 0;
|
||||
size_t reconnect_delay = 0;
|
||||
|
||||
const size_t RECONNECT_DELAY_FRAMES = 60;
|
||||
|
||||
std::queue<
|
||||
std::pair<std::chrono::high_resolution_clock::time_point,
|
||||
frame_info>> delta_queue;
|
||||
|
||||
public:
|
||||
void update();
|
||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
||||
void send_commands(command_queue::commands_map commands);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user