#pragma once #include #include #include #include #include "Socket.h" #include "serial_io.h" class ClientHandler { private: bool _isBroadcasting = false; CommunicatingSocket *_socket; bool _disconnected = false; public: void cleanUp(); ClientHandler(CommunicatingSocket * const socket); ~ClientHandler(); bool operator==(int fd); bool operator!=(int fd); bool operator==(ClientHandler& other); bool operator!=(ClientHandler& other); bool isDisconnected(); bool run(std::vector* clients, int serial, bool verbose = false); void write(const char* buffer, const size_t len); void dropData(); }; ClientHandler::ClientHandler(CommunicatingSocket * const socket): _socket(socket) {} ClientHandler::~ClientHandler() { } void ClientHandler::cleanUp() { _socket->cleanUp(); delete _socket; } void ClientHandler::dropData() { std::array buffer; while(_socket->recv(buffer.data(), 4096) > 0); } bool ClientHandler::run(std::vector* clients, int serial, bool verbose) { std::array buffer; int reclen = 0; try { reclen = _socket->recv(buffer.data(), 4096); if(verbose) std::cout<<"Recived "< 0) { if(!_isBroadcasting && reclen >= 5 && strncmp( buffer.data(), "bcst:", 5) == 0) _isBroadcasting = true; if(_isBroadcasting) { if(verbose) std::cout<<"Boradcasting "<send(buffer, len); } catch (SocketException &e) { std::cout<getFD(); } bool ClientHandler::operator!=(int fd) { return fd != _socket->getFD(); } bool ClientHandler::operator==(ClientHandler& other) { return _socket->getFD() == other._socket->getFD(); } bool ClientHandler::operator!=(ClientHandler& other) { return !(operator==(other)); }