mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-18 00:49:19 +02:00
work
This commit is contained in:
@@ -258,3 +258,7 @@ target_link_libraries(${PROJECT_NAME} ASIO::ASIO)
|
|||||||
if (WIN32)
|
if (WIN32)
|
||||||
target_link_libraries(${PROJECT_NAME} ws2_32)
|
target_link_libraries(${PROJECT_NAME} ws2_32)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize=undefined")
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=undefined")
|
||||||
|
set(CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -fsanitize=address -fsanitize=undefined")
|
||||||
|
|||||||
@@ -3,7 +3,8 @@
|
|||||||
#include "Logs.h"
|
#include "Logs.h"
|
||||||
|
|
||||||
network::tcp::connection::connection(asio::io_context &io_ctx, bool client, size_t counter)
|
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);
|
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)
|
network::tcp::client::client(asio::io_context &io_ctxx, const std::string &hostarg, uint32_t portarg)
|
||||||
: host(host), port(port), io_ctx(io_ctx)
|
: host(hostarg), port(portarg), io_ctx(io_ctxx)
|
||||||
{
|
{
|
||||||
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void network::tcp::client::connect()
|
void network::tcp::client::connect()
|
||||||
@@ -175,21 +177,30 @@ void network::tcp::client::connect()
|
|||||||
if (this->conn)
|
if (this->conn)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::shared_ptr<connection> conn = std::make_shared<connection>(io_ctx, true, messages_counter);
|
std::cout << "create sock" << std::endl;
|
||||||
conn->set_handler(std::bind(&client::handle_message, this, conn, std::placeholders::_1));
|
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::tcp::endpoint endpoint(
|
||||||
asio::ip::address::from_string(host), port);
|
asio::ip::address::from_string(host), port);
|
||||||
conn->m_socket.open(endpoint.protocol());
|
sock->m_socket.open(endpoint.protocol());
|
||||||
conn->m_socket.set_option(asio::ip::tcp::no_delay(true));
|
sock->m_socket.set_option(asio::ip::tcp::no_delay(true));
|
||||||
conn->m_socket.async_connect(endpoint,
|
sock->m_socket.async_connect(endpoint,
|
||||||
std::bind(&client::handle_accept, this, std::placeholders::_1));
|
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)
|
void network::tcp::client::handle_accept(const asio::error_code &err)
|
||||||
{
|
{
|
||||||
|
std::cout << "connect handl" <<std::endl;
|
||||||
if (!err)
|
if (!err)
|
||||||
{
|
{
|
||||||
conn->connected();
|
conn->connected();
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "network/network.h"
|
#include "network/network.h"
|
||||||
|
//#define ASIO_ENABLE_HANDLER_TRACKING
|
||||||
|
#define ASIO_DISABLE_THREADS
|
||||||
|
|
||||||
#include <asio.hpp>
|
#include <asio.hpp>
|
||||||
|
|
||||||
namespace network::tcp
|
namespace network::tcp
|
||||||
|
|||||||
@@ -216,15 +216,19 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
|
|||||||
// ------------
|
// ------------
|
||||||
|
|
||||||
// client
|
// client
|
||||||
|
int zzz = 10;
|
||||||
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()
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
if (conn && conn->state == connection::DEAD) {
|
if (conn && conn->state == connection::DEAD) {
|
||||||
conn = nullptr;
|
conn = nullptr;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if (!conn) {
|
if (!conn) {
|
||||||
connect();
|
zzz--;
|
||||||
|
if (zzz < 0)
|
||||||
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (delta_queue.empty()) {
|
if (delta_queue.empty()) {
|
||||||
@@ -258,8 +262,6 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
messages_counter++;
|
|
||||||
|
|
||||||
if (msg.type == message::SERVER_HELLO) {
|
if (msg.type == message::SERVER_HELLO) {
|
||||||
auto cmd = dynamic_cast<const server_hello&>(msg);
|
auto cmd = dynamic_cast<const server_hello&>(msg);
|
||||||
conn->state = connection::ACTIVE;
|
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);
|
WriteLog("net: accept received", logtype::net);
|
||||||
}
|
}
|
||||||
else if (msg.type == message::FRAME_INFO) {
|
else if (msg.type == message::FRAME_INFO) {
|
||||||
|
delta_count++;
|
||||||
|
|
||||||
auto delta = dynamic_cast<const frame_info&>(msg);
|
auto delta = dynamic_cast<const frame_info&>(msg);
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
delta_queue.push(std::make_pair(now, delta));
|
delta_queue.push(std::make_pair(now, delta));
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ namespace network
|
|||||||
virtual void connect() = 0;
|
virtual void connect() = 0;
|
||||||
void handle_message(std::shared_ptr<connection> conn, const message &msg);
|
void handle_message(std::shared_ptr<connection> conn, const message &msg);
|
||||||
std::shared_ptr<connection> conn;
|
std::shared_ptr<connection> conn;
|
||||||
size_t messages_counter = 0;
|
size_t delta_count = 0;
|
||||||
|
|
||||||
std::queue<
|
std::queue<
|
||||||
std::pair<std::chrono::high_resolution_clock::time_point,
|
std::pair<std::chrono::high_resolution_clock::time_point,
|
||||||
|
|||||||
Reference in New Issue
Block a user