MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
destination_socket.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_DESTINATION_SOCKET_INCLUDED
27#define MYSQL_HARNESS_DESTINATION_SOCKET_INCLUDED
28
29#include "harness_export.h"
30
31#include <variant>
32
37
38namespace mysql_harness {
39
40class HARNESS_EXPORT DestinationSocket {
41 public:
44
47
48 bool is_tcp() const { return std::holds_alternative<TcpType>(sock_); }
49 bool is_local() const { return !is_tcp(); }
50
51 TcpType &as_tcp() { return std::get<TcpType>(sock_); }
52 const TcpType &as_tcp() const { return std::get<TcpType>(sock_); }
53
54 LocalType &as_local() { return std::get<LocalType>(sock_); }
55 const LocalType &as_local() const { return std::get<LocalType>(sock_); }
56
58 const mysql_harness::DestinationEndpoint &ep, int flags = 0);
59
60 bool is_open() const {
61 if (is_local()) {
62 return as_local().is_open();
63 }
64
65 return as_tcp().is_open();
66 }
67
69 if (is_local()) {
70 return as_local().native_non_blocking(val);
71 }
72
73 return as_tcp().native_non_blocking(val);
74 }
75
77 if (is_local()) {
78 return as_local().native_handle();
79 }
80
81 return as_tcp().native_handle();
82 }
83
86
87 template <typename SettableSocketOption>
89 const SettableSocketOption &option) {
90 if (is_local()) {
91 return as_local().set_option(option);
92 }
93
94 return as_tcp().set_option(option);
95 }
96
97 template <typename GettableSocketOption>
99 GettableSocketOption &option) const {
100 if (is_local()) {
101 return as_local().get_option(option);
102 }
103
104 return as_tcp().get_option(option);
105 }
106
108 if (is_local()) {
109 return as_local().get_executor();
110 }
111
112 return as_tcp().get_executor();
113 }
114
115 net::io_context &io_context() { return get_executor().context(); }
116
118 if (is_local()) {
119 return as_local().cancel();
120 }
121
122 return as_tcp().cancel();
123 }
124
126 if (is_local()) {
127 return as_local().close();
128 }
129
130 return as_tcp().close();
131 }
132
133 template <class CompletionToken>
134 void async_wait(net::socket_base::wait_type wt, CompletionToken &&token) {
135 if (is_local()) {
136 return as_local().async_wait(wt, std::forward<CompletionToken>(token));
137 }
138
139 return as_tcp().async_wait(wt, std::forward<CompletionToken>(token));
140 }
141
142 template <class DynamicBuffer, class CompletionToken>
143 void async_send(DynamicBuffer &&dyn_buf, CompletionToken &&token)
144 requires(net::is_dynamic_buffer_v<DynamicBuffer>)
145 {
146 if (is_local()) {
147 return net::async_write(as_local(), std::forward<DynamicBuffer>(dyn_buf),
148 std::forward<CompletionToken>(token));
149 }
150
151 return net::async_write(as_tcp(), std::forward<DynamicBuffer>(dyn_buf),
152 std::forward<CompletionToken>(token));
153 }
154
155 template <class DynamicBuffer, class CompletionToken>
156 void async_recv(DynamicBuffer &&dyn_buf, CompletionToken &&token)
157 requires(net::is_dynamic_buffer_v<DynamicBuffer>)
158 {
159 if (is_local()) {
160 return net::async_read(as_local(), std::forward<DynamicBuffer>(dyn_buf),
161 std::forward<CompletionToken>(token));
162 }
163
164 return net::async_read(as_tcp(), std::forward<DynamicBuffer>(dyn_buf),
165 std::forward<CompletionToken>(token));
166 }
167
168 private:
169 std::variant<TcpType, LocalType> sock_;
170};
171
172} // namespace mysql_harness
173
174#endif
net::basic_stream_socket< stream_protocol > socket
Definition: local.h:304
Definition: destination_endpoint.h:38
Definition: destination_socket.h:40
net::io_context & io_context()
Definition: destination_socket.h:115
net::io_context::executor_type get_executor()
Definition: destination_socket.h:107
void async_recv(DynamicBuffer &&dyn_buf, CompletionToken &&token)
Definition: destination_socket.h:156
stdx::expected< void, std::error_code > native_non_blocking(bool val)
Definition: destination_socket.h:68
stdx::expected< void, std::error_code > cancel()
Definition: destination_socket.h:117
DestinationSocket(LocalType sock)
Definition: destination_socket.h:46
bool is_open() const
Definition: destination_socket.h:60
stdx::expected< void, std::error_code > get_option(GettableSocketOption &option) const
Definition: destination_socket.h:98
const LocalType & as_local() const
Definition: destination_socket.h:55
const TcpType & as_tcp() const
Definition: destination_socket.h:52
bool is_tcp() const
Definition: destination_socket.h:48
LocalType & as_local()
Definition: destination_socket.h:54
void async_send(DynamicBuffer &&dyn_buf, CompletionToken &&token)
Definition: destination_socket.h:143
DestinationSocket(TcpType sock)
Definition: destination_socket.h:45
void async_wait(net::socket_base::wait_type wt, CompletionToken &&token)
Definition: destination_socket.h:134
net::impl::socket::native_handle_type native_handle() const
Definition: destination_socket.h:76
TcpType & as_tcp()
Definition: destination_socket.h:51
bool is_local() const
Definition: destination_socket.h:49
stdx::expected< void, std::error_code > set_option(const SettableSocketOption &option)
Definition: destination_socket.h:88
stdx::expected< void, std::error_code > close()
Definition: destination_socket.h:125
std::variant< TcpType, LocalType > sock_
Definition: destination_socket.h:169
Definition: socket.h:1090
Definition: io_context.h:991
Definition: io_context.h:61
basic_stream_socket< tcp > socket
Definition: internet.h:1159
Definition: expected.h:286
static int flags[50]
Definition: hp_test1.cc:40
static MYSQL * sock
Definition: mysqlcheck.cc:57
Definition: common.h:44
wait_type
Definition: socket_constants.h:86
stdx::expected< void, error_type > connect(native_handle_type native_handle, const struct sockaddr *addr, size_t addr_len)
wrap connect() in a portable way.
Definition: socket.h:353
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
void async_write(AsyncWriteStream &stream, const ConstBufferSequence &buffers, CompletionCondition cond, CompletionToken &&token)
Definition: buffer.h:1066
Definition: gcs_xcom_synode.h:64
stdx::expected< int, std::error_code > open(const char *fname, int flags, mode_t mode) noexcept
Definition: file_handle.cc:79