gpt4 book ai didi

c++ - Qt:QSslSocket::bytesWritten() 信号发射太频繁

转载 作者:搜寻专家 更新时间:2023-10-31 01:45:57 25 4
gpt4 key购买 nike

我使用此代码通过套接字传输大文件内存使用量没有峰值:

    connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(refillSocketBuffer(qint64)));
refillSocketBuffer(128*1024);
}

void FtpRetrCommand::refillSocketBuffer(qint64 bytes)
{
if (!file->atEnd()) {
socket->write(file->read(bytes));
} else {
socket->disconnectFromHost();
}
}

这适用于 QTcpSocket,但是对于加密的 QSslSocket,bytesWritten() 信号不断发出,这导致我的函数一直写入套接字,比它通过套接字发送数据快得多,所以最终它的内存使用量达到 400 MB,操作系统将其杀死。

最佳答案

我只是在进一步挖掘后找到了答案,它实际上在文档中。看来我应该为 SSL 套接字使用 encryptedBytesWritten():

Note: Be aware of the difference between the bytesWritten() signal and the encryptedBytesWritten() signal. For a QTcpSocket, bytesWritten() will get emitted as soon as data has been written to the TCP socket. For a QSslSocket, bytesWritten() will get emitted when the data is being encrypted and encryptedBytesWritten() will get emitted as soon as data has been written to the TCP socket.

所以我需要更改这段代码:

connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(refillSocketBuffer(qint64)));

为此:

if (socket->isEncrypted()) {
connect(socket, SIGNAL(encryptedBytesWritten(qint64)), this, SLOT(refillSocketBuffer(qint64)));
} else {
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(refillSocketBuffer(qint64)));
}

关于c++ - Qt:QSslSocket::bytesWritten() 信号发射太频繁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21350676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com