MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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()
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)
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)
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)
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 #ifdef WIN32
92 explicit SocketErrorMessage(socket_error_t code=GetSocketError());
93 #else
94 explicit SocketErrorMessage(socket_error_t code=GetSocketError());
95 #endif
96
97 operator const char *() const {
98 return msg;
99 }
100 };
101
102 gcc_const
103 static inline std::system_error
104 MakeSocketError(socket_error_t code, const char *msg)
105 {
106 #ifdef WIN32
107 return MakeLastError(code, msg);
108 #else
109 return MakeErrno(code, msg);
110 #endif
111 }
112
113 gcc_pure
114 static inline std::system_error
115 MakeSocketError(const char *msg)
116 {
117 return MakeSocketError(GetSocketError(), msg);
118 }
119
120 #endif
GetSocketError
static gcc_pure socket_error_t GetSocketError()
Definition: SocketError.hxx:36
MakeErrno
static std::system_error MakeErrno(int code, const char *msg)
Definition: Error.hxx:121
Compiler.h
SocketErrorMessage::SocketErrorMessage
SocketErrorMessage(socket_error_t code=GetSocketError())
gcc_const
#define gcc_const
Definition: Compiler.h:109
IsSocketErrorAgain
static gcc_const bool IsSocketErrorAgain(socket_error_t code)
Definition: SocketError.hxx:47
Error.hxx
IsSocketErrorInterruped
static gcc_const bool IsSocketErrorInterruped(socket_error_t code)
Definition: SocketError.hxx:58
socket_error_t
int socket_error_t
Definition: SocketError.hxx:31
MakeSocketError
static gcc_const std::system_error MakeSocketError(socket_error_t code, const char *msg)
Definition: SocketError.hxx:104
IsSocketErrorClosed
static gcc_const bool IsSocketErrorClosed(socket_error_t code)
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