48 lines
937 B
C++
48 lines
937 B
C++
/*
|
||
* server.hpp
|
||
*
|
||
* Created on: 20 сент. 2022 г.
|
||
* Author: alexander
|
||
*/
|
||
|
||
#pragma once
|
||
|
||
#include <boost/asio.hpp>
|
||
#include <connection/connection.hpp>
|
||
#include <vector>
|
||
#include <functional>
|
||
|
||
namespace azh
|
||
{
|
||
enum class IPV
|
||
{
|
||
V4, V6
|
||
};
|
||
|
||
class TCPServer
|
||
{
|
||
public:
|
||
TCPServer(IPV ipv, int port);
|
||
|
||
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);
|
||
private:
|
||
IPV _ipVersion;
|
||
int _port;
|
||
boost::asio::io_context _ioContext;
|
||
boost::asio::ip::tcp::acceptor _acceptor;
|
||
std::vector<Connection::pointer> _connections;
|
||
private:
|
||
void startAccept();
|
||
};
|
||
|
||
}
|