lordgamez commented on code in PR #1595:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1595#discussion_r1280707520
##########
libminifi/src/io/InputStream.cpp:
##########
@@ -71,18 +67,20 @@ size_t InputStream::read(std::string &str, bool widen) {
return length_return;
}
- std::vector<std::byte> buffer(string_length);
- const auto read_return = read(buffer);
- if (read_return != string_length) {
- return read_return;
+ str.clear();
+ str.reserve(string_length);
+
+ auto bytes_to_read = string_length;
+ while (bytes_to_read > 0) {
+ std::vector<std::byte> buffer(bytes_to_read);
+ const auto read_return = read(buffer);
+ if (io::isError(read_return))
+ return read_return;
+ bytes_to_read -= read_return;
+ str.append(std::string(reinterpret_cast<const char*>(buffer.data()),
read_return));
Review Comment:
The problem with the read was that in case of the `AsioStream` there were 2
possible read operations we could use to rewrite the C sockets' read operation:
`asio::read` and `asio::basic_stream_socket::read_some`. The problem with
`asio::read` was discovered with `CSite2SiteTests` where `asio::read` was
blocking until it did not receive the number of bytes available in the buffer
(1000 in that scenario) when only less bytes were available in the read
operation:
"When calling an overload that does not accept a CompletionCondition, it is
the equivalent to calling its associated overload with a CompletionCondition of
boost::asio::transfer_all(), causing the operation to read streambuf.max_size()
bytes."
So instead `asio::basic_stream_socket::read_some` was used which is said to
be equivalent to the `::read()` on Linux. The problem with this function was
that it returned after reading the number of bytes that fit in its buffer, so
it failed on larger inputs, due to this code in the original
`InputStream::read(std::string &str, bool widen)`:
```
const auto read_return = read(buffer);
if (read_return != string_length) {
return read_return;
```
This is why the `InputStream::read` was changed as we cannot change the
asio's read function.
------------
You are right, I think we should return if the returned read is 0 with a
STREAM_ERROR. Updated in c8d0aed1f0eb559c1edc2beda019f4a11228ae26
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]