25#ifndef QB_IO_ASYNC_IO_HANDLER_H
26#define QB_IO_ASYNC_IO_HANDLER_H
32namespace qb::io::async {
46template <
typename _Derived,
typename _Session>
61 friend typename _Session::base_io_t;
72 disconnected(
uuid ident) {
73 _sessions.erase(ident);
118 std::shared_ptr<_Session>
120 auto it = _sessions.find(
id);
121 return (it != std::end(_sessions)) ? it->second :
nullptr;
136 template <
typename... Args>
139 auto session = std::make_shared<_Session>(
static_cast<_Derived &
>(*
this),
140 std::forward<Args>(args)...);
142 session->transport() = std::move(new_io);
144 if constexpr (has_method_on<_Derived, void, _Session &>::value)
145 static_cast<_Derived &
>(*this).on(*
session);
158 auto it = _sessions.find(ident);
159 if (it != _sessions.cend())
160 it->second->disconnect();
171 [[nodiscard]] std::pair<typename _Session::transport_io_type, bool>
173 auto it = _sessions.find(ident);
174 if (it != _sessions.cend()) {
175 if constexpr (has_method_on<_Session, void, qb::io::async::event::extracted>::value)
176 (*it->second).on(qb::io::async::event::extracted{});
177 auto t_io = std::move(it->second->transport());
179 return {std::move(t_io),
true};
181 return {
typename _Session::transport_io_type{},
false};
193 template <
typename... _Args>
197 (*
session << ... << std::forward<_Args>(args));
198 return static_cast<_Derived &
>(*this);
212 template <
typename _Func,
typename... _Args>
217 (*
session << ... << std::forward<_Args>(args));
218 return static_cast<_Derived &
>(*this);
std::shared_ptr< _Session > session(uuid id)
Get a session by its UUID.
Definition io_handler.h:119
session_map_t & sessions()
Get the map of active sessions.
Definition io_handler.h:108
_Derived & stream(_Args &&...args)
Broadcast data to all sessions.
Definition io_handler.h:195
qb::unordered_map< uuid, std::shared_ptr< _Client > > session_map_t
Definition io_handler.h:82
io_handler()=default
Default constructor.
std::pair< typename _Session::transport_io_type, bool > extractSession(uuid const &ident)
Extract a session's IO object.
Definition io_handler.h:172
~io_handler()=default
Default destructor.
_Session & registerSession(typename _Session::transport_io_type &&new_io, Args &&...args)
Register a new session.
Definition io_handler.h:138
_Derived & stream_if(_Func const &func, _Args &&...args)
Broadcast data to selected sessions.
Definition io_handler.h:214
_Client IOSession
Definition io_handler.h:91
void unregisterSession(uuid const &ident)
Unregister a session.
Definition io_handler.h:157
std::unordered_map< K, V, H, E, A > unordered_map
The primary unordered map implementation.
Definition unordered_map.h:90
Optimized unordered map implementations.
Universally Unique Identifier (UUID) support.
::uuids::uuid uuid
UUID type alias for the underlying implementation.
Definition uuid.h:40