当前位置: 首页>>代码示例>>C++>>正文


C++ QTcpSocket::setReadBufferSize方法代码示例

本文整理汇总了C++中QTcpSocket::setReadBufferSize方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpSocket::setReadBufferSize方法的具体用法?C++ QTcpSocket::setReadBufferSize怎么用?C++ QTcpSocket::setReadBufferSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QTcpSocket的用法示例。


在下文中一共展示了QTcpSocket::setReadBufferSize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: handleNewConnection

void HttpSocket::handleNewConnection()
{
    DBUG;
    while (hasPendingConnections()) {
        QTcpSocket *socket = nextPendingConnection();

        // prevent clients from sending too much data
        socket->setReadBufferSize(constMaxBuffer);

        static const QLatin1String constIpV6Prefix("::ffff:");

        QString peer=socket->peerAddress().toString();
        QString ifaceAddress=serverAddress().toString();
        const bool hostOk=peer==ifaceAddress || peer==mpdAddr || peer==(constIpV6Prefix+mpdAddr) ||
                          peer==QLatin1String("127.0.0.1") || peer==(constIpV6Prefix+QLatin1String("127.0.0.1"));

        DBUG << "peer:" << peer << "mpd:" << mpdAddr << "iface:" << ifaceAddress << "ok:" << hostOk;
        if (!hostOk) {
            sendErrorResponse(socket, 400);
            socket->close();
            DBUG << "Not from valid host";
            return;
        }

        connect(socket, SIGNAL(readyRead()), this, SLOT(readClient()));
        connect(socket, SIGNAL(disconnected()), this, SLOT(discardClient()));
    }
}
开发者ID:CDrummond,项目名称:cantata,代码行数:28,代码来源:httpsocket.cpp

示例2: next

/* Function that is called whenever there is data ready to be processed.
 * This function should block until all data for the chunk has been read from
 * the device.
 */
void StreamChunker::next(QIODevice* device)
{
    // Note:
    // The next() function is slowed down initially by allocating the
    // data chunks.
    // It is probably best therefore, when running the benchmark, to make sure
    // all chunks are allocated before the timing starts.

    QTcpSocket* socket = static_cast<QTcpSocket*>(device);
    quint64 totalBytesRead = 0;
    quint64 bytesRead = 0;

    // Header values.
    quint32 packetSize = 0;
    quint32 packetCounter = 0;
    quint32 packetTime = 0;
    qint32 numPackets = 0;
    qint32 reportInterval = 0;
    size_t headerSize = 3*sizeof(quint32) + 2*sizeof(qint32);

    // Read header values.
    while (isActive() && totalBytesRead != headerSize)
    {
        while (socket->bytesAvailable() < (qint64)(headerSize)) {
            socket->waitForReadyRead(-1);
        }
        bytesRead = socket->read((char*)&packetSize, (qint64)sizeof(quint32));
        totalBytesRead+=bytesRead;
        bytesRead = socket->read((char*)&packetCounter, (qint64)sizeof(quint32));
        totalBytesRead+=bytesRead;
        bytesRead = socket->read((char*)&packetTime, (qint64)sizeof(quint32));
        totalBytesRead+=bytesRead;
        bytesRead = socket->read((char*)&numPackets, (qint64)sizeof(qint32));
        totalBytesRead+=bytesRead;
        bytesRead = socket->read((char*)&reportInterval, (qint64)sizeof(qint32));
        totalBytesRead+=bytesRead;
    }

    //
    // Writable chunks are obtained using the getDataStorage() method
    // using the the following priority:
    //
    // 1) A Pre-allocated chunk in the buffer that has already been served
    //    and has been marked for reuse.
    // 2) Allocating a new chunk in the buffer if space allows.
    // 3) Overwriting the oldest chunk in the buffer that matches the space
    //    requirements.
    //
    // If none of these conditions can be met, an invalid chunk is returned.
    //

    // If there are no usable chunks, i.e. the buffer is full we will
    // be overwriting chunks.
#if 1
    if (numUsableChunks(packetSize) == 0) {
        ++totalOverwriteCounter_;
        ++intervalOverwriteCounter_;
    }
#endif

    // Ask the data manager for a writable data chunk and get its data pointer.
    pelican::WritableData chunk = getDataStorage(packetSize);

    // Check chunk->isValid() before proceeding!
    if (!chunk.isValid()) {
        QString error = "StreamChunker::next(): Unable to get a valid chunk. ";
        if (packetSize > maxChunkSize()) {
            error += QString(
                    "\n== The requested packet size is greater than the maximum"
                    "\n== chunk size allowed by the buffer.");
        }
        throw error;
    }

    // Extract the data pointer from the chunk
    char* chunkPtr = (char*)chunk.ptr();

    // Write the header into the chunk.
    quint32* header1 = reinterpret_cast<quint32*>(chunkPtr);
    header1[0] = packetSize;
    header1[1] = packetCounter;
    header1[2] = packetTime;
    qint32* header2 = reinterpret_cast<qint32*>(chunkPtr+3*sizeof(quint32));
    header2[0] = numPackets;
    header2[1] = reportInterval;

    // NOTE Setting the read buffer size may have an impact on performance.
#if 0
    socket->setReadBufferSize(2*1024*1024);
#endif

    // NOTE The minimum read size has an impact on performance and has not
    // been optimised.
    quint64 minReadSize = 1024;//2*1024*1024; // bytes

    // Read data block directly into the chunk.
//.........这里部分代码省略.........
开发者ID:Error323,项目名称:pelican,代码行数:101,代码来源:StreamChunker.cpp


注:本文中的QTcpSocket::setReadBufferSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。