Skip to content

Commit

Permalink
pw_stream: Add ready method to socket stream
Browse files Browse the repository at this point in the history
For now the connection is not ready when:
* Read returned a wrong value (0 bytes = remote peer closed
  the connection). Close() is called.
* Manually released or closed

Change-Id: I9306116f01233a48e5d61e8054e0047ffce19b7c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/253772
Pigweed-Auto-Submit: Armando Montanez <[email protected]>
Docs-Not-Needed: Ronan Chauvin <[email protected]>
Reviewed-by: Armando Montanez <[email protected]>
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Ted Pudlik <[email protected]>
Commit-Queue: Ted Pudlik <[email protected]>
  • Loading branch information
Ronan Chauvin authored and CQ Bot Account committed Jan 6, 2025
1 parent 9cb5a89 commit e7380e5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pw_stream/public/pw_stream/socket_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class SocketStream : public NonSeekableReaderWriter {
const void* optval,
unsigned int optlen);

// Get the connection ready state (true if ready, false otherwise).
bool IsReady();

// Close the socket stream and release all resources
void Close();

Expand Down
5 changes: 5 additions & 0 deletions pw_stream/socket_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ int SocketStream::SetSockOpt(int level,
return setsockopt(ownership.fd(), level, optname, optval, optlen);
}

bool SocketStream::IsReady() {
std::lock_guard lock(connection_mutex_);
return ready_;
}

void SocketStream::Close() {
ConnectionOwnership ownership(this);
{
Expand Down
2 changes: 2 additions & 0 deletions pw_stream/socket_stream_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ TEST(SocketStreamTest, ConnectSpecificPort) {

SocketStream client;
EXPECT_EQ(client.Connect("localhost", server.port()), OkStatus());
EXPECT_TRUE(client.IsReady());

accept_thread.join();
EXPECT_EQ(server_stream.status(), OkStatus());

server_stream.value().Close();
server.Close();
client.Close();
EXPECT_FALSE(client.IsReady());
}

// Helper function to test exchanging data on a pair of sockets.
Expand Down

0 comments on commit e7380e5

Please sign in to comment.