Skip to content

Commit

Permalink
pw_stream: SocketStream reports out of range status on connection closed
Browse files Browse the repository at this point in the history
This allows clients of SocketStream to detect when the connection
has been terminated.

Change-Id: I83c93b72a6ba4b111f501481a880012c869f1c5b
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/90900
Reviewed-by: Ted Pudlik <[email protected]>
Reviewed-by: Armando Montanez <[email protected]>
Commit-Queue: Erik Gilling <[email protected]>
  • Loading branch information
konkers authored and CQ Bot Account committed Apr 27, 2022
1 parent 522834d commit 33da116
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pw_stream/socket_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ 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 as successful
// zero length write to inform the caller.
// An EPIPE indicates that the connection is close. Return an OutOfRange
// error.
return Status::OutOfRange();
}

Expand All @@ -137,13 +137,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 (bytes_rcvd == 0) {
// Remote peer has closed the connection.
Close();
return StatusWithSize::OutOfRange();
} else if (bytes_rcvd < 0) {
return StatusWithSize::Unknown();
}

// TODO(b/229988958): Return Status::OutOfRange() on connection closed
// (bytes_rcvd == 0) This is a breaking API change and needs to be
// managed with a soft transition
return StatusWithSize(bytes_rcvd);
}

Expand Down

0 comments on commit 33da116

Please sign in to comment.