Skip to content

Commit 39041c4

Browse files
committed
* windows fixes
* remove error.hpp and move to socket.hpp
1 parent 9919b21 commit 39041c4

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ find_package(Threads)
1414
set(NETLIB_SRC
1515
src/netlib.hpp
1616
src/socket.hpp
17-
src/error.hpp
1817
src/client.hpp
1918
src/server.hpp
2019
src/service_resolver.hpp

src/error.hpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/service_resolver.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include "socket.hpp"
4-
#include <netdb.h>
54
#include <system_error>
65
#include <utility>
76

@@ -31,27 +30,31 @@ namespace netlib {
3130
switch (gai_res) {
3231
case EAI_AGAIN:{
3332
return { nullptr, std::errc::resource_unavailable_try_again};
34-
} break;
33+
}
3534
case EAI_FAMILY:{
3635
return { nullptr, std::errc::address_family_not_supported};
37-
} break;
36+
}
37+
#ifndef _WIN32
3838
case EAI_ADDRFAMILY:
39+
#endif
3940
case EAI_NODATA:
4041
case EAI_NONAME:
4142
case EAI_FAIL: {
4243
return { nullptr, std::errc::network_unreachable};
43-
} break;
44+
}
4445
case EAI_MEMORY: {
4546
return { nullptr, std::errc::not_enough_memory};
46-
} break;
47+
}
48+
#ifndef _WIN32
4749
case EAI_SYSTEM: {
4850
return { nullptr, socket_get_last_error()};
49-
} break;
51+
}
52+
#endif
5053
case EAI_SOCKTYPE:
5154
case EAI_SERVICE: //this is perhaps debatable?
5255
case EAI_BADFLAGS: {
5356
return { nullptr, std::errc::invalid_argument};
54-
} break;
57+
}
5558
default: {
5659
// we missuse the "not_supported" posix error type to indicate
5760
// that we don't recognize the error type

src/socket.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ using socklen_t = int32_t;
2222
using ssize_t = signed long long int;
2323
#else
2424
//headers
25-
#include "error.hpp"
2625
#include <arpa/inet.h>
2726
#include <chrono>
2827
#include <fcntl.h>
@@ -39,6 +38,14 @@ using socket_t = int32_t;
3938

4039
namespace netlib {
4140

41+
static std::error_condition socket_get_last_error(){
42+
#ifdef _WIN32
43+
return WASGetLastError();
44+
#else
45+
return std::make_error_condition(static_cast<std::errc>(errno));
46+
#endif
47+
}
48+
4249
enum class AddressFamily {IPv4 = AF_INET, IPv6 = AF_INET6, unspecified = AF_UNSPEC};
4350
enum class AddressProtocol {TCP = SOCK_STREAM, UDP = SOCK_DGRAM};
4451
enum class OperationClass {read = 1, write = 2, both = 3};
@@ -72,8 +79,8 @@ namespace netlib {
7279

7380
bool set_nonblocking(bool nonblocking = true) {
7481
#ifdef _WIN32
75-
uint32_t mode = static_cast<uint32_t>(nonblocking);
76-
return ioctlsocket(sockfd, FIONBIO, &mode) == 0;
82+
u_long mode = static_cast<u_long>(nonblocking);
83+
return ioctlsocket(_socket.value(), FIONBIO, &mode) == 0;
7784
#else
7885
return fcntl(_socket.value(), F_SETFL, fcntl(_socket.value(), F_GETFL, 0) | (nonblocking ? O_NONBLOCK : 0)) == 0;
7986
#endif
@@ -82,7 +89,7 @@ namespace netlib {
8289
bool set_reuseaddr(bool reuseaddr = true){
8390
#ifdef _WIN32
8491
int32_t val = static_cast<int32_t>(reuseaddr);
85-
return setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
92+
return setsockopt(_socket.value(), SOL_SOCKET, SO_REUSEADDR,
8693
reinterpret_cast<char*>(&val), sizeof(val)) == 0;
8794
#else
8895
auto mode = static_cast<int32_t>(reuseaddr);
@@ -107,7 +114,7 @@ namespace netlib {
107114
void close() {
108115
if (_socket) {
109116
#ifdef _WIN32
110-
closesocket(sockfd);
117+
closesocket(_socket.value());
111118
#else
112119
::close(_socket.value());
113120
#endif

0 commit comments

Comments
 (0)