diff --git a/net/except.cpp b/net/except.cpp index 398389d2..86851d02 100644 --- a/net/except.cpp +++ b/net/except.cpp @@ -20,6 +20,7 @@ #include "except.hpp" #include "../debug.hpp" +#include "../types/casts.hpp" //----------------------------------------------------------------------------- @@ -38,11 +39,15 @@ net::error::error (int _code): { CHECK (_code != 0); } +//----------------------------------------------------------------------------- std::string net::error::code_to_string (int code) { #ifdef __WIN32 char message[256]; - int output = FormatMessage (0, NULL, code, 0, message, sizeof (message), NULL); + + // It should be fine to signcast the code here as Windows guarantees all + // error messages are positive but appears to use int for compatibility + DWORD output = FormatMessage (0, NULL, sign_cast (code), 0, message, sizeof (message), NULL); CHECK_HARD (output != 0); return std::string (message); @@ -52,6 +57,7 @@ net::error::code_to_string (int code) { } +//----------------------------------------------------------------------------- void net::error::throw_code (int code) { #ifdef __WIN32 @@ -78,6 +84,12 @@ net::error::throw_code (int code) { } +void +net::error::throw_code (void) + { throw_code (last_code ()); } + + +//----------------------------------------------------------------------------- void net::error::try_code (int err) { if (err == 0) @@ -92,11 +104,7 @@ net::error::try_code (void) { try_code (last_code ()); } -void -net::error::throw_code (void) - { throw_code (last_code ()); } - - +//----------------------------------------------------------------------------- int net::error::last_code (void) { #ifdef __WIN32 @@ -107,6 +115,7 @@ net::error::last_code (void) { } +//----------------------------------------------------------------------------- template net::error_code::error_code (void): net::error (CODE) diff --git a/net/socket.cpp b/net/socket.cpp index f9982f7b..139c5574 100644 --- a/net/socket.cpp +++ b/net/socket.cpp @@ -25,8 +25,10 @@ #include "../log.hpp" #include "except.hpp" -#if !defined(HAVE_WINSOCK2_H) - #include +#if defined(HAVE_WINSOCK2_H) + #include +#else + #include #endif #include @@ -211,8 +213,8 @@ net::socket::listen (const address_type &_addr, unsigned int _b template typename net::socket::socket_ptr net::socket::accept (void) { - int newfd = ::accept (this->m_fd, NULL, 0); - if (newfd < 0) + socket_t newfd = ::accept (this->m_fd, NULL, 0); + if (newfd == INVALID_SOCKET) net::error::throw_code (); return socket_ptr(new socket (newfd)); diff --git a/net/types.hpp b/net/types.hpp index 30b61eee..f2c0bf17 100644 --- a/net/types.hpp +++ b/net/types.hpp @@ -41,6 +41,7 @@ namespace net { typedef SOCKET socket_t; #else typedef int socket_t; + const socket_t INVALID_SOCKET = -1; #endif /// Defines the protocol family, or communication domain of a socket (see `man socket'). diff --git a/string.cpp b/string.cpp index b5354f09..0c69d203 100644 --- a/string.cpp +++ b/string.cpp @@ -21,6 +21,8 @@ #include +#include "types/casts.hpp" + // TODO: Horribly inefficient, but God help you if you're relying on this // being efficient in the first place. bool @@ -32,23 +34,24 @@ strbegins (const char *restrict str, #if !defined(HAVE_STRNDUP) +#include + // Written by Niels Möller // Placed in the public domain -char *restrict +char * strndup (const char *restrict s, size_t size) { - char *r; - char *end = (char *)memchr(s, 0, size); + char *end = static_cast (memchr (s, 0, size)); if (end) // Length + 1 - size = end - s + 1; + size = sign_cast (end - s) + 1u; - char * r = malloc(size); + char * r = static_cast (malloc (size)); if (size) { - memcpy(r, s, size-1); + memcpy (r, s, size-1); r[size-1] = '\0'; } return r; diff --git a/string.hpp b/string.hpp index a885925d..530ee222 100644 --- a/string.hpp +++ b/string.hpp @@ -26,8 +26,12 @@ strbegins(const char *restrict str, #if !defined(HAVE_STRNDUP) -char *restrict -strndup (const char *restrict s, size_t size); +#include + +extern "C" { + char * + strndup (const char *restrict s, size_t size); +} #endif #endif // __UTIL_STRING_HPP