3 #include "fly/net/socket/concepts.hpp"
4 #include "fly/net/socket/types.hpp"
11 class SequencedTaskRunner;
25 class SocketService :
public std::enable_shared_from_this<SocketService>
41 static std::shared_ptr<SocketService>
create(
42 std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
43 std::shared_ptr<NetworkConfig> config);
50 template <Socket SocketType>
80 template <Socket SocketType, SocketNotification<SocketType> Callback>
101 template <Socket SocketType, SocketNotification<SocketType> Callback>
105 using Notification = std::function<void()>;
109 Request(socket_type handle, Notification callback) noexcept;
111 socket_type m_handle;
112 Notification m_callback;
121 std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
122 std::shared_ptr<NetworkConfig> config) noexcept;
161 template <Socket SocketType, SocketNotification<SocketType> Callback>
162 Notification wrap_callback(
const std::shared_ptr<SocketType> &socket, Callback &&callback);
170 std::shared_ptr<fly::task::SequencedTaskRunner> m_task_runner;
172 std::shared_ptr<NetworkConfig> m_config;
174 std::vector<Request> m_write_requests;
175 std::vector<Request> m_read_requests;
179 template <Socket SocketType>
182 return SocketType::create_socket(shared_from_this(), m_config);
186 template <Socket SocketType, SocketNotification<SocketType> Callback>
188 const std::shared_ptr<SocketType> &socket,
191 notify_when_writable(socket->handle(), wrap_callback(socket, std::forward<Callback>(callback)));
195 template <Socket SocketType, SocketNotification<SocketType> Callback>
197 const std::shared_ptr<SocketType> &socket,
200 notify_when_readable(socket->handle(), wrap_callback(socket, std::forward<Callback>(callback)));
204 template <Socket SocketType, SocketNotification<SocketType> Callback>
205 auto SocketService::wrap_callback(
const std::shared_ptr<SocketType> &socket, Callback &&callback)
209 struct CallbackHolder
214 std::weak_ptr<SocketType> weak_socket = socket;
215 CallbackHolder holder {std::forward<Callback>(callback)};
217 return [weak_socket = std::move(weak_socket), holder = std::move(holder)]()
mutable {
218 if (std::shared_ptr<SocketType> strong_socket = weak_socket.lock(); strong_socket)
220 std::invoke(std::move(holder.m_callback), std::move(strong_socket));
Definition: socket_service.hpp:26
void notify_when_readable(const std::shared_ptr< SocketType > &socket, Callback &&callback)
Definition: socket_service.hpp:196
void remove_socket(socket_type handle)
Definition: socket_service.cpp:48
~SocketService() noexcept
Definition: socket_service.cpp:42
std::shared_ptr< SocketType > create_socket()
Definition: socket_service.hpp:180
void notify_when_writable(const std::shared_ptr< SocketType > &socket, Callback &&callback)
Definition: socket_service.hpp:187
static std::shared_ptr< SocketService > create(std::shared_ptr< fly::task::SequencedTaskRunner > task_runner, std::shared_ptr< NetworkConfig > config)
Definition: socket_service.cpp:12