userver: userver/engine/io/socket.hpp Source File
Loading...
Searching...
No Matches
socket.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/io/socket.hpp
4/// @brief @copybrief engine::io::Socket
5
6#include <sys/socket.h>
7
8#include <initializer_list>
9
10#include <userver/engine/deadline.hpp>
11#include <userver/engine/io/common.hpp>
12#include <userver/engine/io/exception.hpp>
13#include <userver/engine/io/fd_control_holder.hpp>
14#include <userver/engine/io/sockaddr.hpp>
15
16struct iovec;
17
18USERVER_NAMESPACE_BEGIN
19
20namespace engine::io {
21
22/// Socket type
23enum class SocketType {
24 kStream = SOCK_STREAM, ///< Stream socket (e.g. TCP)
25 kDgram = SOCK_DGRAM, ///< Datagram socket (e.g. UDP)
26
27 kTcp = kStream,
28 kUdp = kDgram,
29};
30
31/// @brief Socket representation.
32///
33/// It is not thread-safe to concurrently read from socket. It is not
34/// thread-safe to concurrently write to socket. However it is safe to
35/// concurrently read and write into socket:
36/// @snippet src/engine/io/socket_test.cpp send self concurrent
37class [[nodiscard]] Socket final : public RwBase {
38 public:
40 size_t bytes_received{0};
41 Sockaddr src_addr;
42 };
43
44 /// Constructs an invalid socket.
45 Socket() = default;
46
47 /// Constructs a socket for the address domain of specified type.
49
50 /// @brief Adopts an existing socket for specified address domain.
51 /// @note File descriptor will be silently forced to nonblocking mode.
52 explicit Socket(int fd, AddrDomain domain = AddrDomain::kUnspecified);
53
54 /// Whether the socket is valid.
55 explicit operator bool() const { return IsValid(); }
56
57 /// Whether the socket is valid.
58 bool IsValid() const override;
59
60 /// @brief Connects the socket to a specified endpoint.
61 /// @note Sockaddr domain must match the socket's domain.
62 void Connect(const Sockaddr&, Deadline);
63
64 /// @brief Binds the socket to the specified endpoint.
65 /// @note Sockaddr domain must match the socket's domain.
66 void Bind(const Sockaddr&);
67
68 /// Starts listening for connections on a specified socket (must be bound).
69 void Listen(int backlog = SOMAXCONN);
70
71 /// Suspends current task until the socket has data available.
72 [[nodiscard]] bool WaitReadable(Deadline) override;
73
74 /// Suspends current task until the socket can accept more data.
75 [[nodiscard]] bool WaitWriteable(Deadline) override;
76
77 /// @brief Receives at least one byte from the socket.
78 /// @returns 0 if connection is closed on one side and no data could be
79 /// received any more, received bytes count otherwise.
80 [[nodiscard]] size_t RecvSome(void* buf, size_t len, Deadline deadline);
81
82 /// @brief Receives exactly len bytes from the socket.
83 /// @note Can return less than len if socket is closed by peer.
84 [[nodiscard]] size_t RecvAll(void* buf, size_t len, Deadline deadline);
85
86 /// @brief Receives up to len bytes from the stream
87 /// @returns
88 /// - nullopt on data absence
89 /// - optional{0} if socket is closed by peer.
90 /// - optional{data_bytes_available} otherwise,
91 /// 1 <= data_bytes_available <= len
92 [[nodiscard]] std::optional<size_t> RecvNoblock(void* buf, size_t len);
93
94 /// @brief Sends a buffer vector to the socket.
95 /// @note Can return less than len if socket is closed by peer.
96 /// @snippet src/engine/io/socket_test.cpp send vector data in socket
99
100 [[nodiscard]] size_t WriteAll(std::initializer_list<IoData> list,
101 Deadline deadline) override {
102 return SendAll(list, deadline);
103 }
104
105 /// @brief Sends exactly list_size IoData to the socket.
106 /// @note Can return less than len if socket is closed by peer.
107 [[nodiscard]] size_t SendAll(const IoData* list, std::size_t list_size,
109
110 /// @brief Sends exactly list_size iovec to the socket.
111 /// @note Can return less than len if socket is closed by peer.
112 [[nodiscard]] size_t SendAll(const struct iovec* list, std::size_t list_size,
114
115 /// @brief Sends exactly len bytes to the socket.
116 /// @note Can return less than len if socket is closed by peer.
117 [[nodiscard]] size_t SendAll(const void* buf, size_t len, Deadline deadline);
118
119 /// @brief Accepts a connection from a listening socket.
120 /// @see engine::io::Listen
121 [[nodiscard]] Socket Accept(Deadline);
122
123 /// @brief Receives at least one byte from the socket, returning source
124 /// address.
125 /// @returns 0 in bytes_sent if connection is closed on one side and no data
126 /// could be received any more, received bytes count otherwise + source
127 /// address.
128 [[nodiscard]] RecvFromResult RecvSomeFrom(void* buf, size_t len,
129 Deadline deadline);
130
131 /// @brief Sends exactly len bytes to the specified address via the socket.
132 /// @note Can return less than len in bytes_sent if socket is closed by peer.
133 /// @note Sockaddr domain must match the socket's domain.
134 /// @note Not for SocketType::kStream connections, see `man sendto`.
135 [[nodiscard]] size_t SendAllTo(const Sockaddr& dest_addr, const void* buf,
137
138 /// File descriptor corresponding to this socket.
139 int Fd() const;
140
141 /// Address of a remote peer.
142 const Sockaddr& Getpeername();
143
144 /// Local socket address.
145 const Sockaddr& Getsockname();
146
147 /// Releases file descriptor and invalidates the socket.
148 [[nodiscard]] int Release() && noexcept;
149
150 /// @brief Closes and invalidates the socket.
151 /// @warning You should not call Close with pending I/O. This may work okay
152 /// sometimes but it's loosely predictable.
153 void Close();
154
155 /// Retrieves a socket option.
156 int GetOption(int layer, int optname) const;
157
158 /// Sets a socket option.
159 void SetOption(int layer, int optname, int optval);
160
161 /// @brief Receives at least one byte from the socket.
162 /// @returns 0 if connection is closed on one side and no data could be
163 /// received any more, received bytes count otherwise.
164 [[nodiscard]] size_t ReadSome(void* buf, size_t len,
165 Deadline deadline) override {
166 return RecvSome(buf, len, deadline);
167 }
168
169 /// @brief Receives exactly len bytes from the socket.
170 /// @note Can return less than len if socket is closed by peer.
171 [[nodiscard]] size_t ReadAll(void* buf, size_t len,
172 Deadline deadline) override {
173 return RecvAll(buf, len, deadline);
174 }
175
176 /// @brief Receives up to len bytes from the stream
177 /// @returns
178 /// - nullopt on data absence
179 /// - optional{0} if socket is closed by peer.
180 /// - optional{data_bytes_available} otherwise,
181 /// 1 <= data_bytes_available <= len
182 [[nodiscard]] std::optional<size_t> ReadNoblock(void* buf,
183 size_t len) override {
184 return RecvNoblock(buf, len);
185 }
186
187 /// @brief Writes exactly len bytes to the socket.
188 /// @note Can return less than len if socket is closed by peer.
189 [[nodiscard]] size_t WriteAll(const void* buf, size_t len,
190 Deadline deadline) override {
191 return SendAll(buf, len, deadline);
192 }
193
194 private:
196
197 impl::FdControlHolder fd_control_;
198 Sockaddr peername_;
199 Sockaddr sockname_;
200};
201
202} // namespace engine::io
203
204USERVER_NAMESPACE_END