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-05 13:40:50 +00:00
|
|
|
|
|
|
|
|
|
namespace zh
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Server::Server(const unsigned short int port) : _port(port), _connfd(-1)
|
|
|
|
|
{
|
|
|
|
|
_sizeClient = sizeof(*_client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerTCP::Hook::Hook(std::string command, hook handler) : _command(command), _handler(handler)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerTCP::Hook::execute(std::string buffer, Server &s)
|
|
|
|
|
{
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerTCP::ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer) : Server(port), _bind(false), _sizeBuffer(sizeBuffer)
|
|
|
|
|
{
|
|
|
|
|
_socket = std::make_unique<Socket>(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
|
|
|
|
_local = std::make_unique<Address>(AF_INET, INADDR_ANY, port);
|
2022-09-06 14:21:06 +00:00
|
|
|
|
_buffer = std::make_unique<std::byte[]>(sizeBuffer);
|
2022-09-05 13:40:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerTCP::bind()
|
|
|
|
|
{
|
|
|
|
|
if (_bind)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Адрес уже связан с дескриптором слушающего сокета!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (::bind((*_socket), reinterpret_cast<const sockaddr*>(&(*_local)), (*_local).size()) != 0)
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Обработка соединения с клиентом
|
|
|
|
|
chat();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 14:21:06 +00:00
|
|
|
|
void ServerTCP::readData(const int sizeData)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-05 13:40:50 +00:00
|
|
|
|
void ServerTCP::chat()
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
bzero(_buffer.get(), _sizeBuffer);
|
2022-09-06 14:21:06 +00:00
|
|
|
|
read(_connfd, _buffer.get(), 4);
|
|
|
|
|
std::cout << *reinterpret_cast<int*>(_buffer.get()) << std::endl;
|
|
|
|
|
close(_connfd);
|
|
|
|
|
break;
|
2022-09-05 13:40:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|