From 3c5834e2069514567d93c1cde9bf16d053e3729f Mon Sep 17 00:00:00 2001 From: uvos Date: Fri, 14 Jan 2022 18:40:00 +0100 Subject: [PATCH] add missing files --- clienthandler.cpp | 0 clienthandler.h | 125 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 clienthandler.cpp create mode 100644 clienthandler.h diff --git a/clienthandler.cpp b/clienthandler.cpp new file mode 100644 index 0000000..e69de29 diff --git a/clienthandler.h b/clienthandler.h new file mode 100644 index 0000000..e8eedbb --- /dev/null +++ b/clienthandler.h @@ -0,0 +1,125 @@ +#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)); +}