Browse Source

Define and use kInvalidSocket

Joshua Peraza 7 years ago
parent
commit
75285e8713
1 changed files with 13 additions and 11 deletions
  1. 13 11
      httplib.h

+ 13 - 11
httplib.h

@@ -39,6 +39,7 @@
 #endif
 #endif
 
 
 typedef SOCKET socket_t;
 typedef SOCKET socket_t;
+constexpr socket_t kInvalidSocket = INVALID_SOCKET;
 #else
 #else
 #include <pthread.h>
 #include <pthread.h>
 #include <unistd.h>
 #include <unistd.h>
@@ -51,6 +52,7 @@ typedef SOCKET socket_t;
 #include <sys/select.h>
 #include <sys/select.h>
 
 
 typedef int socket_t;
 typedef int socket_t;
+constexpr socket_t kInvalidSocket = -1;
 #endif
 #endif
 
 
 #include <fstream>
 #include <fstream>
@@ -560,13 +562,13 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0)
     auto service = std::to_string(port);
     auto service = std::to_string(port);
 
 
     if (getaddrinfo(host, service.c_str(), &hints, &result)) {
     if (getaddrinfo(host, service.c_str(), &hints, &result)) {
-        return -1;
+        return kInvalidSocket;
     }
     }
 
 
     for (auto rp = result; rp; rp = rp->ai_next) {
     for (auto rp = result; rp; rp = rp->ai_next) {
        // Create a socket
        // Create a socket
        auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
-       if (sock == -1) {
+       if (sock == kInvalidSocket) {
           continue;
           continue;
        }
        }
 
 
@@ -584,7 +586,7 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0)
     }
     }
 
 
     freeaddrinfo(result);
     freeaddrinfo(result);
-    return -1;
+    return kInvalidSocket;
 }
 }
 
 
 inline void set_nonblocking(socket_t sock, bool nonblocking)
 inline void set_nonblocking(socket_t sock, bool nonblocking)
@@ -1415,7 +1417,7 @@ inline std::string SocketStream::get_remote_addr() {
 inline Server::Server(HttpVersion http_version)
 inline Server::Server(HttpVersion http_version)
     : http_version_(http_version)
     : http_version_(http_version)
     , is_running_(false)
     , is_running_(false)
-    , svr_sock_(-1)
+    , svr_sock_(kInvalidSocket)
     , running_threads_(0)
     , running_threads_(0)
 {
 {
 #ifndef _WIN32
 #ifndef _WIN32
@@ -1500,10 +1502,10 @@ inline bool Server::is_running() const
 inline void Server::stop()
 inline void Server::stop()
 {
 {
     if (is_running_) {
     if (is_running_) {
-        assert(svr_sock_ != -1);
+        assert(svr_sock_ != kInvalidSocket);
         detail::shutdown_socket(svr_sock_);
         detail::shutdown_socket(svr_sock_);
         detail::close_socket(svr_sock_);
         detail::close_socket(svr_sock_);
-        svr_sock_ = -1;
+        svr_sock_ = kInvalidSocket;
     }
     }
 }
 }
 
 
@@ -1624,7 +1626,7 @@ inline int Server::bind_internal(const char* host, int port, int socket_flags)
     }
     }
 
 
     svr_sock_ = create_server_socket(host, port, socket_flags);
     svr_sock_ = create_server_socket(host, port, socket_flags);
-    if (svr_sock_ == -1) {
+    if (svr_sock_ == kInvalidSocket) {
         return -1;
         return -1;
     }
     }
 
 
@@ -1650,7 +1652,7 @@ inline bool Server::listen_internal()
         auto val = detail::select_read(svr_sock_, 0, 100000);
         auto val = detail::select_read(svr_sock_, 0, 100000);
 
 
         if (val == 0) { // Timeout
         if (val == 0) { // Timeout
-            if (svr_sock_ == -1) {
+            if (svr_sock_ == kInvalidSocket) {
                 // The server socket was closed by 'stop' method.
                 // The server socket was closed by 'stop' method.
                 break;
                 break;
             }
             }
@@ -1659,8 +1661,8 @@ inline bool Server::listen_internal()
 
 
         socket_t sock = accept(svr_sock_, NULL, NULL);
         socket_t sock = accept(svr_sock_, NULL, NULL);
 
 
-        if (sock == -1) {
-            if (svr_sock_ != -1) {
+        if (sock == kInvalidSocket) {
+            if (svr_sock_ != kInvalidSocket) {
                 detail::close_socket(svr_sock_);
                 detail::close_socket(svr_sock_);
                 ret = false;
                 ret = false;
             } else {
             } else {
@@ -1894,7 +1896,7 @@ inline bool Client::send(Request& req, Response& res)
     }
     }
 
 
     auto sock = create_client_socket();
     auto sock = create_client_socket();
-    if (sock == -1) {
+    if (sock == kInvalidSocket) {
         return false;
         return false;
     }
     }