Files
ResInsight/ApplicationLibCode/SocketInterface/RiaSocketDataTransfer.cpp
T

111 lines
3.9 KiB
C++
Raw Normal View History

2014-04-14 14:36:47 +02:00
/////////////////////////////////////////////////////////////////////////////////
//
2014-09-24 07:14:52 +02:00
// Copyright (C) Statoil ASA
// Copyright (C) Ceetron Solutions AS
//
2014-04-14 14:36:47 +02:00
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2014-04-14 14:36:47 +02:00
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
2014-04-14 14:36:47 +02:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaSocketDataTransfer.h"
//--------------------------------------------------------------------------------------------------
///
2014-04-14 14:36:47 +02:00
//--------------------------------------------------------------------------------------------------
bool RiaSocketDataTransfer::writeBlockDataToSocket( QTcpSocket* socket,
const char* data,
quint64 bytesToWrite,
QStringList& errorMessages )
2014-04-14 14:36:47 +02:00
{
quint64 bytesWritten = 0;
quint64 maxBlockSize = maximumValueCountInBlock() * sizeof( double );
2014-04-14 14:36:47 +02:00
while ( bytesWritten < bytesToWrite )
2014-04-14 14:36:47 +02:00
{
quint64 byteCountToWrite = qMin( bytesToWrite - bytesWritten, maxBlockSize );
2014-04-14 14:36:47 +02:00
qint64 actuallyBytesWritten = socket->write( data + bytesWritten, byteCountToWrite );
if ( actuallyBytesWritten < 0 )
2014-04-14 14:36:47 +02:00
{
errorMessages.push_back( "Error detected when writing data, error string from socket" );
errorMessages.push_back( socket->errorString() );
2014-04-14 14:36:47 +02:00
return false;
}
bytesWritten += actuallyBytesWritten;
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
2014-04-14 14:36:47 +02:00
//--------------------------------------------------------------------------------------------------
2020-02-12 11:43:15 +01:00
bool RiaSocketDataTransfer::readBlockDataFromSocket( QTcpSocket* socket, char* data, quint64 bytesToRead, QStringList& errorMessages )
2014-04-14 14:36:47 +02:00
{
quint64 bytesRead = 0;
quint64 maxBlockSize = maximumValueCountInBlock() * sizeof( double );
2014-04-24 07:40:29 +02:00
while ( bytesRead < bytesToRead )
2014-04-14 14:36:47 +02:00
{
if ( socket->bytesAvailable() )
2014-04-14 14:36:47 +02:00
{
quint64 byteCountToRead = bytesToRead - bytesRead;
byteCountToRead = qMin( byteCountToRead, maxBlockSize );
2014-04-14 14:36:47 +02:00
qint64 actuallyBytesRead = socket->read( data + bytesRead, byteCountToRead );
if ( actuallyBytesRead < 0 )
2014-04-14 14:36:47 +02:00
{
errorMessages.push_back( "Error detected when reading data, error string from socket" );
errorMessages.push_back( socket->errorString() );
2014-04-14 14:36:47 +02:00
return false;
}
bytesRead += actuallyBytesRead;
2014-04-24 07:40:29 +02:00
#ifdef octave_oct_h
// octave_stdout << "Byte read " << bytesRead << " of a total of "<< bytesToRead << "\n";
2014-04-24 07:40:29 +02:00
#endif
2014-04-14 14:36:47 +02:00
}
else
{
if ( !socket->waitForReadyRead() )
2014-04-14 14:36:47 +02:00
{
errorMessages.push_back( "Waited for data for %1 milli seconds." );
errorMessages.push_back( socket->errorString() );
2014-04-14 14:36:47 +02:00
return false;
}
}
2014-04-24 07:40:29 +02:00
// Allow Octave process to end a long running Octave function
#ifdef octave_oct_h
OCTAVE_QUIT;
#endif
2014-04-14 14:36:47 +02:00
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
2014-04-14 14:36:47 +02:00
//--------------------------------------------------------------------------------------------------
2014-04-24 07:40:29 +02:00
size_t RiaSocketDataTransfer::maximumValueCountInBlock()
2014-04-14 14:36:47 +02:00
{
2014-04-24 07:40:29 +02:00
return 20000;
2014-04-14 14:36:47 +02:00
}