コード例 #1
0
ファイル: ServerPanel.cpp プロジェクト: bolvarak/ServerPanel
/**
 * @paragraph This method reads the socket response
 * @brief ServerPanel::ReadResponse
 * @return void
 */
void ServerPanel::ReadResponse() {
    // Create the data stream
    QDataStream qdsServerPanel(this->mClient);
    // Check the block size
    if (this->mBlockSize == 0) {
        // Make sure we have a valid amount of daya
        if (this->mClient->bytesAvailable() < (int) sizeof(quint16)) {
            // We're done
            return;
        }
        // Read the data
        qdsServerPanel >> this->mBlockSize;
    }
    // See if we have read all of the data
    if (this->mClient->bytesAvailable() < this->mBlockSize) {
        // We're done
        return;
    }
    // Set a response placeholder
    QString sJson;
    // Read the stream
    qdsServerPanel >> sJson;
    // Check for data
    if (sJson.isEmpty()) {
        // Rerun the read
        QTimer::singleShot(0, this, SLOT(ReadResponse()));
        // We're done
        return;
    }
    // Store the json into the system
    this->mJsonResponse = sJson;
    // Disconnect from the host
    this->mClient->disconnectFromHost();
}
コード例 #2
0
/**
 * @paragraph This method slot handles the reading and responding of the server
 * @brief ServerPanel::startRead()
 * @return void
 */
void ServerPanelService::startRead() {
    // Create the data stream
    QDataStream qdsServerPanel(this->mClient);
    // Check the block size
    if (this->mBlockSize == 0) {
        // Make sure we have a valid amount of daya
        if (this->mClient->bytesAvailable() < (int) sizeof(quint16)) {
            // We're done
            return;
        }
        // Read the data
        qdsServerPanel >> this->mBlockSize;
    }
    // See if we have read all of the data
    if (this->mClient->bytesAvailable() < this->mBlockSize) {
        // We're done
        return;
    }
    // Set a response placeholder
    QString sJson;
    // Read the stream
    qdsServerPanel >> sJson;
    // Check for data
    if (sJson.isEmpty()) {
        // Rerun the read
        QTimer::singleShot(0, this, SLOT(startRead()));
        // We're done
        return;
    }
    // Create an empty byte array
    QByteArray qbaOutput;
    // Handle the request
    QByteArray qbaClientResponse = ServerPanel::Instance()->HandleRequest(sJson);
    // Create a data stream
    QDataStream qdsResponse(&qbaOutput, QIODevice::WriteOnly);
    // Send a 0 response
    qdsResponse << (quint16) 0;
    // Send the JSON response
    qdsResponse << QString(qbaClientResponse);
    // Reset the response
    qdsResponse.device()->seek(0);
    // Send the block size
    qdsResponse << (quint16) (qbaOutput.size() - sizeof(quint16));
    // Write the response
    this->mClient->write(qbaOutput);
    // Disconnect
    this->mClient->waitForDisconnected();
}