mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
work
This commit is contained in:
12
network/manager.cpp
Normal file
12
network/manager.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "network/manager.h"
|
||||
#include "network/tcp.h"
|
||||
|
||||
network::manager::manager()
|
||||
{
|
||||
servers.emplace_back(std::make_shared<tcp_server>(io_context));
|
||||
}
|
||||
|
||||
void network::manager::poll()
|
||||
{
|
||||
io_context.poll();
|
||||
}
|
||||
18
network/manager.h
Normal file
18
network/manager.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <asio.hpp>
|
||||
#include <memory>
|
||||
#include "network/network.h"
|
||||
|
||||
namespace network
|
||||
{
|
||||
class manager
|
||||
{
|
||||
asio::io_context io_context;
|
||||
std::vector<std::shared_ptr<server>> servers;
|
||||
|
||||
public:
|
||||
manager();
|
||||
|
||||
void poll();
|
||||
};
|
||||
}
|
||||
18
network/message.cpp
Normal file
18
network/message.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "network/message.h"
|
||||
#include "sn_utils.h"
|
||||
|
||||
void network::message::serialize(std::ostream &stream)
|
||||
{
|
||||
sn_utils::ls_uint16(stream, (uint16_t)type);
|
||||
}
|
||||
|
||||
network::message network::message::deserialize(std::istream &stream)
|
||||
{
|
||||
type_e type = (type_e)sn_utils::ld_uint16(stream);
|
||||
if ((int)type > (int)message::TYPE_MAX)
|
||||
{
|
||||
type = message::TYPE_MAX;
|
||||
}
|
||||
|
||||
return message({type});
|
||||
}
|
||||
23
network/message.h
Normal file
23
network/message.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "network/message.h"
|
||||
|
||||
namespace network
|
||||
{
|
||||
struct message
|
||||
{
|
||||
enum type_e
|
||||
{
|
||||
CONNECT_REQUEST = 0,
|
||||
CONNECT_ACCEPT,
|
||||
STEP_INFO,
|
||||
VEHICLE_COMMAND,
|
||||
TYPE_MAX
|
||||
};
|
||||
|
||||
type_e type;
|
||||
|
||||
void serialize(std::ostream &stream);
|
||||
|
||||
static message deserialize(std::istream &stream);
|
||||
};
|
||||
}
|
||||
18
network/network.cpp
Normal file
18
network/network.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "network/network.h"
|
||||
#include "network/message.h"
|
||||
#include "Logs.h"
|
||||
|
||||
void network::connection::connected()
|
||||
{
|
||||
WriteLog("net: socket connected", logtype::net);
|
||||
}
|
||||
|
||||
void network::connection::message_received(message msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void network::connection::data_received(uint8_t *buffer, size_t len)
|
||||
{
|
||||
|
||||
}
|
||||
29
network/network.h
Normal file
29
network/network.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <asio.hpp>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include "network/message.h"
|
||||
|
||||
namespace network
|
||||
{
|
||||
class connection : public std::enable_shared_from_this<connection>
|
||||
{
|
||||
private:
|
||||
void message_received(message msg);
|
||||
|
||||
protected:
|
||||
virtual void disconnect() = 0;
|
||||
virtual void send_data(uint8_t *buffer, size_t len) = 0;
|
||||
void data_received(uint8_t *buffer, size_t len);
|
||||
|
||||
public:
|
||||
virtual void connected();
|
||||
};
|
||||
|
||||
class server
|
||||
{
|
||||
protected:
|
||||
std::vector<std::shared_ptr<connection>> clients;
|
||||
};
|
||||
}
|
||||
111
network/tcp.cpp
Normal file
111
network/tcp.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include "network/tcp.h"
|
||||
#include "Logs.h"
|
||||
#include "sn_utils.h"
|
||||
|
||||
network::tcp_conn::tcp_conn(asio::io_context &io_ctx)
|
||||
: m_socket(io_ctx)
|
||||
{
|
||||
m_header_buffer.resize(12);
|
||||
}
|
||||
|
||||
void network::tcp_conn::disconnect()
|
||||
{
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
void network::tcp_conn::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));
|
||||
}
|
||||
|
||||
void network::tcp_conn::send_data(uint8_t *buffer, size_t len)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void network::tcp_conn::handle_header(const asio::error_code &err)
|
||||
{
|
||||
std::istringstream header(m_header_buffer);
|
||||
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));
|
||||
}
|
||||
|
||||
void network::tcp_conn::handle_data(const asio::error_code &err)
|
||||
{
|
||||
std::istringstream body(m_body_buffer);
|
||||
message msg = message::deserialize(body);
|
||||
|
||||
if (msg.type == message::TYPE_MAX)
|
||||
{
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type == message::CONNECT_REQUEST)
|
||||
{
|
||||
message reply({message::CONNECT_ACCEPT});
|
||||
|
||||
std::string reply_buf;
|
||||
std::ostringstream stream(reply_buf);
|
||||
reply.serialize(stream);
|
||||
}
|
||||
|
||||
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, asio::ip::tcp::endpoint(asio::ip::tcp::v6(), 7424))
|
||||
{
|
||||
asio::ip::v6_only v6_only(false);
|
||||
m_acceptor.set_option(v6_only);
|
||||
|
||||
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();
|
||||
}
|
||||
45
network/tcp.h
Normal file
45
network/tcp.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#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_header(const asio::error_code &err);
|
||||
void handle_data(const asio::error_code &err);
|
||||
|
||||
protected:
|
||||
void disconnect() override;
|
||||
void send_data(uint8_t *buffer, size_t len) override;
|
||||
|
||||
public:
|
||||
tcp_conn(asio::io_context &io_ctx);
|
||||
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;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user