libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
udp_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 <string>
11 
12 namespace fly::net {
13 
14 class NetworkConfig;
15 class SocketService;
16 
23 template <IPEndpoint EndpointType>
24 class UdpSocket :
25  public detail::BaseSocket<EndpointType>,
26  public std::enable_shared_from_this<UdpSocket<EndpointType>>
27 {
29 
30  using SendCompletion = std::function<void(std::size_t)>;
31  using ReceiveCompletion = std::function<void(std::string)>;
32 
33 public:
39  explicit UdpSocket(std::shared_ptr<NetworkConfig> config) noexcept;
40 
47  UdpSocket(std::shared_ptr<NetworkConfig> config, IOMode mode) noexcept;
48 
54  UdpSocket(UdpSocket &&socket) noexcept;
55 
63  UdpSocket &operator=(UdpSocket &&socket) noexcept;
64 
74  std::size_t send(const EndpointType &endpoint, std::string_view message);
75 
86  std::size_t send(std::string_view hostname, port_type port, std::string_view message);
87 
103  bool
104  send_async(const EndpointType &endpoint, std::string_view message, SendCompletion &&callback);
105 
122  bool send_async(
123  std::string_view hostname,
124  port_type port,
125  std::string_view message,
126  SendCompletion &&callback);
127 
134  std::string receive();
135 
148  bool receive_async(ReceiveCompletion &&callback);
149 
150  using BaseSocket::close;
151  using BaseSocket::handle;
153 
154 private:
155  friend SocketService;
156 
165  static std::shared_ptr<UdpSocket> create_socket(
166  const std::shared_ptr<SocketService> &service,
167  std::shared_ptr<NetworkConfig> config);
168 
176  UdpSocket(
177  const std::shared_ptr<SocketService> &service,
178  std::shared_ptr<NetworkConfig> config) noexcept;
179 
193  void ready_to_send(
194  const EndpointType &endpoint,
195  std::string_view message,
196  SendCompletion &&callback,
197  std::size_t bytes_sent,
198  std::size_t total_bytes);
199 
210  void ready_to_receive(ReceiveCompletion &&callback, std::string received);
211 
212  UdpSocket(const UdpSocket &) = delete;
213  UdpSocket &operator=(const UdpSocket &) = delete;
214 
217 };
218 
219 } // namespace fly::net
Definition: socket_service.hpp:26
Definition: udp_socket.hpp:27
UdpSocket & operator=(UdpSocket &&socket) noexcept
Definition: udp_socket.cpp:64
std::size_t send(const EndpointType &endpoint, std::string_view message)
Definition: udp_socket.cpp:71
std::string receive()
Definition: udp_socket.cpp:148
bool send_async(const EndpointType &endpoint, std::string_view message, SendCompletion &&callback)
Definition: udp_socket.cpp:107
bool receive_async(ReceiveCompletion &&callback)
Definition: udp_socket.cpp:170
UdpSocket(std::shared_ptr< NetworkConfig > config) noexcept
Definition: udp_socket.cpp:14
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
std::shared_ptr< fly::net::SocketService > socket_service() const
Definition: base_socket.cpp:182