2022-09-05 13:40:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* server.hpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 5 сент. 2022 г.
|
|
|
|
|
* Author: alexander
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
//#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include <socket.hpp>
|
|
|
|
|
#include <address.hpp>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
namespace zh
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class Server // @suppress("Class has a virtual method and non-virtual destructor")
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
std::unique_ptr<Socket> _socket;
|
|
|
|
|
std::unique_ptr<Address> _local, _client;
|
|
|
|
|
unsigned int _sizeClient;
|
|
|
|
|
const unsigned short int _port;
|
|
|
|
|
int _connfd;
|
|
|
|
|
public:
|
|
|
|
|
Server(const unsigned short int port);
|
|
|
|
|
virtual void bind() = 0;
|
|
|
|
|
virtual void listen() = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef void (*hook)(std::string buffer, Server &s);
|
|
|
|
|
|
|
|
|
|
class ServerTCP: public Server // @suppress("Class has a virtual method and non-virtual destructor")
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
bool _bind;
|
|
|
|
|
const unsigned short int _sizeBuffer;
|
2022-09-06 14:21:06 +00:00
|
|
|
|
std::unique_ptr<std::byte[]> _buffer;
|
2022-09-05 13:40:50 +00:00
|
|
|
|
|
|
|
|
|
class Hook
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
std::string _command;
|
|
|
|
|
hook _handler;
|
|
|
|
|
public:
|
|
|
|
|
Hook(std::string command, hook handler);
|
|
|
|
|
void execute(std::string buffer, Server &s);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<Hook> _hooks;
|
|
|
|
|
|
|
|
|
|
void chat();
|
2022-09-06 14:21:06 +00:00
|
|
|
|
void readData(const int sizeData);
|
2022-09-05 13:40:50 +00:00
|
|
|
|
public:
|
|
|
|
|
ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer = 1024);
|
|
|
|
|
void bind();
|
|
|
|
|
void listen();
|
|
|
|
|
void registerHook(std::string command, hook handler);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|