/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* Client.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bcausseq +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/09/15 21:48:12 by bcausseq #+# #+# */ /* Updated: 2026/09/27 22:43:01 by bcausseq ### ########.fr */ /* */ /* ************************************************************************** */ #include "Client.hpp" #include "http/Header.hpp" #include #include #include #include #include Client::Client(int fd, Config const& config) : _fd(fd), _in_buffer(""), _out_buffer(""), _should_disconnect(false), _config(config) { } Client::~Client() { this->disconnect(); } void Client::receive(std::vector& pollables) { (void) pollables; char buf[4096]; ssize_t bytes = ::recv(get_fd(), buf, 4096, 0); if (bytes > 0) { std::stringstream ss; ss << "Received " << bytes << " bytes"; log(ss.str()); _in_buffer.append(buf, static_cast(bytes)); parse_header(*this);//j'ai pas trouvé mieux //parse_header(this, &pfd, s); } else if (bytes == 0) { this->_should_disconnect = true; return; } } void Client::send() { if (_out_buffer.empty()) return; std::stringstream ss; ss << "Sending " << _out_buffer.size() << " bytes..."; log(ss.str()); ssize_t bytes = ::send(get_fd(), _out_buffer.c_str(), _out_buffer.size(), 0); if (bytes <= 0) { this->_should_disconnect = true; return; } ss.clear(); ss << "Actually sent " << bytes << " bytes"; log(ss.str()); _out_buffer.erase(0 , static_cast(bytes)); } void Client::disconnect() { if (_fd > 2) { close(_fd); _fd = -1; } } void Client::log(std::string message) { std::cout << "[Client " << this->_fd << "] " << message << std::endl; } bool Client::should_disconnect() const { return this->_should_disconnect; } int Client::get_fd() const { return (_fd); } void Client::set_append_out_buffer(const std::string &message) { if (_out_buffer.empty()) _out_buffer = message; else _out_buffer += message; } std::string Client::get_in_buffer() const { return (_in_buffer); } Config const& Client::get_config() const { return _config; }