libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
tcp_socket.hpp
1 #pragma once
2 
3 #include "fly/net/socket/concepts.hpp"
4 #include "fly/net/socket/detail/base_socket.hpp"
5 #include "fly/net/socket/types.hpp"
6 
7 #include <cstddef>
8 #include <functional>
9 #include <memory>
10 #include <optional>
11 #include <string>
12 
13 namespace fly::net {
14 
15 class NetworkConfig;
16 class SocketService;
17 
18 template <IPEndpoint EndpointType>
19 class ListenSocket;
20 
27 template <IPEndpoint EndpointType>
28 class TcpSocket :
29  public detail::BaseSocket<EndpointType>,
30  public std::enable_shared_from_this<TcpSocket<EndpointType>>
31 {
33 
34  using ConnectCompletion = std::function<void(ConnectedState)>;
35  using SendCompletion = std::function<void(std::size_t)>;
36  using ReceiveCompletion = std::function<void(std::string)>;
37 
38 public:
44  explicit TcpSocket(std::shared_ptr<NetworkConfig> config) noexcept;
45 
52  TcpSocket(std::shared_ptr<NetworkConfig> config, IOMode mode) noexcept;
53 
59  TcpSocket(TcpSocket &&socket) noexcept;
60 
68  TcpSocket &operator=(TcpSocket &&socket) noexcept;
69 
75  std::optional<EndpointType> remote_endpoint() const;
76 
86  ConnectedState connect(const EndpointType &endpoint);
87 
98  ConnectedState connect(std::string_view hostname, port_type port);
99 
116  ConnectedState connect_async(const EndpointType &endpoint, ConnectCompletion &&callback);
117 
135  ConnectedState
136  connect_async(std::string_view hostname, port_type port, ConnectCompletion &&callback);
137 
144  ConnectedState finish_connect();
145 
149  bool is_connecting() const;
150 
154  bool is_connected() const;
155 
164  std::size_t send(std::string_view message);
165 
180  bool send_async(std::string_view message, SendCompletion &&callback);
181 
188  std::string receive();
189 
202  bool receive_async(ReceiveCompletion &&callback);
203 
204  using BaseSocket::close;
205  using BaseSocket::handle;
207  using BaseSocket::is_open;
208 
209 private:
211  friend SocketService;
212 
221  static std::shared_ptr<TcpSocket> create_socket(
222  const std::shared_ptr<SocketService> &service,
223  std::shared_ptr<NetworkConfig> config);
224 
235  static std::shared_ptr<TcpSocket> create_socket(
236  const std::shared_ptr<SocketService> &service,
237  std::shared_ptr<NetworkConfig> config,
238  socket_type handle);
239 
247  TcpSocket(
248  const std::shared_ptr<SocketService> &service,
249  std::shared_ptr<NetworkConfig> config) noexcept;
250 
259  TcpSocket(std::shared_ptr<NetworkConfig> config, socket_type handle, IOMode mode) noexcept;
260 
269  TcpSocket(
270  const std::shared_ptr<SocketService> &service,
271  std::shared_ptr<NetworkConfig> config,
272  socket_type handle) noexcept;
273 
274  TcpSocket(const TcpSocket &) = delete;
275  TcpSocket &operator=(const TcpSocket &) = delete;
276 
289  void ready_to_send(
290  std::string_view message,
291  SendCompletion &&callback,
292  std::size_t bytes_sent,
293  std::size_t total_bytes);
294 
305  void ready_to_receive(ReceiveCompletion &&callback, std::string received);
306 
309 
310  std::atomic<ConnectedState> m_connected_state {ConnectedState::Disconnected};
311 };
312 
313 } // namespace fly::net
Definition: listen_socket.hpp:29
Definition: socket_service.hpp:26
Definition: tcp_socket.hpp:31
bool receive_async(ReceiveCompletion &&callback)
Definition: tcp_socket.cpp:297
TcpSocket(std::shared_ptr< NetworkConfig > config) noexcept
Definition: tcp_socket.cpp:14
std::size_t send(std::string_view message)
Definition: tcp_socket.cpp:239
std::optional< EndpointType > remote_endpoint() const
Definition: tcp_socket.cpp:120
ConnectedState connect(const EndpointType &endpoint)
Definition: tcp_socket.cpp:127
std::string receive()
Definition: tcp_socket.cpp:277
socket_type handle() const
Definition: base_socket.cpp:102
bool send_async(std::string_view message, SendCompletion &&callback)
Definition: tcp_socket.cpp:259
TcpSocket & operator=(TcpSocket &&socket) noexcept
Definition: tcp_socket.cpp:111
bool is_connected() const
Definition: tcp_socket.cpp:232
ConnectedState finish_connect()
Definition: tcp_socket.cpp:204
bool is_connecting() const
Definition: tcp_socket.cpp:225
ConnectedState connect_async(const EndpointType &endpoint, ConnectCompletion &&callback)
Definition: tcp_socket.cpp:166
Definition: base_socket.hpp:29
std::size_t packet_size() const
Definition: base_socket.cpp:196
socket_type handle() const
Definition: base_socket.cpp:102
void close()
Definition: base_socket.cpp:146
static std::optional< address_type > hostname_to_address(std::string_view hostname)
Definition: base_socket.cpp:81
bool is_open() const
Definition: base_socket.cpp:95
std::shared_ptr< fly::net::SocketService > socket_service() const
Definition: base_socket.cpp:182