готовый сервер

This commit is contained in:
Alexander Zhirov 2022-09-21 09:57:37 +03:00
parent 7c3a27d64a
commit 089820116b
4 changed files with 27 additions and 36 deletions

View File

@ -10,7 +10,7 @@
namespace azh
{
Connection::Connection(boost::asio::io_context &ioContext) : _socket(ioContext)
Connection::Connection(io::ip::tcp::socket&& socket) : _socket(std::move(socket))
{
}
@ -43,8 +43,8 @@ namespace azh
});
}
Connection::pointer Connection::create(boost::asio::io_context &ioContext)
Connection::pointer Connection::create(io::ip::tcp::socket&& socket)
{
return pointer(new Connection(ioContext));
return pointer(new Connection(std::move(socket)));
}
}

View File

@ -13,20 +13,21 @@
namespace azh
{
using boost::asio::ip::tcp;
namespace io = boost::asio;
class Connection : public std::enable_shared_from_this<Connection>
{
public:
using pointer = std::shared_ptr<Connection>;
static pointer create(boost::asio::io_context& ioContext);
static pointer create(io::ip::tcp::socket&& socket);
tcp::socket& socket();
void start();
private:
tcp::socket _socket;
std::string _message = "Hello, client!\n";
private:
explicit Connection(boost::asio::io_context& ioContext);
explicit Connection(io::ip::tcp::socket&& socket);
};
}

View File

@ -33,33 +33,25 @@ namespace azh
return 0;
}
void TCPServer::broadcast(const std::string& message)
{
}
void TCPServer::startAccept()
{
auto connection = Connection::create(_ioContext);
_socket.emplace(_ioContext);
_connections.push_back(connection);
_acceptor.async_accept(connection->socket(), [connection, this](const boost::system::error_code &error)
_acceptor.async_accept(*_socket, [this](const boost::system::error_code &error)
{
auto connection = Connection::create(std::move(*_socket));
_connections.insert(connection);
if (!error)
connection->start();
startAccept();
});
}
template<typename T>
void writeToConnection(int connectionIndex, const T& message)
{
}
template<typename T>
using listenCallback = std::function<void(int, const T&)>;
template<typename T>
void registerListenCallback(int connectionIndex, listenCallback<T> callback)
{
}
}

View File

@ -10,10 +10,13 @@
#include <boost/asio.hpp>
#include <connection/connection.hpp>
#include <vector>
#include <functional>
#include <optional>
#include <unordered_set>
namespace azh
{
namespace io = boost::asio;
enum class IPV
{
V4, V6
@ -26,20 +29,15 @@ namespace azh
int run();
template<typename T>
void writeToConnection(int connectionIndex, const T& message);
template<typename T>
using listenCallback = std::function<void(int, const T&)>;
template<typename T>
void registerListenCallback(int connectionIndex, listenCallback<T> callback);
void broadcast(const std::string& message);
private:
IPV _ipVersion;
int _port;
boost::asio::io_context _ioContext;
boost::asio::ip::tcp::acceptor _acceptor;
std::vector<Connection::pointer> _connections;
io::io_context _ioContext;
io::ip::tcp::acceptor _acceptor;
std::optional<io::ip::tcp::socket> _socket;
std::unordered_set<Connection::pointer> _connections;
private:
void startAccept();
};