26#ifndef ROUTER_CONNECTION_POOL_CONNECTION_BASE_INCLUDED
27#define ROUTER_CONNECTION_POOL_CONNECTION_BASE_INCLUDED
31#include <system_error>
41#include <openssl/ssl.h>
59 std::vector<uint8_t, default_init_allocator<uint8_t>>;
65 std::function<
void(std::error_code ec,
size_t transferred)>) = 0;
69 std::function<
void(std::error_code ec,
size_t transferred)>) = 0;
75 [[nodiscard]]
virtual bool is_open()
const = 0;
84 [[nodiscard]]
virtual std::string
endpoint()
const = 0;
94template <
class Protocol>
97#ifdef NET_TS_HAS_UNIX_SOCKET
112template <
class Protocol>
131 return sock_.release().and_then(
137 if (!assign_res)
return assign_res;
146 std::function<
void(std::error_code ec,
size_t transferred)>
147 completion)
override {
149 std::move(completion));
153 std::function<
void(std::error_code ec,
size_t transferred)>
154 completion)
override {
155 if (
sock_.native_non_blocking()) {
162 transferred = *write_res]() {
163 completion({}, transferred);
168 const auto ec = write_res.error();
174 ec]() { completion(ec, 0); });
182 std::move(completion));
186 std::function<
void(std::error_code ec)> completion)
override {
191 std::function<
void(std::error_code ec)> completion)
override {
196 std::function<
void(std::error_code ec)> completion)
override {
200 [[nodiscard]]
bool is_open()
const override {
return sock_.is_open(); }
204 return sock_.native_handle();
208 return sock_.close();
212 return sock_.cancel();
217 return sock_.shutdown(st);
220 [[nodiscard]] std::string
endpoint()
const override {
228 template <
class GettableSocketOption>
230 GettableSocketOption &opt)
const {
231 return sock_.get_option(opt);
249#ifdef NET_TS_HAS_UNIX_SOCKET
297 conn_ = std::move(conn);
302 SSL_set_info_callback(
ssl,
nullptr);
303 SSL_set_msg_callback_arg(
ssl,
nullptr);
316 template <
class Func>
324 [
this, func = std::forward<Func>(func)](
325 std::error_code ec,
size_t transferred) {
326 if (ec == std::error_code()) {
327 channel_.view_sync_raw();
330 func(ec, transferred);
339 template <
class Func>
341 conn_->async_send(channel_.send_buffer(), std::forward<Func>(func));
349 template <
class Func>
351 conn_->async_wait_send(std::forward<Func>(func));
354 template <
class Func>
356 conn_->async_wait_error(std::forward<Func>(func));
365 [[nodiscard]]
bool is_open()
const {
return conn_ && conn_->is_open(); }
368 return conn_->native_handle();
375 return conn_->close();
383 return conn_->shutdown(st);
387 if (!is_open())
return "";
389 return conn_->endpoint();
393 if (!conn_)
return {};
395 return conn_->cancel();
404 std::unique_ptr<ConnectionBase> &
connection() {
return conn_; }
406 const std::unique_ptr<ConnectionBase> &
connection()
const {
return conn_; }
415 return conn_->is_secure_transport() || (channel_.ssl() !=
nullptr);
420 std::unique_ptr<ConnectionBase>
conn_;
basic connection which wraps a net-ts Protocol.
Definition: connection_base.h:113
stdx::expected< void, std::error_code > set_io_context(net::io_context &new_ctx) override
Definition: connection_base.h:126
stdx::expected< void, std::error_code > get_option(GettableSocketOption &opt) const
Definition: connection_base.h:229
stdx::expected< void, std::error_code > close() override
Definition: connection_base.h:207
void async_wait_error(std::function< void(std::error_code ec)> completion) override
Definition: connection_base.h:195
std::string endpoint() const override
Definition: connection_base.h:220
void async_wait_recv(std::function< void(std::error_code ec)> completion) override
Definition: connection_base.h:190
net::impl::socket::native_handle_type native_handle() const override
Definition: connection_base.h:202
void async_recv(recv_buffer_type &buf, std::function< void(std::error_code ec, size_t transferred)> completion) override
Definition: connection_base.h:145
stdx::expected< void, std::error_code > cancel() override
Definition: connection_base.h:211
bool is_open() const override
Definition: connection_base.h:200
socket_type sock_
Definition: connection_base.h:244
typename protocol_type::socket socket_type
Definition: connection_base.h:116
BasicConnection(socket_type sock, endpoint_type ep)
Definition: connection_base.h:121
bool is_secure_transport() const override
check if the underlying transport is secure.
Definition: connection_base.h:239
net::io_context & io_ctx() override
Definition: connection_base.h:124
endpoint_type ep_
Definition: connection_base.h:245
stdx::expected< void, std::error_code > shutdown(net::socket_base::shutdown_type st) override
Definition: connection_base.h:215
void async_send(recv_buffer_type &buf, std::function< void(std::error_code ec, size_t transferred)> completion) override
Definition: connection_base.h:152
void async_wait_send(std::function< void(std::error_code ec)> completion) override
Definition: connection_base.h:185
typename protocol_type::endpoint endpoint_type
Definition: connection_base.h:117
SSL aware socket buffers.
Definition: channel.h:65
void view_discard_raw()
Definition: channel.cc:302
void clear()
clears all buffers.
Definition: channel.h:79
SSL * ssl() const
get access to the raw SSL handle.
Definition: channel.h:324
recv_buffer_type & recv_buffer()
buffer of data that was received from the socket.
Definition: channel.h:240
virtual base-class of BasicConnection.
Definition: connection_base.h:54
virtual void async_send(recv_buffer_type &, std::function< void(std::error_code ec, size_t transferred)>)=0
std::vector< uint8_t, default_init_allocator< uint8_t > > recv_buffer_type
Definition: connection_base.h:59
virtual void async_wait_recv(std::function< void(std::error_code ec)>)=0
virtual stdx::expected< void, std::error_code > close()=0
virtual void async_recv(recv_buffer_type &, std::function< void(std::error_code ec, size_t transferred)>)=0
virtual void async_wait_send(std::function< void(std::error_code ec)>)=0
virtual bool is_secure_transport() const =0
virtual net::impl::socket::native_handle_type native_handle() const =0
virtual net::io_context & io_ctx()=0
virtual std::string endpoint() const =0
virtual bool is_open() const =0
virtual stdx::expected< void, std::error_code > shutdown(net::socket_base::shutdown_type st)=0
virtual stdx::expected< void, std::error_code > cancel()=0
virtual stdx::expected< void, std::error_code > set_io_context(net::io_context &new_ctx)=0
virtual void async_wait_error(std::function< void(std::error_code ec)>)=0
virtual ~ConnectionBase()=default
Definition: protocol.h:33
a Connection that can be switched to TLS.
Definition: connection_base.h:264
void prepare_for_pool()
Definition: connection_base.h:300
SslMode ssl_mode_
Definition: connection_base.h:422
Channel & channel()
Definition: connection_base.h:359
std::unique_ptr< ConnectionBase > conn_
Definition: connection_base.h:420
T protocol_state_type
Definition: connection_base.h:272
bool is_secure_transport() const
check if the channel is secure.
Definition: connection_base.h:414
void async_wait_send(Func &&func)
async wait until connection allows to send data.
Definition: connection_base.h:350
std::string endpoint() const
Definition: connection_base.h:386
protocol_state_type protocol_
Definition: connection_base.h:428
protocol_state_type & protocol()
Definition: connection_base.h:398
const protocol_state_type & protocol() const
Definition: connection_base.h:400
const std::unique_ptr< ConnectionBase > & connection() const
Definition: connection_base.h:406
bool is_open() const
Definition: connection_base.h:365
void async_send(Func &&func)
async send data from the channel's send buffer to the connection.
Definition: connection_base.h:340
std::unique_ptr< ConnectionBase > & connection()
Definition: connection_base.h:404
TlsSwitchableConnection(std::unique_ptr< ConnectionBase > conn, SslMode ssl_mode, protocol_state_type state)
Definition: connection_base.h:274
void async_recv(Func &&func)
async receive data from connection into the channel's receive buffer.
Definition: connection_base.h:317
net::impl::socket::native_handle_type native_handle() const
Definition: connection_base.h:367
stdx::expected< void, std::error_code > cancel()
Definition: connection_base.h:392
void assign_connection(std::unique_ptr< ConnectionBase > conn)
assign a low-level connection.
Definition: connection_base.h:296
void async_wait_error(Func &&func)
Definition: connection_base.h:355
Channel channel_
Definition: connection_base.h:425
stdx::expected< void, std::error_code > shutdown(net::socket_base::shutdown_type st) const
Definition: connection_base.h:378
stdx::expected< void, std::error_code > close() const
Definition: connection_base.h:371
const Channel & channel() const
Definition: connection_base.h:361
static constexpr size_t kRecvBufferSize
Definition: connection_base.h:271
SslMode ssl_mode() const
Definition: connection_base.h:363
TlsSwitchableConnection(std::unique_ptr< ConnectionBase > conn, SslMode ssl_mode, Channel channel, protocol_state_type state)
Definition: connection_base.h:283
Definition: io_context.h:61
executor_type get_executor() noexcept
Definition: io_context.h:1081
static constexpr wait_type wait_read
Definition: socket.h:168
shutdown_type
Definition: socket.h:172
static constexpr wait_type wait_write
Definition: socket.h:169
static constexpr wait_type wait_error
Definition: socket.h:170
Definition: expected.h:286
#define harness_assert(COND)
Improved assert()
Definition: harness_assert.h:37
static MYSQL * sock
Definition: mysqlcheck.cc:57
Definition: buf0block_hint.cc:30
constexpr value_type ssl
Definition: classic_protocol_constants.h:49
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:79
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
int native_handle_type
Definition: socket_constants.h:51
void async_read(AsyncReadStream &stream, DynamicBuffer &&b, CompletionCondition completion_condition, CompletionToken &&token)
Definition: buffer.h:933
stdx::expected< size_t, std::error_code > write(SyncWriteStream &stream, const ConstBufferSequence &buffers)
Definition: buffer.h:977
dynamic_vector_buffer< T, Allocator > dynamic_buffer(std::vector< T, Allocator > &vec) noexcept
Definition: buffer.h:661
auto defer(CompletionToken &&token)
Definition: executor.h:779
void async_write(AsyncWriteStream &stream, const ConstBufferSequence &buffers, CompletionCondition cond, CompletionToken &&token)
Definition: buffer.h:1066
std::error_condition make_error_condition(net::stream_errc e) noexcept
Definition: buffer.h:107
Definition: gcs_xcom_synode.h:64
unexpected(E) -> unexpected< E >
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2872
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:663
SslMode
Definition: ssl_mode.h:29
Definition: connection_base.h:95