简体   繁体   English

Qt QIODevice :: write / QTcpSocket ::写入和写入的字节

[英]Qt QIODevice::write / QTcpSocket::write and bytes written

We are quite confused about the behavior of QIODevice::write in general and the QTcpSocket implementation specifically. 我们对QIODevice::write的行为以及具体的QTcpSocket实现非常困惑。 There is a similar question already, but the answer is not really satisfactory. 已经有类似的问题 ,但答案并不令人满意。 The main confusion stems from the there mentioned bytesWritten signal respectively the waitForBytesWritten method. 主要的混淆源于那里提到的bytesWritten信号和waitForBytesWritten方法。 Those two seem to indicate the bytes that were written from the buffer employed by the QIODevice to the actual underlying device (there must be such buffer, otherwise the method would not make much sense). 这两个似乎表明从QIODevice使用的缓冲区写入到实际底层设备的字节(必须有这样的缓冲区,否则该方法没有多大意义)。 The question then is though, if the number returned by QIODevice::write corresponds with this number, or if in that case it indicates the number of bytes that were stored in the internal buffer , not the bytes written to the underlying device . 那么问题是,如果QIODevice::write返回的数字与此数字相对应,或者在这种情况下,它表示存储内部缓冲区中的字节数,而不是写入底层设备的字节数。 If the number returned would indicate the bytes written to the internal buffer, we would need to employ a pattern like the following to ensure all our data is written: 如果返回的数字表示写入内部缓冲区的字节,我们需要使用如下的模式来确保写入所有数据:

void writeAll(QIODevice& device, const QByteArray& data) {
   int written = 0;
   do {
     written = device.write(data.constData() + written, data.size() - written);
   } while(written < data.size());
}

However, this will insert duplicate data if the return value of QIODevice::write corresponds with the meaning of the bytesWritten signal. 但是,如果QIODevice::write的返回值与bytesWritten信号的含义相对应,则会插入重复数据。 The documentation is very confusing about this, as in both methods the word device is used, even though it seems logical and the general understanding, that one actually indicates written to buffer, and not device. 文档对此非常困惑,因为在两种方法中都使用了“ 设备”这个词,即使它似乎是逻辑和一般的理解,一个实际上指示写入缓冲区而不是设备。

So to summarize, the question is: Is the number returned bye QIODevice::write the number of bytes written to the underlying device, and hence its save to call QIODevice::write without checking the returned number of bytes, as everything is stored in the internal buffer. 总而言之,问题是: QIODevice::write返回的数字是否写入写入底层设备的字节数,因此保存为调用QIODevice::write 而不检查返回的字节数,因为所有内容都存储在内部缓冲区。 Or does it indicate how much bytes it could store internally and a pattern like the above writeAll has to be employed to safely write all data to the device? 或者它是否表明它可以在内部存储多少字节,并且必须采用类似上述writeAll的模式来安全地将所有数据写入设备?

(UPDATE: Looking at the source, the QTcpSocket::write implementation actually will never return less bytes than one wanted to write, so the writeAll above is not needed. However, that is specific to the socket and this Qt version, the documentation is still confusing...) (更新:看一下源代码, QTcpSocket::write实现实际上永远不会返回比想要写的更少的字节,所以上面的writeAll不需要。但是,这是特定于套接字和这个Qt版本,文档是仍然令人困惑...)

QTcpSocket is a buffered QAbstractSocket . QTcpSocket是一个缓冲的QAbstractSocket An internal buffer is allocated inside QAbstractSocket , and data is copied in that buffer. QAbstractSocket内部分配内部缓冲区,并将数据复制到该缓冲区中。 The return value of write is the size of the data passed to write() . write的返回值是传递给write()的数据的大小。

waitForBytesWritten waits until the data in the internal buffer of QAbstractSocket is written to the native socket. waitForBytesWritten等待,直到QAbstractSocket的内部缓冲区中的数据写入本机套接字。

That previous question answers your question, as does the QIODevice::write(const char * data, qint64 maxSize) documentation: 上一个问题回答了您的问题, QIODevice::write(const char * data, qint64 maxSize)文档也是如此:

Writes at most maxSize bytes of data from data to the device. 最多 maxSize字节的数据从数据写入设备。 Returns the number of bytes that were actually written, or -1 if an error occurred. 返回实际写入的字节数,如果发生错误,则返回-1。

This can (and will in real life) return less than what you requested, and it's up to you to call write again with the remainder. 这可以(并且将在现实生活中)返回少于您请求的内容,并且您可以使用其余内容再次调用write

As for waitForBytesWritten : 至于waitForBytesWritten

For buffered devices , this function waits until a payload of buffered written data has been written to the device... 对于缓冲设备 ,此功能等待,直到已将缓冲写入数据的有效负载写入设备...

It applies only to buffered devices. 它仅适用于缓冲设备。 Not all devices are buffered. 并非所有设备都是缓冲的。 If they are, and you wrote less than what the buffer can hold, write can return successfully before the device has finished sending all the data. 如果它们是,并且您写入的内容少于缓冲区可以容纳的内容,则write可以在设备完成所有数据的发送之前成功返回。

Devices are not necessarily buffered. 设备不一定是缓冲的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM