MySQL 8.0.39
Source Code Documentation
socket_container.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 ROUTING_SOCKETCONTAINER_INCLUDED
27#define ROUTING_SOCKETCONTAINER_INCLUDED
28
29#include <mutex>
30#include <vector>
31
32/**
33 * container of sockets.
34 *
35 * allows to disconnect all of them.
36 *
37 * thread-safe.
38 */
39template <class Protocol>
41 public:
44
45 // as a ref will get returned, the socket_type' object needs a stable address.
46 // - std::list<socket_type> provide that.
47 // - std::vector<std::unique_ptr<socket_type>> should work too.
48 using container_type = std::list<socket_type>;
49
50 /**
51 * move ownership of socket_type to the container.
52 *
53 * @return a ref to the stored socket.
54 */
56 std::lock_guard<std::mutex> lk(mtx_);
57
58 sockets_.push_back(std::move(sock));
59
60 return sockets_.back();
61 }
62
63 /**
64 * move ownership of socket_type to the container.
65 *
66 * @return a ref to the stored socket.
67 */
68 template <class... Args>
69 socket_type &emplace_back(Args &&... args) {
70 std::lock_guard<std::mutex> lk(mtx_);
71
72 sockets_.emplace_back(std::forward<Args>(args)...);
73
74 return sockets_.back();
75 }
76
77 /**
78 * release socket from container.
79 *
80 * moves ownership of the socket to the caller.
81 *
82 * @return socket
83 */
85 std::lock_guard<std::mutex> lk(mtx_);
86
87 return release_unlocked(client_sock);
88 }
89
91 for (auto cur = sockets_.begin(); cur != sockets_.end(); ++cur) {
92 if (cur->native_handle() == client_sock.native_handle()) {
93 auto sock = std::move(*cur);
94 sockets_.erase(cur);
95 return sock;
96 }
97 }
98
99 // not found.
100 return socket_type{client_sock.get_executor().context()};
101 }
102
103 template <class F>
104 auto run(F &&f) {
105 std::lock_guard<std::mutex> lk(mtx_);
106
107 return f();
108 }
109
110 /**
111 * disconnect all sockets.
112 */
113
115 std::lock_guard<std::mutex> lk(mtx_);
116
117 for (auto &sock : sockets_) {
118 sock.cancel();
119 }
120 }
121
122 /**
123 * check if the container is empty.
124 */
125 bool empty() const {
126 std::lock_guard<std::mutex> lk(mtx_);
127 return sockets_.empty();
128 }
129
130 /**
131 * get size of container.
132 */
133 size_t size() const {
134 std::lock_guard<std::mutex> lk(mtx_);
135 return sockets_.size();
136 }
137
138 private:
140
141 mutable std::mutex mtx_;
142};
143
144#endif
Definition: protocol.h:33
container of sockets.
Definition: socket_container.h:40
std::list< socket_type > container_type
Definition: socket_container.h:48
bool empty() const
check if the container is empty.
Definition: socket_container.h:125
auto run(F &&f)
Definition: socket_container.h:104
socket_type & push_back(socket_type &&sock)
move ownership of socket_type to the container.
Definition: socket_container.h:55
socket_type release(socket_type &client_sock)
release socket from container.
Definition: socket_container.h:84
container_type sockets_
Definition: socket_container.h:139
socket_type & emplace_back(Args &&... args)
move ownership of socket_type to the container.
Definition: socket_container.h:69
size_t size() const
get size of container.
Definition: socket_container.h:133
void disconnect_all()
disconnect all sockets.
Definition: socket_container.h:114
typename protocol_type::socket socket_type
Definition: socket_container.h:43
socket_type release_unlocked(socket_type &client_sock)
Definition: socket_container.h:90
std::mutex mtx_
Definition: socket_container.h:141
static MYSQL * sock
Definition: mysqlcheck.cc:56
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63