C++: double underscores are reserved
[awesomized/libmemcached] / src / libmemcached / connect.cc
index 3648a242ec87303fbcb54af056a236e33b3f8d67..84617133741a9e5332e212ac50f29af4f2a8b563 100644 (file)
 */
 
 #include "libmemcached/common.h"
+#include "p9y/poll.hpp"
 
 #include <cassert>
 
-#ifndef SOCK_CLOEXEC
-#  define SOCK_CLOEXEC 0
-#endif
-
-#ifndef SOCK_NONBLOCK
-#  define SOCK_NONBLOCK 0
-#endif
-
-#ifndef FD_CLOEXEC
-#  define FD_CLOEXEC 0
-#endif
-
-#ifndef SO_NOSIGPIPE
-#  define SO_NOSIGPIPE 0
-#endif
-
-#ifndef TCP_NODELAY
-#  define TCP_NODELAY 0
-#endif
-
-#ifndef TCP_KEEPIDLE
-#  define TCP_KEEPIDLE 0
-#endif
 
 static memcached_return_t connect_poll(memcached_instance_st *server, const int connection_error) {
   struct pollfd fds[1];
@@ -49,7 +27,7 @@ static memcached_return_t connect_poll(memcached_instance_st *server, const int
 
   size_t loop_max = 5;
 
-  if (server->root->poll_timeout == 0) {
+  if (server->root->connect_timeout == 0) {
     return memcached_set_error(
         *server, MEMCACHED_TIMEOUT, MEMCACHED_AT,
         memcached_literal_param("The time to wait for a connection to be established was set to "
@@ -59,10 +37,10 @@ static memcached_return_t connect_poll(memcached_instance_st *server, const int
   while (--loop_max) // Should only loop on cases of ERESTART or EINTR
   {
     int number_of;
-    if ((number_of = poll(fds, 1, server->root->connect_timeout)) == -1) {
+    if ((number_of = poll(fds, 1, server->root->connect_timeout)) == SOCKET_ERROR) {
       int local_errno = get_socket_errno(); // We cache in case closesocket() modifies errno
       switch (local_errno) {
-#ifdef __linux__
+#ifdef HAVE_ERESTART
       case ERESTART:
 #endif
       case EINTR:
@@ -90,7 +68,7 @@ static memcached_return_t connect_poll(memcached_instance_st *server, const int
     }
 
     if (number_of == 0) {
-      if (connection_error == EINPROGRESS) {
+      if (connection_error != EALREADY) {
         int err;
         socklen_t len = sizeof(err);
         if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == -1) {
@@ -101,7 +79,7 @@ static memcached_return_t connect_poll(memcached_instance_st *server, const int
         }
 
         // If Zero, my hero, we just fail to a generic MEMCACHED_TIMEOUT error
-        if (err != 0) {
+        if (err) {
           return memcached_set_errno(
               *server, err, MEMCACHED_AT,
               memcached_literal_param("getsockopt() found the error from poll() after connect() "
@@ -138,7 +116,7 @@ static memcached_return_t connect_poll(memcached_instance_st *server, const int
     }
     assert(fds[0].revents & POLLOUT);
 
-    if (fds[0].revents & POLLOUT and connection_error == EINPROGRESS) {
+    if (fds[0].revents & POLLOUT and connection_error != EALREADY) {
       int err;
       socklen_t len = sizeof(err);
       if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == -1) {
@@ -170,7 +148,7 @@ static memcached_return_t set_hostinfo(memcached_instance_st *server) {
   char str_port[MEMCACHED_NI_MAXSERV] = {0};
   errno = 0;
   int length = snprintf(str_port, MEMCACHED_NI_MAXSERV, "%u", uint32_t(server->port()));
-  if (length >= MEMCACHED_NI_MAXSERV or length <= 0 or errno != 0) {
+  if (length >= MEMCACHED_NI_MAXSERV or length <= 0 or errno) {
     return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
                                memcached_literal_param("snprintf(NI_MAXSERV)"));
   }
@@ -263,7 +241,7 @@ static bool set_socket_options(memcached_instance_st *server) {
 #ifdef HAVE_FCNTL
   // If SOCK_CLOEXEC exists then we don't need to call the following
   if (SOCK_CLOEXEC == 0) {
-    if (FD_CLOEXEC != 0) {
+    if (FD_CLOEXEC) {
       int flags;
       do {
         flags = fcntl(server->fd, F_GETFD, 0);
@@ -397,11 +375,11 @@ static memcached_return_t unix_socket_connect(memcached_instance_st *server) {
 
   do {
     int type = SOCK_STREAM;
-    if (SOCK_CLOEXEC != 0) {
+    if (SOCK_CLOEXEC) {
       type |= SOCK_CLOEXEC;
     }
 
-    if (SOCK_NONBLOCK != 0) {
+    if (SOCK_NONBLOCK) {
       type |= SOCK_NONBLOCK;
     }
 
@@ -482,11 +460,11 @@ static memcached_return_t network_connect(memcached_instance_st *server) {
   /* Create the socket */
   while (server->address_info_next and server->fd == INVALID_SOCKET) {
     int type = server->address_info_next->ai_socktype;
-    if (SOCK_CLOEXEC != 0) {
+    if (SOCK_CLOEXEC) {
       type |= SOCK_CLOEXEC;
     }
 
-    if (SOCK_NONBLOCK != 0) {
+    if (SOCK_NONBLOCK) {
       type |= SOCK_NONBLOCK;
     }
 
@@ -521,6 +499,7 @@ static memcached_return_t network_connect(memcached_instance_st *server) {
 #if EWOULDBLOCK != EAGAIN
     case EWOULDBLOCK:
 #endif
+    case EAGAIN:
     case EINPROGRESS: // nonblocking mode - first return
     case EALREADY:    // nonblocking mode - subsequent returns
     {