Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: correct read behavior #16

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/iso15118/io/connection_plain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,19 @@ ReadResult ConnectionPlain::read(uint8_t* buf, size_t len) {
assert(connection_open);

const auto read_result = ::read(fd, buf, len);
if (read_result > 0) {
size_t bytes_read = read_result;
const auto would_block = (bytes_read < len);
return {would_block, bytes_read};
}
const auto did_block = (len > 0) and (read_result != len);

if (read_result == -1) {
// handle blocking read case
if (errno == EAGAIN) {
return {true, 0};
}
if (read_result >= 0) {
return {did_block, static_cast<size_t>(read_result)};
}

// should be an error
if (errno != EAGAIN) {
// in case the error is not due to blocking, log it
logf("ConnectionPlain::read failed with error code: %d", errno);
}

return {false, 0};
return {did_block, 0};
}

void ConnectionPlain::handle_connect() {
Expand Down
21 changes: 12 additions & 9 deletions src/iso15118/session/iso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ bool read_single_sdp_packet(io::IConnection& connection, io::SdpPacket& sdp_pack

assert(sdp_packet.get_state() == PacketState::EMPTY || sdp_packet.get_state() == PacketState::HEADER_READ);

// FIXME (aw): proper handling of case, where read returns 0!
auto result = connection.read(sdp_packet.get_current_buffer_pos(), sdp_packet.get_remaining_bytes_to_read());
sdp_packet.update_read_bytes(result.bytes_read);
const auto first_try =
connection.read(sdp_packet.get_current_buffer_pos(), sdp_packet.get_remaining_bytes_to_read());

if (result.would_block) {
// need more data!
sdp_packet.update_read_bytes(first_try.bytes_read);

if (first_try.would_block) {
// need more data for at least the header
return true;
}

Expand All @@ -83,11 +84,13 @@ bool read_single_sdp_packet(io::IConnection& connection, io::SdpPacket& sdp_pack
}

// header read successfully, try to read the rest
result = connection.read(sdp_packet.get_current_buffer_pos(), sdp_packet.get_remaining_bytes_to_read());
sdp_packet.update_read_bytes(result.bytes_read);
const auto second_try =
connection.read(sdp_packet.get_current_buffer_pos(), sdp_packet.get_remaining_bytes_to_read());

sdp_packet.update_read_bytes(second_try.bytes_read);

if (result.would_block) {
// need more data!
if (second_try.would_block) {
// need more data for the rest of the packet!
return true;
}

Expand Down
Loading