aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Lipsitz <elipsitz@google.com>2023-04-11 21:21:27 +0000
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-04-11 21:21:27 +0000
commitbff0a9fe009d99333a977243e267baec6cfadc4f (patch)
tree9fd74247a54beed865523a34f41121d0132bb1f6
parent5fc2e6c663e123eb796e7f7be6ec329b98c94398 (diff)
downloadpigweed-bff0a9fe009d99333a977243e267baec6cfadc4f.tar.gz
pw_stream: Remove deprecated SocketStream::Serve
This method has been deprecated in favor of ServerSocketStream. This commit cleans up the method and the internal state in SocketStream that was required to support it. Fixes: b/271323032 Change-Id: Ia5571ff43b4158ebf2c93c16a0511c74af94e1d2 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/138254 Reviewed-by: Keir Mierle <keir@google.com> Commit-Queue: Eli Lipsitz <elipsitz@google.com>
-rw-r--r--pw_stream/public/pw_stream/socket_stream.h19
-rw-r--r--pw_stream/socket_stream.cc55
2 files changed, 1 insertions, 73 deletions
diff --git a/pw_stream/public/pw_stream/socket_stream.h b/pw_stream/public/pw_stream/socket_stream.h
index 0092fc808..4666d91ee 100644
--- a/pw_stream/public/pw_stream/socket_stream.h
+++ b/pw_stream/public/pw_stream/socket_stream.h
@@ -29,20 +29,12 @@ class SocketStream : public NonSeekableReaderWriter {
// SocketStream objects are moveable but not copyable.
SocketStream& operator=(SocketStream&& other) {
- listen_port_ = other.listen_port_;
- socket_fd_ = other.socket_fd_;
- other.socket_fd_ = kInvalidFd;
connection_fd_ = other.connection_fd_;
other.connection_fd_ = kInvalidFd;
- sockaddr_client_ = other.sockaddr_client_;
return *this;
}
SocketStream(SocketStream&& other) noexcept
- : listen_port_(other.listen_port_),
- socket_fd_(other.socket_fd_),
- connection_fd_(other.connection_fd_),
- sockaddr_client_(other.sockaddr_client_) {
- other.socket_fd_ = kInvalidFd;
+ : connection_fd_(other.connection_fd_) {
other.connection_fd_ = kInvalidFd;
}
SocketStream(const SocketStream&) = delete;
@@ -50,12 +42,6 @@ class SocketStream : public NonSeekableReaderWriter {
~SocketStream() override { Close(); }
- // Listen to the port and return after a client is connected
- //
- // DEPRECATED: Use the ServerSocket class instead.
- // TODO(b/271323032): Remove when this method is no longer used.
- Status Serve(uint16_t port);
-
// Connect to a local or remote endpoint. Host may be either an IPv4 or IPv6
// address. If host is nullptr then the IPv4 localhost address is used
// instead.
@@ -80,10 +66,7 @@ class SocketStream : public NonSeekableReaderWriter {
StatusWithSize DoRead(ByteSpan dest) override;
- uint16_t listen_port_ = 0;
- int socket_fd_ = kInvalidFd;
int connection_fd_ = kInvalidFd;
- struct sockaddr_in sockaddr_client_ = {};
};
/// `ServerSocket` wraps a POSIX-style server socket, producing a `SocketStream`
diff --git a/pw_stream/socket_stream.cc b/pw_stream/socket_stream.cc
index b5b33e1dc..103629bca 100644
--- a/pw_stream/socket_stream.cc
+++ b/pw_stream/socket_stream.cc
@@ -50,56 +50,6 @@ void ConfigureSocket([[maybe_unused]] int socket) {
// TODO(b/240982565): Implement SocketStream for Windows.
-// Listen to the port and return after a client is connected
-Status SocketStream::Serve(uint16_t port) {
- listen_port_ = port;
- socket_fd_ = socket(AF_INET, SOCK_STREAM, 0);
- if (socket_fd_ == kInvalidFd) {
- PW_LOG_ERROR("Failed to create socket: %s", std::strerror(errno));
- return Status::Unknown();
- }
-
- struct sockaddr_in addr = {};
- addr.sin_family = AF_INET;
- addr.sin_port = htons(listen_port_);
- addr.sin_addr.s_addr = INADDR_ANY;
-
- // Configure the socket to allow reusing the address. Closing a socket does
- // not immediately close it. Instead, the socket waits for some period of time
- // before it is actually closed. Setting SO_REUSEADDR allows this socket to
- // bind to an address that may still be in use by a recently closed socket.
- // Without this option, running a program multiple times in series may fail
- // unexpectedly.
- constexpr int value = 1;
-
- if (setsockopt(socket_fd_, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int)) <
- 0) {
- PW_LOG_WARN("Failed to set SO_REUSEADDR: %s", std::strerror(errno));
- }
-
- if (bind(socket_fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
- PW_LOG_ERROR("Failed to bind socket to localhost:%hu: %s",
- listen_port_,
- std::strerror(errno));
- return Status::Unknown();
- }
-
- if (listen(socket_fd_, kServerBacklogLength) < 0) {
- PW_LOG_ERROR("Failed to listen to socket: %s", std::strerror(errno));
- return Status::Unknown();
- }
-
- socklen_t len = sizeof(sockaddr_client_);
-
- connection_fd_ =
- accept(socket_fd_, reinterpret_cast<sockaddr*>(&sockaddr_client_), &len);
- if (connection_fd_ < 0) {
- return Status::Unknown();
- }
- ConfigureSocket(connection_fd_);
- return OkStatus();
-}
-
Status SocketStream::SocketStream::Connect(const char* host, uint16_t port) {
if (host == nullptr || std::strcmp(host, "localhost") == 0) {
host = kLocalhostAddress;
@@ -135,11 +85,6 @@ Status SocketStream::SocketStream::Connect(const char* host, uint16_t port) {
}
void SocketStream::Close() {
- if (socket_fd_ != kInvalidFd) {
- close(socket_fd_);
- socket_fd_ = kInvalidFd;
- }
-
if (connection_fd_ != kInvalidFd) {
close(connection_fd_);
connection_fd_ = kInvalidFd;