From e5df453d741f40ef85767fc2a5bb1ad0fcba83e8 Mon Sep 17 00:00:00 2001 From: "David A. Cuadrado" Date: Wed, 14 Nov 2007 13:27:09 -0500 Subject: [PATCH] Improved socket read/write --- src/dash/network/socket.cpp | 57 ++++++++++++++++++++++++++------------------ src/dash/network/socket.h | 4 ++-- src/shell/networkhandler.cpp | 21 +++++++++------- src/shell/networkhandler.h | 2 +- 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/dash/network/socket.cpp b/src/dash/network/socket.cpp index 7e6ce57..989552c 100644 --- a/src/dash/network/socket.cpp +++ b/src/dash/network/socket.cpp @@ -40,6 +40,8 @@ struct Socket::Private QList observers; Format format; + + QString readed; }; Socket::Socket(QObject *parent) @@ -93,7 +95,7 @@ void Socket::clearQueue() d->queue.clear(); } -void Socket::send(const QByteArray &data) +bool Socket::send(const QByteArray &data) { if( state() == QAbstractSocket::ConnectedState ) { @@ -101,34 +103,33 @@ void Socket::send(const QByteArray &data) { case Text: { -// QTextStream stream(this); -// stream << data.toBase64() << "%%" << endl; - - QByteArray toSend = data.toBase64()+"%%\n"; QByteArray block; - dfDebug << "ENVIANDO " << toSend.size() << " bytes"; - for(int i = 0; i < toSend.size(); i++) { block += toSend[i]; if( i % 1024 == 0 ) { - write(block); + if( write(block) == -1 ) + { + return false; + } block = QByteArray(); } } if( !block.isEmpty() ) { - write(block); + return write(block) != -1; } + + return true; } break; case Binary: { - write(data); + return write(data) != -1; } break; } @@ -139,9 +140,9 @@ void Socket::send(const QByteArray &data) } } -void Socket::send(const QDomDocument &doc) +bool Socket::send(const QDomDocument &doc) { - send(doc.toString(0).toLocal8Bit()); + return send(doc.toString(0).toLocal8Bit()); } void Socket::readFromServer() @@ -150,29 +151,38 @@ void Socket::readFromServer() { case Text: { - QString readed; -// while(this->canReadLine()) - while(bytesAvailable() > 0 ) { - bool process = false; - readed += this->readLine(); //this->read(1024); + int bytes = bytesAvailable(); - if ( readed.endsWith("%%\n") ) + bool process = false; + { + QString line = this->readLine(); + + if( line.isEmpty() ) + { + d->readed += this->read(1024); + } + else + { + d->readed += line; + } + } + if ( d->readed.endsWith("%%\n") ) { - readed.remove(readed.lastIndexOf("%%"), 2); + d->readed.remove(d->readed.lastIndexOf("%%"), 2); process = true; } - if( process && !readed.isEmpty() ) + if( process && !d->readed.isEmpty() ) { - readed = QByteArray::fromBase64(readed.toLocal8Bit()); + d->readed = QByteArray::fromBase64(d->readed.toLocal8Bit()); foreach(SocketObserver *o, d->observers) { - o->readed(readed); + o->readed(d->readed); } - readed = QString(); + d->readed = QString(); if( d->format != Text ) break; } @@ -181,6 +191,7 @@ void Socket::readFromServer() break; case Binary: { + d->readed = QString(); while(bytesAvailable() > 0) { QByteArray data = this->read(1024); diff --git a/src/dash/network/socket.h b/src/dash/network/socket.h index 6725133..d58fbab 100644 --- a/src/dash/network/socket.h +++ b/src/dash/network/socket.h @@ -55,8 +55,8 @@ class DASH_EXPORT Socket : public QTcpSocket //QSslSocket void addObserver(SocketObserver *observer); void removeObserver(SocketObserver *observer); - void send(const QByteArray &data); - void send(const QDomDocument &doc); + bool send(const QByteArray &data); + bool send(const QDomDocument &doc); protected slots: virtual void readFromServer(); diff --git a/src/shell/networkhandler.cpp b/src/shell/networkhandler.cpp index be6721b..400654c 100644 --- a/src/shell/networkhandler.cpp +++ b/src/shell/networkhandler.cpp @@ -89,7 +89,7 @@ void NetworkHandler::connectToHost(const QString &host, quint16 port, const QStr m_socket->send(cnx.toString().toLocal8Bit()); } -void NetworkHandler::send(const QString &xml) +bool NetworkHandler::send(const QString &xml) { QString tosend; if( !m_sign.isEmpty() ) @@ -116,9 +116,9 @@ void NetworkHandler::send(const QString &xml) tosend = xml; } - dfDebug << "SENDING: " << tosend << " SIGN IS : " << m_sign; + dfDebug << "SENDING: " << tosend.left(100) << " SIGN IS : " << m_sign; - m_socket->send(tosend.toLocal8Bit()); + return m_socket->send(tosend.toLocal8Bit()); } void NetworkHandler::addObserver(Dash::Network::Package::Observer *observer) @@ -135,13 +135,17 @@ void NetworkHandler::execute(YAMF::Command::Base *cmd) { bool isEmpty = m_commands.isEmpty(); - dfDebug << "ENCOLANDO: " << cmd->toXml(); + dfDebug << "ENCOLANDO: " << cmd->toXml().left(100); m_commands.enqueue(cmd); if( isEmpty && !m_waitingResponse && !m_sign.isEmpty() ) { - m_waitingResponse = true; - send(cmd->toXml()); + m_waitingResponse = send(cmd->toXml()); + + if( !m_waitingResponse) + { + m_commands.dequeue(); + } } } @@ -188,7 +192,7 @@ void NetworkHandler::showChatWindow() void NetworkHandler::readed(const QString &txt) { - dfDebug << "READED: " << txt; + dfDebug << "READED: " << txt.left(100); QString root; @@ -205,7 +209,7 @@ void NetworkHandler::readed(const QString &txt) if( root.isEmpty() ) { - dWarning() << "Invalid package: " << txt; + dWarning() << "Invalid package: " << txt.left(100)+"...."+txt.right(100); dWarning() << "Error was: " << reader.errorString(); } } @@ -259,7 +263,6 @@ void NetworkHandler::readed(const QString &txt) } } - if( !executed ) // External command { m_manager->setLocal(true); diff --git a/src/shell/networkhandler.h b/src/shell/networkhandler.h index 5adcc52..78ab734 100644 --- a/src/shell/networkhandler.h +++ b/src/shell/networkhandler.h @@ -62,7 +62,7 @@ class NetworkHandler : public QObject, Dash::Network::SocketObserver ~NetworkHandler(); void connectToHost(const QString &host, quint16 port, const QString &login, const QString &password); - void send(const QString &xml); + bool send(const QString &xml); void addObserver(Dash::Network::Package::Observer *observer); void removeObserver(Dash::Network::Package::Observer *observer); -- 2.11.4.GIT