qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1
25
26#ifndef QB_IO_ASYNC_TCP_SESSION_H
27#define QB_IO_ASYNC_TCP_SESSION_H
28
29#include "../../../uuid.h"
30#include "../io.h"
31
32namespace qb::io::async::tcp {
33
46template <typename _Derived, typename _Transport, typename _Server = void>
47class client
48 : public io<_Derived>
49 , _Transport {
50 using base_t = io<_Derived>;
51 friend base_t;
52
53public:
56 typename _Transport::transport_io_type;
57 using _Transport::in;
58 using _Transport::out;
59 using _Transport::transport;
60 using base_t::publish;
61
62protected:
63 const uuid _uuid;
64 _Server &_server;
65
66public:
67 using IOServer = _Server;
68 constexpr static const bool has_server =
69 true;
70
74 client() = delete;
75
85 explicit client(_Server &server)
87 , _server(server) {
88 if constexpr (has_member_Protocol<_Derived>::value) {
89 if constexpr (!std::is_void_v<typename _Derived::Protocol>) {
91 static_cast<_Derived &>(*this));
92 }
93 }
94 }
95
99 ~client() = default;
100
105 inline _Server &
107 return _server;
108 }
109
114 inline _Server &
115 server() const {
116 return _server;
117 }
118
123 inline uuid const &
124 id() const noexcept {
125 return _uuid;
126 }
127
132 inline auto
133 shared() const {
134 return server().session(id());
135 }
136
140 inline auto
141 ip() const {
142 return transport().peer_endpoint().ip();
143 }
144
149 inline auto
150 port() const {
151 return transport().peer_endpoint().port();
152 }
153};
154
168template <typename _Derived, typename _Transport>
170 : public io<_Derived>
171 , _Transport {
172 using base_t = io<_Derived>;
173 friend base_t;
174protected:
175 const uuid _uuid;
176public:
179 typename _Transport::transport_io_type;
180 using _Transport::in;
181 using _Transport::out;
182 using _Transport::transport;
183 using base_t::publish;
184
185public:
193 client() noexcept {
194 if constexpr (has_member_Protocol<_Derived>::value) {
195 if constexpr (!std::is_void_v<typename _Derived::Protocol>) {
197 static_cast<_Derived &>(*this));
198 }
199 }
200 }
201
209 ~client() = default;
210};
211
212} // namespace qb::io::async::tcp
213
214#endif // QB_IO_ASYNC_TCP_SESSION_H
_Protocol * switch_protocol(_Args &&...args)
Switches to a new protocol for I/O processing, taking ownership.
Definition io.h:988
io()=default
Default constructor.
auto & publish(_Args &&...args) noexcept
Publishes data to the output buffer and ensures write readiness.
Definition io.h:1073
io< _Derived > base_io_t
Base I/O type.
Definition client.h:177
client() noexcept
< Import the publish method from the base class
Definition client.h:193
const uuid _uuid
Unique identifier for this client.
Definition client.h:175
typename _Transport::transport_io_type transport_io_type
Transport I/O type.
Definition client.h:178
client()=delete
Default constructor (deleted)
static constexpr const bool has_server
Definition client.h:68
client(_Server &server)
Constructor with server reference.
Definition client.h:85
~client()=default
Destructor.
auto port() const
Get the client's port.
Definition client.h:150
uuid const & id() const noexcept
Get the client's unique identifier.
Definition client.h:124
_Server & server() const
Get the associated server (const version)
Definition client.h:115
auto ip() const
Get the client's IP address.
Definition client.h:141
typename transport::tcp::transport_io_type transport_io_type
Definition client.h:55
auto shared() const
Get the client as a shared pointer.
Definition client.h:133
io::tcp::socket & transport() noexcept
Definition stream.h:76
Core asynchronous I/O class templates for event-driven operations.
Universally Unique Identifier (UUID) support.
uuid generate_random_uuid()
Generates a random (version 4) UUID.
::uuids::uuid uuid
UUID type alias for the underlying implementation.
Definition uuid.h:40