#include "forwardsender.h" ForwardSender::ForwardSender() : TransmittedBytes(0), socketFrom(-1), socketTo(-1) { qDebug(" +ForwardSender"); //timerID = this->startTimer(100); //if(timerID == 0) { // perror("Could not start Timer"); //} } ForwardSender::~ForwardSender() { //killTimer(timerID); this->stopLoop(); if(this->isRunning()) { this->quit(); if( ! this->wait(500) ) { this->terminate(); if( ! this->wait(3000) ) std::cout << " Terminating Thread timeouted: " << __func__ << std::endl; } } qDebug(" ~ForwardSender"); } /* void ForwardSender::timerEvent(QTimerEvent *) { qDebug("%s", ("Trasmitted: " + QString::number(TransmittedBytes)).toStdString().c_str()); emit transmittedBytes(vector_index, isSendingToRecirectServer, TransmittedBytes); TransmittedBytes = 0; } */ int ForwardSender::startRedirecting(int Socketfrom, int SocketTo, int vector_index, bool isSendingToRecirectServer) { if(this->isRunning()) return -1; this->socketFrom = Socketfrom; this->socketTo = SocketTo; this->vector_index = vector_index; this->isSendingToRecirectServer = isSendingToRecirectServer; this->start(); return (this->isRunning()) ? 0 : 1; } void ForwardSender::stopLoop() { shoudStop = true; } void ForwardSender::run() { qDebug(" -> ForwardSender::run()"); shoudStop = false; char buffer; while ( !shoudStop ) { if( read( socketFrom, &buffer, 1) <= 0 ) { std::cerr << " DEBUG: read() failed." << std::endl; return emit errorOccurred( (isSendingToRecirectServer) ? ERROR_LIST::RECV_FROM_LOCAL_SERVER_FAILED : ERROR_LIST::RECV_FROM_REDIRECT_SERVER_FAILED, vector_index ); } else if ( write(socketTo, &buffer, 1) <= 0) { std::cerr << " DEBUG: write() failed." << std::endl; return emit errorOccurred( (isSendingToRecirectServer) ? ERROR_LIST::SEND_TO_REDIRECT_SERVER_FAILED : ERROR_LIST::SEND_TO_LOCAL_SERVER_FAILED, vector_index ); } TransmittedBytes++; } }