114 lines
2.8 KiB
C++
114 lines
2.8 KiB
C++
|
/*
|
|||
|
* server.cpp
|
|||
|
*
|
|||
|
* Created on: 5 сент. 2022 г.
|
|||
|
* Author: alexander
|
|||
|
*/
|
|||
|
|
|||
|
#include <server.hpp>
|
|||
|
#include <iostream>
|
|||
|
#include <unistd.h>
|
|||
|
|
|||
|
namespace zh
|
|||
|
{
|
|||
|
|
|||
|
#define MAX 1024
|
|||
|
|
|||
|
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);
|
|||
|
_buffer = std::make_unique<char[]>(sizeBuffer);
|
|||
|
}
|
|||
|
|
|||
|
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();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void ServerTCP::chat()
|
|||
|
{
|
|||
|
// char buff[MAX];
|
|||
|
// int n;
|
|||
|
while (true)
|
|||
|
{
|
|||
|
// bzero(buff, sizeof(buff));
|
|||
|
// printf("Enter the string : ");
|
|||
|
// n = 0;
|
|||
|
// while ((buff[n++] = getchar()) != '\n')
|
|||
|
// ;
|
|||
|
// write(_connfd, buff, sizeof(buff));
|
|||
|
bzero(_buffer.get(), _sizeBuffer);
|
|||
|
read(_connfd, _buffer.get(), _sizeBuffer);
|
|||
|
std::cout << _buffer.get() << std::endl;
|
|||
|
// printf("From Server : %s", *_buffer);
|
|||
|
// if ((strncmp((*_buffer), "exit", 4)) == 0)
|
|||
|
// {
|
|||
|
// printf("Client Exit...\n");
|
|||
|
// break;
|
|||
|
// }
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|