diff --git a/lesson_03/header/buffer.hpp b/lesson_03/header/buffer.hpp index 208bd7b..1705611 100644 --- a/lesson_03/header/buffer.hpp +++ b/lesson_03/header/buffer.hpp @@ -22,7 +22,7 @@ public: const unsigned short int getSize() const; const std::string getString() const; void clear(); - operator void*(); + operator char*(); }; } diff --git a/lesson_03/header/client.hpp b/lesson_03/header/client.hpp index 08ec0a0..d5e83f0 100644 --- a/lesson_03/header/client.hpp +++ b/lesson_03/header/client.hpp @@ -16,7 +16,6 @@ class ClientTCP: public Server // @suppress("Class has a virtual method and non- { private: bool _connect; - int _connfd; void chat(); public: diff --git a/lesson_03/main.cpp b/lesson_03/main.cpp index 04d2d5b..bb9600d 100644 --- a/lesson_03/main.cpp +++ b/lesson_03/main.cpp @@ -75,7 +75,7 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - std::string host = optionHost.getValues()[0]; + auto host = optionHost.getValues()[0]; client(port, host); } diff --git a/lesson_03/source/buffer.cpp b/lesson_03/source/buffer.cpp index 05ebc35..706ab6a 100644 --- a/lesson_03/source/buffer.cpp +++ b/lesson_03/source/buffer.cpp @@ -32,9 +32,9 @@ void Buffer::clear() bzero(_buffer.get(), _size); } -Buffer::operator void*() +Buffer::operator char*() { - return _buffer.get(); + return reinterpret_cast(_buffer.get()); } } diff --git a/lesson_03/source/client.cpp b/lesson_03/source/client.cpp index 4c7073e..a70bd52 100644 --- a/lesson_03/source/client.cpp +++ b/lesson_03/source/client.cpp @@ -13,7 +13,7 @@ namespace zh { -ClientTCP::ClientTCP(const std::string &address, const unsigned short int port, const unsigned short int sizeBuffer) : Server(port, sizeBuffer), _connect(false), _connfd(-1) +ClientTCP::ClientTCP(const std::string &address, const unsigned short int port, const unsigned short int sizeBuffer) : Server(port, sizeBuffer), _connect(false) { _socket = std::make_unique(AF_INET, SOCK_STREAM, IPPROTO_IP); _address = std::make_unique
(AF_INET, address, port); @@ -27,7 +27,7 @@ void ClientTCP::connect() return; } - if (::connect((*_socket), reinterpret_cast(&(*_address)), (*_address).size()) != 0) + if (::connect(*_socket, reinterpret_cast(&(*_address)), (*_address).size()) != 0) std::cerr << "Не удаётся подключить клиентский сокет к серверному сокету!" << std::endl; else _connect = true; @@ -37,12 +37,20 @@ void ClientTCP::connect() void ClientTCP::chat() { + Buffer *bf = &(*_buffer); while (_connect) { - Buffer *bf = &(*_buffer); bf->clear(); - std::cin.getline(reinterpret_cast(bf->operator void *()), bf->getSize()); - write(_connfd, *bf, bf->getSize()); + std::cin.getline(*bf, bf->getSize()); + write(*_socket, *bf, bf->getSize()); + bf->clear(); + if (read(*_socket, *bf, bf->getSize()) > 0) + { + if (bf->getString() == "exit") + break; + else + std::cout << '\t' << bf->getString() << std::endl; + } } close(*_socket); } diff --git a/lesson_03/source/server.cpp b/lesson_03/source/server.cpp index 9fd9ac3..56ee3bc 100644 --- a/lesson_03/source/server.cpp +++ b/lesson_03/source/server.cpp @@ -26,8 +26,7 @@ void ServerTCP::Hook::execute(std::string buffer, ServerTCP &s) } else { - auto pos = buffer.find_first_of(_command); - if (pos != std::string::npos && pos == 0) + if (buffer == _command) { _handler(buffer, s); } @@ -54,7 +53,7 @@ void ServerTCP::bind() return; } - if (::bind((*_socket), reinterpret_cast(&(*_address)), (*_address).size()) != 0) + if (::bind(*_socket, reinterpret_cast(&(*_address)), (*_address).size()) != 0) std::cerr << "Не удаётся связать адрес с дескриптором слушающего сокета!" << std::endl; else _bind = true; @@ -85,16 +84,22 @@ void ServerTCP::listen() void ServerTCP::chat() { + Buffer *bf = &(*_buffer); 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); }); + if (!_listenLoop) + write(_connfd, "exit", 4); + else + write(_connfd, "OK", 2); + } else if (size < 0) break; }