16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-17 23:39:18 +02:00
This commit is contained in:
milek7
2019-01-28 23:19:41 +01:00
parent d5760c9e9c
commit 672e3e022a
5 changed files with 36 additions and 14 deletions

View File

@@ -3,7 +3,8 @@
#include "Logs.h"
network::tcp::connection::connection(asio::io_context &io_ctx, bool client, size_t counter)
: network::connection(client, counter), m_socket(io_ctx)
: network::connection(client, counter),
m_socket(io_ctx)
{
m_header_buffer.resize(8);
}
@@ -165,9 +166,10 @@ 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)
: host(host), port(port), io_ctx(io_ctx)
network::tcp::client::client(asio::io_context &io_ctxx, const std::string &hostarg, uint32_t portarg)
: host(hostarg), port(portarg), io_ctx(io_ctxx)
{
connect();
}
void network::tcp::client::connect()
@@ -175,21 +177,30 @@ void network::tcp::client::connect()
if (this->conn)
return;
std::shared_ptr<connection> conn = std::make_shared<connection>(io_ctx, true, messages_counter);
conn->set_handler(std::bind(&client::handle_message, this, conn, std::placeholders::_1));
std::cout << "create sock" << std::endl;
connection *sockr = new connection(io_ctx, false, 0);
//std::cout << "sock crt" << std::endl;
//std::unique_ptr<connection> sock(sockr);
//this->conn = sock;
//connection *sockr = new connection(io_ctx, true, delta_count);
std::shared_ptr<connection> sock(sockr);
this->conn = sock;
sock->set_handler(std::bind(&client::handle_message, this, sock, 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,
sock->m_socket.open(endpoint.protocol());
sock->m_socket.set_option(asio::ip::tcp::no_delay(true));
sock->m_socket.async_connect(endpoint,
std::bind(&client::handle_accept, this, std::placeholders::_1));
std::cout << "connect start" <<std::endl;
this->conn = conn;
}
void network::tcp::client::handle_accept(const asio::error_code &err)
{
std::cout << "connect handl" <<std::endl;
if (!err)
{
conn->connected();

View File

@@ -1,4 +1,7 @@
#include "network/network.h"
//#define ASIO_ENABLE_HANDLER_TRACKING
#define ASIO_DISABLE_THREADS
#include <asio.hpp>
namespace network::tcp

View File

@@ -216,15 +216,19 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
// ------------
// client
int zzz = 10;
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta()
{
/*
if (conn && conn->state == connection::DEAD) {
conn = nullptr;
}
*/
if (!conn) {
connect();
zzz--;
if (zzz < 0)
connect();
}
if (delta_queue.empty()) {
@@ -258,8 +262,6 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
return;
}
messages_counter++;
if (msg.type == message::SERVER_HELLO) {
auto cmd = dynamic_cast<const server_hello&>(msg);
conn->state = connection::ACTIVE;
@@ -270,6 +272,8 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
WriteLog("net: accept received", logtype::net);
}
else if (msg.type == message::FRAME_INFO) {
delta_count++;
auto delta = dynamic_cast<const frame_info&>(msg);
auto now = std::chrono::high_resolution_clock::now();
delta_queue.push(std::make_pair(now, delta));

View File

@@ -84,7 +84,7 @@ namespace network
virtual void connect() = 0;
void handle_message(std::shared_ptr<connection> conn, const message &msg);
std::shared_ptr<connection> conn;
size_t messages_counter = 0;
size_t delta_count = 0;
std::queue<
std::pair<std::chrono::high_resolution_clock::time_point,