qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
io_handler.h
Go to the documentation of this file.
1
24
25#ifndef QB_IO_ASYNC_IO_HANDLER_H
26#define QB_IO_ASYNC_IO_HANDLER_H
27
29#include <qb/uuid.h>
31
32namespace qb::io::async {
33
46template <typename _Derived, typename _Session>
61 friend typename _Session::base_io_t;
62
71 void
72 disconnected(uuid ident) {
73 _sessions.erase(ident);
74 }
75
76public:
83
84private:
85 session_map_t _sessions;
86
87public:
91 using IOSession = _Session;
92
96 io_handler() = default;
97
101 ~io_handler() = default;
102
109 return _sessions;
110 }
111
118 std::shared_ptr<_Session>
120 auto it = _sessions.find(id);
121 return (it != std::end(_sessions)) ? it->second : nullptr;
122 }
123
136 template <typename... Args>
137 _Session &
138 registerSession(typename _Session::transport_io_type &&new_io, Args &&...args) {
139 auto session = std::make_shared<_Session>(static_cast<_Derived &>(*this),
140 std::forward<Args>(args)...);
141 sessions().emplace(session->id(), session);
142 session->transport() = std::move(new_io);
143 session->start();
144 if constexpr (has_method_on<_Derived, void, _Session &>::value)
145 static_cast<_Derived &>(*this).on(*session);
146 return *session;
147 }
148
156 void
157 unregisterSession(uuid const &ident) {
158 auto it = _sessions.find(ident);
159 if (it != _sessions.cend())
160 it->second->disconnect();
161 }
162
171 [[nodiscard]] std::pair<typename _Session::transport_io_type, bool>
172 extractSession(uuid const &ident) {
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());
178 _sessions.erase(it);
179 return {std::move(t_io), true};
180 }
181 return {typename _Session::transport_io_type{}, false};
182 }
183
193 template <typename... _Args>
194 _Derived &
195 stream(_Args &&...args) {
196 for (auto &[key, session] : sessions())
197 (*session << ... << std::forward<_Args>(args));
198 return static_cast<_Derived &>(*this);
199 }
200
212 template <typename _Func, typename... _Args>
213 _Derived &
214 stream_if(_Func const &func, _Args &&...args) {
215 for (auto &[key, session] : sessions())
216 if (func(*session))
217 (*session << ... << std::forward<_Args>(args));
218 return static_cast<_Derived &>(*this);
219 }
220};
221
222} // namespace qb::io::async
223
224#endif // QB_IO_ASYNC_IO_HANDLER_H
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
Extracted event for asynchronous I/O operations.
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