void ExchangeRateClient::beginRequest(){ m_socket = new QTcpSocket(); connect(m_socket, SIGNAL(connected()), this, SLOT(connectedHandle())); m_socket->connectToHost(m_serverAddress, m_serverPort);}void ExchangeRateClient::connectedHandle(){ if(!m_socket->isValid()) { qCritical() << "Could not connect to server. Action canceled."; return; } QByteArray xmlData = m_requestInfo->xml().toUtf8(); m_xml = new QDataStream(xmlData); connect(m_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(sendNextXmlPiece(qint64))); QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_3); out << quint64(xmlData.size()); m_socket->write(block); connect(m_socket, SIGNAL(readyRead()), this, SLOT(responseHandle())); connect(m_socket, SIGNAL(disconnected()), this, SLOT(disconnectedHandle())); connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorHandle(QAbstractSocket::SocketError)));}void ExchangeRateClient::sendNextXmlPiece(qint64 bytes){ if(m_socket->bytesToWrite() > XML_CHUNK_SIZE || m_xml->atEnd()) { delete m_xml; m_xml = NULL; return; } long int lBytes = 0; char buffer[XML_CHUNK_SIZE]; buffer[XML_CHUNK_SIZE - 1] = '\0'; int l = m_xml->readRawData(buffer, sizeof(char)* (XML_CHUNK_SIZE - 1)); QByteArray ba(buffer, sizeof(char)*l); lBytes = m_socket->write(ba, sizeof(char)*l); qDebug() << QString(buffer); qDebug() << lBytes; m_socket->waitForBytesWritten();}
void ServerObject::handleConnection(){ m_socket = new QTcpSocket(); if(!m_socket->setSocketDescriptor(m_socketDescriptor)) { qCritical() << "QTcpSocket::setSocketDescriptor() failed, thread terminating..."; emit finished(); return; } connect(m_socket, SIGNAL(readyRead()), this, SLOT(readReady())); connect(m_socket, SIGNAL(disconnected()), this, SLOT(disconnected()));}void ServerObject::readReady(){ m_write = new QDataStream(&incomingXml, QIODevice::WriteOnly); m_write->setVersion(QDataStream::Qt_5_3); QByteArray bArray; QDataStream dStream(&bArray, QIODevice::ReadWrite); dStream.setVersion(QDataStream::Qt_5_3); bArray = m_socket->readAll(); dStream >> blockSize; qDebug() << blockSize; disconnect(m_socket, SIGNAL(readyRead()), 0, 0); connect(m_socket, SIGNAL(readyRead()), this, SLOT(dataReceive()));}void ServerObject::dataReceive(){ QByteArray tmp = m_socket->readAll(); m_bytePosition += m_write->writeRawData(tmp.data(), tmp.size()); qDebug() << m_bytePosition; qDebug() << QString::fromUtf8(incomingXml.data()); if(m_bytePosition >= blockSize) { completeAndClose(); }}bool ServerObject::completeAndClose(){ reply(invokeMethod(QString::fromUtf8(incomingXml.data()))); m_socket->close(); delete m_write; return true;}