25
25
// To match windows's constants
26
26
static constexpr int INVALID_SOCKET = -1 ;
27
27
static constexpr int SOCKET_ERROR = -1 ;
28
+
29
+ #include < arpa/inet.h>
30
+ #include < unistd.h>
28
31
#endif
29
32
30
33
static constexpr int BUFF_SIZE = 4096 ;
@@ -35,7 +38,6 @@ static constexpr int BUFF_SIZE = 4096;
35
38
#include < sstream>
36
39
#include < stdexcept>
37
40
38
-
39
41
// TODO add exceptions on error retunrs
40
42
// TODO throw custom exceptions on invalid status (eg: socket already connected)
41
43
namespace CppSockets {
@@ -44,8 +46,8 @@ namespace CppSockets {
44
46
{
45
47
socklen_t len = sizeof (int );
46
48
47
- Socket::getsockopt (sockfd, SOL_SOCKET, SO_TYPE, &m_type, &len);
48
- #if !defined (OS_APPLE) && !defined (OS_WINDOWS)
49
+ Socket::getsockopt (sockfd, SOL_SOCKET, SO_TYPE, (SockOptType *) &m_type, &len);
50
+ #ifdef OS_LINUX
49
51
Socket::getsockopt (sockfd, SOL_SOCKET, SO_DOMAIN, &m_domain, &len);
50
52
Socket::getsockopt (sockfd, SOL_SOCKET, SO_PROTOCOL, &m_protocol, &len);
51
53
#endif
@@ -95,7 +97,7 @@ namespace CppSockets {
95
97
close ();
96
98
}
97
99
98
- int Socket::getsockopt (int fd, int level, int optname, void *optval, socklen_t *optlen) {
100
+ int Socket::getsockopt (int fd, int level, int optname, SockOptType *optval, socklen_t *optlen) {
99
101
int ret = ::getsockopt (fd, level, optname, optval, optlen);
100
102
101
103
if (ret == SOCKET_ERROR) {
@@ -109,14 +111,12 @@ namespace CppSockets {
109
111
}
110
112
111
113
char *Socket::strerror (int err) {
112
- #ifdef OS_WINDOWS
113
- #else
114
114
return ::strerror (err);
115
- #endif
116
115
}
117
116
118
117
int Socket::get_errno () {
119
118
#ifdef OS_WINDOWS
119
+ return WSAGetLastError ();
120
120
#else
121
121
return errno;
122
122
#endif
@@ -170,14 +170,14 @@ namespace CppSockets {
170
170
int Socket::set_reuseaddr (bool value) {
171
171
int val = value;
172
172
173
- return this ->setsockopt (SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
173
+ return this ->setsockopt (SOL_SOCKET, SO_REUSEADDR, (SockOptType *) &val, sizeof (val));
174
174
}
175
175
176
- int Socket::getsockopt (int level, int optname, void *optval, socklen_t *optlen) {
176
+ int Socket::getsockopt (int level, int optname, SockOptType *optval, socklen_t *optlen) {
177
177
return this ->getsockopt (m_sockfd, level, optname, optval, optlen);
178
178
}
179
179
180
- int Socket::setsockopt (int level, int optname, const void *optval, socklen_t optlen) {
180
+ int Socket::setsockopt (int level, int optname, const SockOptType *optval, socklen_t optlen) {
181
181
int ret = ::setsockopt (m_sockfd, level, optname, optval, optlen);
182
182
183
183
if (ret < 0 ) {
@@ -257,6 +257,13 @@ namespace CppSockets {
257
257
}
258
258
259
259
void Socket::set_blocking (bool val) {
260
+ #ifdef OS_WINDOWS
261
+ u_long mode = val ? 0 : 1 ;
262
+ int result = ioctlsocket (m_sockfd, FIONBIO, &mode);
263
+ if (result != NO_ERROR) {
264
+ throw std::runtime_error (std::string (" Failed to change socket: " ) + Socket::strerror ());
265
+ }
266
+ #else
260
267
int flags = fcntl (m_sockfd, F_GETFL, 0 );
261
268
int ret = flags;
262
269
@@ -270,6 +277,7 @@ namespace CppSockets {
270
277
if (ret < 0 ) {
271
278
throw std::runtime_error (std::string (" Failed to change socket: " ) + Socket::strerror ());
272
279
}
280
+ #endif
273
281
}
274
282
275
283
RawSocketType Socket::get_fd () const {
0 commit comments