2022-09-05 13:40:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* server.cpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 5 сент. 2022 г.
|
|
|
|
|
* Author: alexander
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <server.hpp>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <unistd.h>
|
2022-09-06 14:21:06 +00:00
|
|
|
|
#include <string.h>
|
2022-09-07 12:43:34 +00:00
|
|
|
|
#include <algorithm>
|
2022-09-05 13:40:50 +00:00
|
|
|
|
|
|
|
|
|
namespace zh
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ServerTCP::Hook::Hook(std::string command, hook handler) : _command(command), _handler(handler)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
void ServerTCP::Hook::execute(std::string buffer, ServerTCP &s)
|
2022-09-05 13:40:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (_command == "")
|
|
|
|
|
{
|
|
|
|
|
_handler(buffer, s);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
auto pos = buffer.find_first_of(_command);
|
|
|
|
|
if (pos != std::string::npos && pos == 0)
|
|
|
|
|
{
|
|
|
|
|
_handler(buffer, s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerTCP::registerHook(std::string command, hook handler)
|
|
|
|
|
{
|
|
|
|
|
_hooks.push_back({ command, handler });
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
ServerTCP::ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer) : Server(port, sizeBuffer), _bind(false), _connfd(-1), _listenLoop(true)
|
2022-09-05 13:40:50 +00:00
|
|
|
|
{
|
|
|
|
|
_socket = std::make_unique<Socket>(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
2022-09-07 12:43:34 +00:00
|
|
|
|
_address = std::make_unique<Address>(AF_INET, INADDR_ANY, port);
|
|
|
|
|
_sizeClient = sizeof(_client);
|
2022-09-05 13:40:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerTCP::bind()
|
|
|
|
|
{
|
|
|
|
|
if (_bind)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Адрес уже связан с дескриптором слушающего сокета!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
if (::bind((*_socket), reinterpret_cast<const sockaddr*>(&(*_address)), (*_address).size()) != 0)
|
2022-09-05 13:40:50 +00:00
|
|
|
|
std::cerr << "Не удаётся связать адрес с дескриптором слушающего сокета!" << std::endl;
|
|
|
|
|
else
|
|
|
|
|
_bind = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerTCP::listen()
|
|
|
|
|
{
|
|
|
|
|
if (::listen(*_socket, 0) != 0)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Не удаётся создать очередь соединений для сокета!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (_bind)
|
|
|
|
|
{
|
|
|
|
|
if ((_connfd = ::accept(*_socket, reinterpret_cast<sockaddr*>(&(*_client)), &_sizeClient)) < 0)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Не удаётся верифицировать и принять пакеты от клиента!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-07 12:43:34 +00:00
|
|
|
|
else
|
|
|
|
|
_listenLoop = true;
|
2022-09-05 13:40:50 +00:00
|
|
|
|
|
|
|
|
|
// Обработка соединения с клиентом
|
|
|
|
|
chat();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
void ServerTCP::chat()
|
2022-09-06 14:21:06 +00:00
|
|
|
|
{
|
2022-09-07 12:43:34 +00:00
|
|
|
|
while (_listenLoop)
|
|
|
|
|
{
|
|
|
|
|
Buffer *bf = &(*_buffer);
|
|
|
|
|
bf->clear();
|
|
|
|
|
int size = read(_connfd, *bf, bf->getSize());
|
|
|
|
|
if (size > 0)
|
|
|
|
|
std::for_each(_hooks.begin(), _hooks.end(), [&](Hook &hook)
|
|
|
|
|
{
|
|
|
|
|
hook.execute(bf->getString(), *this);
|
|
|
|
|
});
|
|
|
|
|
else if (size < 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
close(_connfd);
|
|
|
|
|
}
|
2022-09-06 14:21:06 +00:00
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
void ServerTCP::stop()
|
|
|
|
|
{
|
|
|
|
|
_listenLoop = false;
|
|
|
|
|
_bind = 0;
|
2022-09-06 14:21:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 12:43:34 +00:00
|
|
|
|
void ServerTCP::disconnect()
|
2022-09-05 13:40:50 +00:00
|
|
|
|
{
|
2022-09-07 12:43:34 +00:00
|
|
|
|
_listenLoop = false;
|
2022-09-05 13:40:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|