geekbrains_network_programming/lesson_04/server/tcp_server.hpp

46 lines
774 B
C++
Raw Normal View History

2022-09-20 13:37:06 +00:00
/*
* server.hpp
*
* Created on: 20 сент. 2022 г.
* Author: alexander
*/
#pragma once
#include <boost/asio.hpp>
#include <connection/connection.hpp>
#include <vector>
2022-09-21 06:57:37 +00:00
#include <optional>
#include <unordered_set>
2022-09-20 13:37:06 +00:00
namespace azh
{
2022-09-21 06:57:37 +00:00
namespace io = boost::asio;
2022-09-20 13:37:06 +00:00
enum class IPV
{
V4, V6
};
class TCPServer
{
public:
TCPServer(IPV ipv, int port);
2022-09-20 13:37:06 +00:00
int run();
2022-09-21 06:57:37 +00:00
void broadcast(const std::string& message);
2022-09-20 13:37:06 +00:00
private:
IPV _ipVersion;
int _port;
2022-09-21 06:57:37 +00:00
io::io_context _ioContext;
io::ip::tcp::acceptor _acceptor;
std::optional<io::ip::tcp::socket> _socket;
std::unordered_set<Connection::pointer> _connections;
2022-09-20 13:37:06 +00:00
private:
void startAccept();
};
}