Skip to content

Commit 75285e8

Browse files
author
Joshua Peraza
committed
Define and use kInvalidSocket
1 parent e6abebf commit 75285e8

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

httplib.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#endif
4040

4141
typedef SOCKET socket_t;
42+
constexpr socket_t kInvalidSocket = INVALID_SOCKET;
4243
#else
4344
#include <pthread.h>
4445
#include <unistd.h>
@@ -51,6 +52,7 @@ typedef SOCKET socket_t;
5152
#include <sys/select.h>
5253

5354
typedef int socket_t;
55+
constexpr socket_t kInvalidSocket = -1;
5456
#endif
5557

5658
#include <fstream>
@@ -560,13 +562,13 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0)
560562
auto service = std::to_string(port);
561563

562564
if (getaddrinfo(host, service.c_str(), &hints, &result)) {
563-
return -1;
565+
return kInvalidSocket;
564566
}
565567

566568
for (auto rp = result; rp; rp = rp->ai_next) {
567569
// Create a socket
568570
auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
569-
if (sock == -1) {
571+
if (sock == kInvalidSocket) {
570572
continue;
571573
}
572574

@@ -584,7 +586,7 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0)
584586
}
585587

586588
freeaddrinfo(result);
587-
return -1;
589+
return kInvalidSocket;
588590
}
589591

590592
inline void set_nonblocking(socket_t sock, bool nonblocking)
@@ -1415,7 +1417,7 @@ inline std::string SocketStream::get_remote_addr() {
14151417
inline Server::Server(HttpVersion http_version)
14161418
: http_version_(http_version)
14171419
, is_running_(false)
1418-
, svr_sock_(-1)
1420+
, svr_sock_(kInvalidSocket)
14191421
, running_threads_(0)
14201422
{
14211423
#ifndef _WIN32
@@ -1500,10 +1502,10 @@ inline bool Server::is_running() const
15001502
inline void Server::stop()
15011503
{
15021504
if (is_running_) {
1503-
assert(svr_sock_ != -1);
1505+
assert(svr_sock_ != kInvalidSocket);
15041506
detail::shutdown_socket(svr_sock_);
15051507
detail::close_socket(svr_sock_);
1506-
svr_sock_ = -1;
1508+
svr_sock_ = kInvalidSocket;
15071509
}
15081510
}
15091511

@@ -1624,7 +1626,7 @@ inline int Server::bind_internal(const char* host, int port, int socket_flags)
16241626
}
16251627

16261628
svr_sock_ = create_server_socket(host, port, socket_flags);
1627-
if (svr_sock_ == -1) {
1629+
if (svr_sock_ == kInvalidSocket) {
16281630
return -1;
16291631
}
16301632

@@ -1650,7 +1652,7 @@ inline bool Server::listen_internal()
16501652
auto val = detail::select_read(svr_sock_, 0, 100000);
16511653

16521654
if (val == 0) { // Timeout
1653-
if (svr_sock_ == -1) {
1655+
if (svr_sock_ == kInvalidSocket) {
16541656
// The server socket was closed by 'stop' method.
16551657
break;
16561658
}
@@ -1659,8 +1661,8 @@ inline bool Server::listen_internal()
16591661

16601662
socket_t sock = accept(svr_sock_, NULL, NULL);
16611663

1662-
if (sock == -1) {
1663-
if (svr_sock_ != -1) {
1664+
if (sock == kInvalidSocket) {
1665+
if (svr_sock_ != kInvalidSocket) {
16641666
detail::close_socket(svr_sock_);
16651667
ret = false;
16661668
} else {
@@ -1894,7 +1896,7 @@ inline bool Client::send(Request& req, Response& res)
18941896
}
18951897

18961898
auto sock = create_client_socket();
1897-
if (sock == -1) {
1899+
if (sock == kInvalidSocket) {
18981900
return false;
18991901
}
19001902

0 commit comments

Comments
 (0)