18#include "pw_result/result.h"
19#include "pw_span/span.h"
20#include "pw_stream/stream.h"
21#include "pw_sync/lock_annotations.h"
22#include "pw_sync/mutex.h"
36 explicit SocketStream(
int connection_fd) : connection_fd_(connection_fd) {
44 MoveFrom(std::move(other));
56 Status Connect(
const char* host, uint16_t port);
59 int SetSockOpt(
int level,
71 static constexpr int kInvalidFd = -1;
73 class ConnectionOwnership {
75 explicit ConnectionOwnership(
SocketStream* socket_stream)
76 : socket_stream_(socket_stream) {
77 fd_ = socket_stream_->TakeConnection();
78 std::lock_guard lock(socket_stream_->connection_mutex_);
79 pipe_r_fd_ = socket_stream->connection_pipe_r_fd_;
82 ~ConnectionOwnership() { socket_stream_->ReleaseConnection(); }
84 int fd() {
return fd_; }
86 int pipe_r_fd() {
return pipe_r_fd_; }
103 int TakeConnection();
104 int TakeConnectionWithLockHeld()
109 void ReleaseConnection();
110 void ReleaseConnectionWithLockHeld()
115 std::lock_guard lock(connection_mutex_);
116 std::lock_guard other_lock(other.connection_mutex_);
118 connection_own_count_ = other.connection_own_count_;
119 other.connection_own_count_ = 0;
120 ready_ = other.ready_;
121 other.ready_ =
false;
122 connection_fd_ = other.connection_fd_;
123 other.connection_fd_ = kInvalidFd;
124 connection_pipe_r_fd_ = other.connection_pipe_r_fd_;
125 other.connection_pipe_r_fd_ = kInvalidFd;
126 connection_pipe_w_fd_ = other.connection_pipe_w_fd_;
127 other.connection_pipe_w_fd_ = kInvalidFd;
131 int connection_own_count_
PW_GUARDED_BY(connection_mutex_) = 0;
133 int connection_fd_
PW_GUARDED_BY(connection_mutex_) = kInvalidFd;
134 int connection_pipe_r_fd_
PW_GUARDED_BY(connection_mutex_) = kInvalidFd;
135 int connection_pipe_w_fd_
PW_GUARDED_BY(connection_mutex_) = kInvalidFd;
154 Status Listen(uint16_t port = 0);
164 uint16_t port()
const {
return port_; }
167 static constexpr int kInvalidFd = -1;
169 class SocketOwnership {
172 : server_socket_(server_socket) {
173 fd_ = server_socket_->TakeSocket();
174 std::lock_guard lock(server_socket->socket_mutex_);
175 pipe_r_fd_ = server_socket->socket_pipe_r_fd_;
178 ~SocketOwnership() { server_socket_->ReleaseSocket(); }
180 int fd() {
return fd_; }
182 int pipe_r_fd() {
return pipe_r_fd_; }
200 void ReleaseSocket();
208 int socket_pipe_r_fd_
PW_GUARDED_BY(socket_mutex_) = kInvalidFd;
209 int socket_pipe_w_fd_
PW_GUARDED_BY(socket_mutex_) = kInvalidFd;
Definition: status_with_size.h:51
Definition: span_impl.h:235
Definition: socket_stream.h:143
Definition: socket_stream.h:32
StatusWithSize DoRead(ByteSpan dest) override
Virtual Read() function implemented by derived classes.
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:146