MySQL 9.0.0
Source Code Documentation
bind.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 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 ROUTER_SRC_HTTP_SRC_HTTP_SERVER_BIND_H_
27#define ROUTER_SRC_HTTP_SRC_HTTP_SERVER_BIND_H_
28
29#include <cstdint>
30#include <string>
31#include <utility>
32
34
37
38namespace http {
39namespace server {
40
42 public:
48
49 Bind(io_context *io_context, const std::string &address, const uint16_t port);
50
51 static bool is_not_fatal(const std::error_code &error) {
52 return error == std::errc::resource_unavailable_try_again ||
54 }
55
56 template <typename Callback>
58 context_->get_executor().post(
59 [this, callback]() {
60 if (sync_state_.exchange(State::kInitializing, State::kRunning)) {
61 on_new_socket_callback(callback);
62 }
63 },
64 nullptr);
65 }
66
68 sync_state_.change([this](auto &value) {
69 switch (value) {
70 case State::kInitializing:
71 value = State::kTerminated;
72 break;
73 case State::kRunning:
74 value = State::kStopping;
75 break;
76 default:
77 break;
78 }
79 socket_.cancel();
80 });
81
82 sync_state_.wait(State::kTerminated);
83 }
84
85 endpoint local_endpoint() { return socket_.local_endpoint().value(); }
86
87 private:
88 template <typename Callback>
90 socket_.async_accept(
91 [this, callback](const std::error_code &error, socket_type socket) {
92 if (!error || is_not_fatal(error)) {
93 if (sync_state_.is(State::kRunning,
94 [this, &error, &socket, &callback]() {
95 if (!error) callback(std::move(socket));
96 on_new_socket_callback(callback);
97 })) {
98 return;
99 }
100 }
101 sync_state_.set(State::kTerminated);
102 });
103 }
104
105 enum class State { kInitializing, kRunning, kStopping, kTerminated };
106
108 resolver resolver_{*context_};
109 acceptor socket_{*context_};
110 WaitableVariable<State> sync_state_{State::kInitializing};
111};
112
113} // namespace server
114} // namespace http
115
116#endif // ROUTER_SRC_HTTP_SRC_HTTP_SERVER_BIND_H_
Class that stores callback function reference as well as the result of the callback function call (in...
Definition: keyring_service.cc:43
Definition: wait_variable.h:34
Definition: bind.h:41
io_context * context_
Definition: bind.h:107
State
Definition: bind.h:105
void on_new_socket_callback(Callback callback)
Definition: bind.h:89
static bool is_not_fatal(const std::error_code &error)
Definition: bind.h:51
acceptor::endpoint_type endpoint
Definition: bind.h:47
acceptor::socket_type socket_type
Definition: bind.h:46
void start_accepting_loop(Callback callback)
Definition: bind.h:57
void stop_accepting_loop()
Definition: bind.h:67
endpoint local_endpoint()
Definition: bind.h:85
Definition: socket.h:1293
typename protocol_type::endpoint endpoint_type
Definition: socket.h:1301
typename protocol_type::socket socket_type
Definition: socket.h:1300
Definition: io_context.h:61
Definition: internet.h:608
basic_resolver< tcp > resolver
Definition: internet.h:1158
basic_socket_acceptor< tcp > acceptor
Definition: internet.h:1160
#define HTTP_SERVER_LIB_EXPORT
Definition: http_server_lib_export.h:15
static bool interrupted
Definition: mysqladmin.cc:72
void error(const char *format,...)
Definition: connection.h:56
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
Definition: server_struct.h:39