67 lines
1.2 KiB
C++
67 lines
1.2 KiB
C++
/*!
|
|
* \copyright Copyright (c) 2016-2019 Governikus GmbH & Co. KG, Germany
|
|
*/
|
|
|
|
#include "MockSocket.h"
|
|
|
|
#include <cstring>
|
|
|
|
using namespace governikus;
|
|
|
|
MockSocket::MockSocket()
|
|
: QTcpSocket()
|
|
, mReadBuffer()
|
|
, mReaderBufferPosition(0)
|
|
, mReaderBufferChunk(-1)
|
|
, mWriteBuffer()
|
|
{
|
|
QTcpSocket::open(QIODevice::ReadWrite);
|
|
}
|
|
|
|
|
|
MockSocket::~MockSocket()
|
|
{
|
|
}
|
|
|
|
|
|
qint64 MockSocket::bytesAvailable() const
|
|
{
|
|
return mReadBuffer.size() - mReaderBufferPosition;
|
|
}
|
|
|
|
|
|
qint64 MockSocket::readData(char* pDestination, qint64 pMaxSize)
|
|
{
|
|
Q_ASSERT(pMaxSize <= INT_MAX);
|
|
|
|
int chunk = static_cast<int>(pMaxSize);
|
|
if (mReaderBufferChunk > -1 && mReaderBufferChunk < chunk)
|
|
{
|
|
chunk = mReaderBufferChunk;
|
|
}
|
|
|
|
QByteArray data = mReadBuffer.mid(mReaderBufferPosition, chunk);
|
|
int length = data.length();
|
|
if (length >= 0)
|
|
{
|
|
memcpy(pDestination, data.constData(), static_cast<size_t>(length));
|
|
mReaderBufferPosition += length;
|
|
}
|
|
return length;
|
|
}
|
|
|
|
|
|
qint64 MockSocket::writeData(const char* pData, qint64 pMaxSize)
|
|
{
|
|
const auto& data = QByteArray(pData, static_cast<int>(pMaxSize));
|
|
mWriteBuffer += data;
|
|
return data.size();
|
|
}
|
|
|
|
|
|
qint64 MockSocket::write(const QByteArray& pByteArray)
|
|
{
|
|
mWriteBuffer += pByteArray;
|
|
return QIODevice::write(pByteArray);
|
|
}
|