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