void FileTransfertUpload::onSocketWritten() { QTcpSocket *socket = dynamic_cast<QTcpSocket*>(sender()); if (socket->bytesToWrite() == 0) { socket->close(); socket->deleteLater(); _completed++; check_nbr(); } }
void Remote::TomahawkConnected() { qDebug() << Q_FUNC_INFO; QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender()); Q_ASSERT(socket); TomahawkConnection* connection = tomahawk_connections_[socket]; QVariantMap map; map["conntype"] = "accept-offer"; map["key"] = connection->key; map["port"] = 12345; map["nodeid"] = "foobar"; map["controlid"] = "foobarbaz"; QJson::Serializer serialiser; const QByteArray& serialised = serialiser.serialize(map); quint32 length = serialised.length(); // Send offer acceptance. { QDataStream stream(socket); stream << (quint32)length; stream << (quint8)(128 | 2); stream.writeRawData(serialised.constData(), serialised.length()); qDebug() << "now:" << socket->bytesToWrite(); } { QDataStream stream(socket); stream << (quint32)2; stream << (quint8)(128 | 1); stream.writeRawData("ok", 2); } // Send ping. { QDataStream stream(socket); stream << (quint32)0; stream << (quint8)(32); } /* { QVariantMap json; json["method"] = "dbsync-offer"; json["key"] = "abcdefg"; const QByteArray& request = serialiser.serialize(json); QDataStream stream(socket); stream << (quint32)request.length(); stream << (quint8)2; // JSON stream.writeRawData(request.constData(), request.length()); } */ }
void setEclipseProperty(const Matrix& propertyFrames, const QString &hostName, quint16 port, const qint64& caseId, QString propertyName, const int32NDArray& requestedTimeSteps, QString porosityModel) { QTcpSocket socket; socket.connectToHost(hostName, port); if (!socket.waitForConnected(riOctavePlugin::connectTimeOutMilliSecs)) { error((("Connection: ") + socket.errorString()).toLatin1().data()); return; } QDataStream socketStream(&socket); socketStream.setVersion(riOctavePlugin::qtDataStreamVersion); // Create command as a string with arguments , and send it: QString command; command += "SetActiveCellProperty " + QString::number(caseId) + " " + propertyName + " " + porosityModel; for (int i = 0; i < requestedTimeSteps.numel(); ++i) { if (i == 0) command += " "; command += QString::number(static_cast<int>(requestedTimeSteps.elem(i)) - 1); // To make the index 0-based if (i != requestedTimeSteps.numel() -1) command += " "; } QByteArray cmdBytes = command.toLatin1(); socketStream << (qint64)(cmdBytes.size()); socket.write(cmdBytes); // Write property data header dim_vector mxDims = propertyFrames.dims(); qint64 cellCount = mxDims.elem(0); qint64 timeStepCount = mxDims.elem(1); qint64 timeStepByteCount = cellCount * sizeof(double); socketStream << (qint64)(timeStepCount); socketStream << (qint64)timeStepByteCount; const double* internalData = propertyFrames.fortran_vec(); QStringList errorMessages; if (!RiaSocketDataTransfer::writeBlockDataToSocket(&socket, (const char *)internalData, timeStepByteCount*timeStepCount, errorMessages)) { for (int i = 0; i < errorMessages.size(); i++) { octave_stdout << errorMessages[i].toStdString(); } return; } QString tmp = QString("riSetActiveCellProperty : Wrote %1").arg(propertyName); if (caseId == -1) { tmp += QString(" to current case."); } else { tmp += QString(" to case with Id = %1.").arg(caseId); } octave_stdout << tmp.toStdString() << " Active Cells : " << cellCount << " Time steps : " << timeStepCount << std::endl; while(socket.bytesToWrite() && socket.state() == QAbstractSocket::ConnectedState) { // octave_stdout << "Bytes to write: " << socket.bytesToWrite() << std::endl; socket.waitForBytesWritten(riOctavePlugin::shortTimeOutMilliSecs); OCTAVE_QUIT; } //octave_stdout << " Socket write completed" << std::endl; if (socket.bytesToWrite() && socket.state() != QAbstractSocket::ConnectedState) { error("riSetActiveCellProperty : ResInsight refused to accept the data. Maybe the dimensions or porosity model is wrong"); } #ifdef WIN32 // TODO: Due to synchronization issues seen on Windows 10, it is required to do a sleep here to be able to catch disconnect // signals from the socket. No sleep causes the server to hang. Sleep(100); #endif //WIN32 return; }
void FileServerThread::run() //TODO: use mutexes { QString filename; QString ID; QFile file; QTcpSocket socket; socket.setSocketDescriptor (m_descriptor); while (!m_doQuit) { m_status = Waiting; while (!socket.bytesAvailable() && !m_doQuit) { socket.waitForReadyRead(); } if (m_doQuit) break; QString data (socket.readAll()); if (!Kapotah::TransferManager::instance()->pathForId (data).isEmpty()) { setStatus(PreparingToSend); ID = data; filename = Kapotah::TransferManager::instance()->pathForId (data); file.setFileName (filename); if (!file.open (QIODevice::ReadOnly)) { setStatus(ErrorFileNotFound); break; } socket.write ("OK"); socket.waitForBytesWritten(); emit startedTransfer (ID); setStatus(Sending); while (!file.atEnd() && !m_doQuit) { if (socket.state() != QTcpSocket::ConnectedState) { emit finishedTransfer (ID); setStatus(Finished); break; } socket.write (file.read (s_bytesPerBlock)); socket.waitForBytesWritten(); while (socket.bytesToWrite()) socket.flush(); emit transferProgress (ID, file.pos() / file.size() *100); } file.close(); if (m_doQuit) { setStatus(Canceled); emit canceledTransfer(ID); socket.disconnectFromHost(); } else { setStatus(Finished); emit finishedTransfer (ID); } socket.waitForDisconnected (); break; } else { setStatus(ErrorIDNotFound); emit transferNotFound(ID); break; } deleteLater(); } }