MySQL 8.3.0
Source Code Documentation
socket_container.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef ROUTING_SOCKETCONTAINER_INCLUDED
26#define ROUTING_SOCKETCONTAINER_INCLUDED
27
28#include <mutex>
29#include <vector>
30
31/**
32 * container of sockets.
33 *
34 * allows to disconnect all of them.
35 *
36 * thread-safe.
37 */
38template <class Protocol>
40 public:
43
44 // as a ref will get returned, the socket_type' object needs a stable address.
45 // - std::list<socket_type> provide that.
46 // - std::vector<std::unique_ptr<socket_type>> should work too.
47 using container_type = std::list<socket_type>;
48
49 /**
50 * move ownership of socket_type to the container.
51 *
52 * @return a ref to the stored socket.
53 */
55 std::lock_guard<std::mutex> lk(mtx_);
56
57 sockets_.push_back(std::move(sock));
58
59 return sockets_.back();
60 }
61
62 /**
63 * move ownership of socket_type to the container.
64 *
65 * @return a ref to the stored socket.
66 */
67 template <class... Args>
68 socket_type &emplace_back(Args &&... args) {
69 std::lock_guard<std::mutex> lk(mtx_);
70
71 sockets_.emplace_back(std::forward<Args>(args)...);
72
73 return sockets_.back();
74 }
75
76 /**
77 * release socket from container.
78 *
79 * moves ownership of the socket to the caller.
80 *
81 * @return socket
82 */
84 std::lock_guard<std::mutex> lk(mtx_);
85
86 return release_unlocked(client_sock);
87 }
88
90 for (auto cur = sockets_.begin(); cur != sockets_.end(); ++cur) {
91 if (cur->native_handle() == client_sock.native_handle()) {
92 auto sock = std::move(*cur);
93 sockets_.erase(cur);
94 return sock;
95 }
96 }
97
98 // not found.
99 return socket_type{client_sock.get_executor().context()};
100 }
101
102 template <class F>
103 auto run(F &&f) {
104 std::lock_guard<std::mutex> lk(mtx_);
105
106 return f();
107 }
108
109 /**
110 * disconnect all sockets.
111 */
112
114 std::lock_guard<std::mutex> lk(mtx_);
115
116 for (auto &sock : sockets_) {
117 sock.cancel();
118 }
119 }
120
121 /**
122 * check if the container is empty.
123 */
124 bool empty() const {
125 std::lock_guard<std::mutex> lk(mtx_);
126 return sockets_.empty();
127 }
128
129 /**
130 * get size of container.
131 */
132 size_t size() const {
133 std::lock_guard<std::mutex> lk(mtx_);
134 return sockets_.size();
135 }
136
137 private:
139
140 mutable std::mutex mtx_;
141};
142
143#endif
Definition: protocol.h:32
container of sockets.
Definition: socket_container.h:39
std::list< socket_type > container_type
Definition: socket_container.h:47
bool empty() const
check if the container is empty.
Definition: socket_container.h:124
auto run(F &&f)
Definition: socket_container.h:103
socket_type & push_back(socket_type &&sock)
move ownership of socket_type to the container.
Definition: socket_container.h:54
socket_type release(socket_type &client_sock)
release socket from container.
Definition: socket_container.h:83
container_type sockets_
Definition: socket_container.h:138
socket_type & emplace_back(Args &&... args)
move ownership of socket_type to the container.
Definition: socket_container.h:68
size_t size() const
get size of container.
Definition: socket_container.h:132
void disconnect_all()
disconnect all sockets.
Definition: socket_container.h:113
typename protocol_type::socket socket_type
Definition: socket_container.h:42
socket_type release_unlocked(socket_type &client_sock)
Definition: socket_container.h:89
std::mutex mtx_
Definition: socket_container.h:140
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:62