MPD: net/SocketError.hxx Source File

MPD 0.20.18

SocketError.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_SOCKET_ERROR_HXX
21 #define MPD_SOCKET_ERROR_HXX
22
23 #include "Compiler.h"
24 #include "system/Error.hxx"
25
26 #ifdef _WIN32
27 #include <winsock2.h>
28 typedef DWORD socket_error_t;
29 #else
30 #include <errno.h>
31 typedef int socket_error_t;
32 #endif
33
34 gcc_pure
35 static inline socket_error_t
36 GetSocketError() noexcept
37 {
38 #ifdef _WIN32
39 return WSAGetLastError();
40 #else
41 return errno;
42 #endif
43 }
44
45 gcc_const
46 static inline bool
47 IsSocketErrorAgain(socket_error_t code) noexcept
48 {
49 #ifdef _WIN32
50 return code == WSAEINPROGRESS;
51 #else
52 return code == EAGAIN;
53 #endif
54 }
55
56 gcc_const
57 static inline bool
58 IsSocketErrorInterruped(socket_error_t code) noexcept
59 {
60 #ifdef _WIN32
61 return code == WSAEINTR;
62 #else
63 return code == EINTR;
64 #endif
65 }
66
67 gcc_const
68 static inline bool
69 IsSocketErrorClosed(socket_error_t code) noexcept
70 {
71 #ifdef _WIN32
72 return code == WSAECONNRESET;
73 #else
74 return code == EPIPE || code == ECONNRESET;
75 #endif
76 }
77
83 class SocketErrorMessage {
84 #ifdef _WIN32
85 char msg[256];
86 #else
87 const char *const msg;
88 #endif
89
90 public:
91 explicit SocketErrorMessage(socket_error_t code=GetSocketError()) noexcept;
92
93 operator const char *() const {
94 return msg;
95 }
96 };
97
98 gcc_const
99 static inline std::system_error
100 MakeSocketError(socket_error_t code, const char *msg) noexcept
101 {
102 #ifdef _WIN32
103 return MakeLastError(code, msg);
104 #else
105 return MakeErrno(code, msg);
106 #endif
107 }
108
109 gcc_pure
110 static inline std::system_error
111 MakeSocketError(const char *msg) noexcept
112 {
113 return MakeSocketError(GetSocketError(), msg);
114 }
115
116 #endif
SocketErrorMessage::SocketErrorMessage
SocketErrorMessage(socket_error_t code=GetSocketError()) noexcept
MakeErrno
static std::system_error MakeErrno(int code, const char *msg)
Definition: Error.hxx:121
IsSocketErrorInterruped
static gcc_const bool IsSocketErrorInterruped(socket_error_t code) noexcept
Definition: SocketError.hxx:58
MakeSocketError
static gcc_const std::system_error MakeSocketError(socket_error_t code, const char *msg) noexcept
Definition: SocketError.hxx:100
Compiler.h
IsSocketErrorAgain
static gcc_const bool IsSocketErrorAgain(socket_error_t code) noexcept
Definition: SocketError.hxx:47
gcc_const
#define gcc_const
Definition: Compiler.h:109
Error.hxx
GetSocketError
static gcc_pure socket_error_t GetSocketError() noexcept
Definition: SocketError.hxx:36
socket_error_t
int socket_error_t
Definition: SocketError.hxx:31
IsSocketErrorClosed
static gcc_const bool IsSocketErrorClosed(socket_error_t code) noexcept
Definition: SocketError.hxx:69
SocketErrorMessage
Helper class that formats a socket error message into a human-readable string.
Definition: SocketError.hxx:83
gcc_pure
#define gcc_pure
Definition: Compiler.h:116