MPD  0.20.6
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 
35 static inline socket_error_t
37 {
38 #ifdef WIN32
39  return WSAGetLastError();
40 #else
41  return errno;
42 #endif
43 }
44 
46 static inline bool
48 {
49 #ifdef WIN32
50  return code == WSAEINPROGRESS;
51 #else
52  return code == EAGAIN;
53 #endif
54 }
55 
57 static inline bool
59 {
60 #ifdef WIN32
61  return code == WSAEINTR;
62 #else
63  return code == EINTR;
64 #endif
65 }
66 
68 static inline bool
70 {
71 #ifdef WIN32
72  return code == WSAECONNRESET;
73 #else
74  return code == EPIPE || code == ECONNRESET;
75 #endif
76 }
77 
84 #ifdef WIN32
85  char msg[256];
86 #else
87  const char *const msg;
88 #endif
89 
90 public:
91 #ifdef WIN32
93 #else
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
static gcc_pure socket_error_t GetSocketError()
Definition: SocketError.hxx:36
static std::system_error MakeErrno(int code, const char *msg)
Definition: Error.hxx:121
SocketErrorMessage(socket_error_t code=GetSocketError())
#define gcc_const
Definition: Compiler.h:109
static gcc_const bool IsSocketErrorAgain(socket_error_t code)
Definition: SocketError.hxx:47
static gcc_const bool IsSocketErrorInterruped(socket_error_t code)
Definition: SocketError.hxx:58
int socket_error_t
Definition: SocketError.hxx:31
static gcc_const std::system_error MakeSocketError(socket_error_t code, const char *msg)
static gcc_const bool IsSocketErrorClosed(socket_error_t code)
Definition: SocketError.hxx:69
Helper class that formats a socket error message into a human-readable string.
Definition: SocketError.hxx:83
#define gcc_pure
Definition: Compiler.h:116