Skip to content

Commit

Permalink
pw_stream: Support SocketStream read timeouts
Browse files Browse the repository at this point in the history
Adds handling for errors produced by socket read timeouts when
SO_RCVTIMEO is configured to be nonzero.

Change-Id: I21da9ee986014b1775539c749d5e41dbc69a8ad2
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/92821
Pigweed-Auto-Submit: Armando Montanez <[email protected]>
Reviewed-by: Erik Gilling <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
  • Loading branch information
armandomontanez authored and CQ Bot Account committed Apr 29, 2022
1 parent 04824b6 commit 6fff19d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pw_stream/socket_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Status SocketStream::DoWrite(std::span<const std::byte> data) {

if (bytes_sent < 0 || static_cast<size_t>(bytes_sent) != data.size()) {
if (errno == EPIPE) {
// An EPIPE indicates that the connection is close. Return an OutOfRange
// An EPIPE indicates that the connection is closed. Return an OutOfRange
// error.
return Status::OutOfRange();
}
Expand All @@ -138,6 +138,13 @@ Status SocketStream::DoWrite(std::span<const std::byte> data) {
StatusWithSize SocketStream::DoRead(ByteSpan dest) {
ssize_t bytes_rcvd = recv(connection_fd_, dest.data(), dest.size_bytes(), 0);
if (bytes_rcvd == 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Socket timed out when trying to read.
// This should only occur if SO_RCVTIMEO was configured to be nonzero, or
// if the socket was opened with the O_NONBLOCK flag to prevent any
// blocking when performing reads or writes.
return StatusWithSize::ResourceExhausted();
}
// Remote peer has closed the connection.
Close();
return StatusWithSize::OutOfRange();
Expand Down

0 comments on commit 6fff19d

Please sign in to comment.