MySQL 8.4.2
Source Code Documentation
socket_error.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 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_NET_TS_IMPL_SOCKET_ERROR_H_
27#define MYSQL_HARNESS_NET_TS_IMPL_SOCKET_ERROR_H_
28
29#include <system_error>
30
31#ifdef _WIN32
32#include <WinSock2.h>
33#include <Windows.h>
34#else
35#include <cerrno>
36#endif
37
38namespace net {
39enum class socket_errc {
40 already_open = 1,
41 not_found = 2,
42};
43} // namespace net
44
45namespace std {
46template <>
47struct is_error_code_enum<net::socket_errc> : public true_type {};
48} // namespace std
49
50namespace net {
51inline const std::error_category &socket_category() noexcept {
52 class category_impl : public std::error_category {
53 public:
54 const char *name() const noexcept override { return "socket"; }
55 std::string message(int ev) const override {
56 switch (static_cast<socket_errc>(ev)) {
58 return "already_open";
60 return "not found";
61 }
62
63 // don't use switch-default to trigger a warning for unhandled enum-value
64 return "unknown";
65 }
66 };
67
68 static category_impl instance;
69 return instance;
70}
71
72inline std::error_code make_error_code(net::socket_errc e) noexcept {
73 return {static_cast<int>(e), net::socket_category()};
74}
75
76namespace impl {
77namespace socket {
78
79/**
80 * get last socket error.
81 */
82inline int last_error() {
83#ifdef _WIN32
84 return WSAGetLastError();
85#else
86 return errno;
87#endif
88}
89
90/**
91 * make proper std::error_code for socket errno's
92 *
93 * On windows, WSAGetLastError() returns a code from the system-category
94 * On POSIX systems, errno returns a code from the generic-category
95 */
96inline std::error_code make_error_code(int errcode) {
97#ifdef _WIN32
98 return {errcode, std::system_category()};
99#else
100 return {errcode, std::generic_category()};
101#endif
102}
103
104/**
105 * get last std::error_code for socket-errors.
106 */
107inline std::error_code last_error_code() {
108 return make_error_code(last_error());
109}
110
111} // namespace socket
112} // namespace impl
113} // namespace net
114
115#endif
Definition: http_server_component.cc:34
std::error_code make_error_code(int errcode)
make proper std::error_code for socket errno's
Definition: socket_error.h:96
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
std::error_code last_error_code()
get last std::error_code for socket-errors.
Definition: socket_error.h:107
int last_error()
get last socket error.
Definition: socket_error.h:82
Definition: buffer.h:45
socket_errc
Definition: socket_error.h:39
const std::error_category & socket_category() noexcept
Definition: socket_error.h:51
std::error_code make_error_code(net::stream_errc e) noexcept
Definition: buffer.h:103
Definition: gcs_xcom_synode.h:64
case opt name
Definition: sslopt-case.h:29