2022-09-05 13:40:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* server.hpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 5 сент. 2022 г.
|
|
|
|
|
* Author: alexander
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
//#include <algorithm>
|
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
#include <stock.hpp>
|
2022-09-05 13:40:50 +00:00
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
namespace zh
|
|
|
|
|
{
|
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
class ServerTCP;
|
2022-09-05 13:40:50 +00:00
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
typedef void (*hook)(std::string buffer, ServerTCP &s);
|
2022-09-05 13:40:50 +00:00
|
|
|
|
|
|
|
|
|
class ServerTCP: public Server // @suppress("Class has a virtual method and non-virtual destructor")
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
bool _bind;
|
2022-09-07 12:43:34 +00:00
|
|
|
|
int _connfd;
|
|
|
|
|
bool _listenLoop;
|
|
|
|
|
unsigned int _sizeClient;
|
|
|
|
|
std::unique_ptr<Address> _client;
|
2022-09-05 13:40:50 +00:00
|
|
|
|
|
|
|
|
|
class Hook
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
std::string _command;
|
|
|
|
|
hook _handler;
|
|
|
|
|
public:
|
|
|
|
|
Hook(std::string command, hook handler);
|
2022-09-07 12:43:34 +00:00
|
|
|
|
void execute(std::string buffer, ServerTCP &s);
|
2022-09-05 13:40:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<Hook> _hooks;
|
|
|
|
|
|
|
|
|
|
void chat();
|
|
|
|
|
public:
|
|
|
|
|
ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer = 1024);
|
2022-09-07 12:43:34 +00:00
|
|
|
|
void registerHook(std::string command, hook handler);
|
2022-09-05 13:40:50 +00:00
|
|
|
|
void bind();
|
|
|
|
|
void listen();
|
2022-09-07 12:43:34 +00:00
|
|
|
|
void disconnect();
|
|
|
|
|
void stop();
|
2022-09-05 13:40:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|