Minor cleanup in confirming socket close.
authorBrian Aker <brian@tangent.org>
Wed, 23 Jan 2013 21:28:45 +0000 (16:28 -0500)
committerBrian Aker <brian@tangent.org>
Wed, 23 Jan 2013 21:28:45 +0000 (16:28 -0500)
14 files changed:
configure.ac
libmemcached/assert.hpp
libmemcached/byteorder.cc
libmemcached/common.h
libmemcached/connect.cc
libmemcached/error.cc
libmemcached/error.hpp
libmemcached/instance.hpp
libmemcached/io.cc
libmemcached/server_instance.h
libmemcached/string.cc
tests/libmemcached-1.0/mem_functions.cc
tests/libmemcached-1.0/pool.cc
win32/wrappers.h

index de966c8074273ba8253ba3ad34f9a7052b02dde0..2435c69e79ca17768bf6af405ec52cd4d6aaa50b 100644 (file)
@@ -113,6 +113,7 @@ AC_CHECK_HEADERS_ONCE([arpa/inet.h])
 AC_CHECK_HEADERS_ONCE([errno.h])
 AC_CHECK_HEADERS_ONCE([execinfo.h])
 AC_CHECK_HEADERS_ONCE([fcntl.h])
+AC_CHECK_HEADERS_ONCE([features.h])
 AC_CHECK_HEADERS_ONCE([fnmatch.h])
 AC_CHECK_HEADERS_ONCE([inttypes.h])
 AC_CHECK_HEADERS_ONCE([libintl.h])
@@ -133,6 +134,7 @@ AC_CHECK_HEADERS_ONCE([stdlib.h])
 AC_CHECK_HEADERS_ONCE([sys/socket.h])
 AC_CHECK_HEADERS_ONCE([sys/sysctl.h])
 AC_CHECK_HEADERS_ONCE([sys/time.h])
+AC_CHECK_HEADERS_ONCE([sys/types.h])
 AC_CHECK_HEADERS_ONCE([sys/un.h])
 AC_CHECK_HEADERS_ONCE([sys/wait.h])
 AC_CHECK_HEADERS_ONCE([syslog.h])
@@ -189,7 +191,16 @@ AM_CONDITIONAL([BUILD_CACHE],[test "x$build_cache" = "xyes"])
 
 AX_COMPILER_VENDOR
 
-AC_CHECK_TYPES([in_port_t])
+AC_CHECK_TYPES([in_port_t],,,[[
+                #ifdef HAVE_SYS_TYPES_H
+                # include <sys/types.h>
+                #endif
+                #ifdef HAVE_NETINET_IN_H
+                # include <netinet/in.h>
+                #endif
+                #ifdef HAVE_NETDB_H
+                # include <netdb.h>
+                #endif]])
 AC_CHECK_TYPES([ptrdiff_t])
 AC_HEADER_STDBOOL
 AC_TYPE_INT16_T
index d66b096fb4f5fcfb94400d29fa65878a0a5e67bf..cd4a194b2b1ce305e074e5f090ca82abead4d2ee 100644 (file)
@@ -2,7 +2,7 @@
  * 
  *  libmcachedd client library.
  *
- *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  *  All rights reserved.
  *
  *  Redistribution and use in source and binary forms, with or without
index eb37fda82dcee74251215479fbe572fc756297b6..9f11aa84b3e27bcc2c5003c1d62856fb42ee8c10 100644 (file)
@@ -46,7 +46,7 @@ static inline uint64_t swap64(uint64_t in)
   /* Little endian, flip the bytes around until someone makes a faster/better
    * way to do this. */
   uint64_t rv= 0;
-  for (uint8_t x= 0; x < 8; x++)
+  for (uint8_t x= 0; x < 8; ++x)
   {
     rv= (rv << 8) | (in & 0xff);
     in >>= 8;
index c7b84c94760d66ba97bb86cc833234abfc64e84e..4ab2680616ca3c8e9c94ac9d55ead63a7a1f21a7 100644 (file)
 #endif
 
 #include <libmemcached-1.0/memcached.h>
-#include <libmemcached/watchpoint.h>
-#include <libmemcached/is.h>
+#include "libmemcached/watchpoint.h"
+#include "libmemcached/is.h"
 typedef struct memcached_st Memcached;
 
-#ifdef __cplusplus
-# include "libmemcached/instance.hpp"
-#endif
-#include <libmemcached/server_instance.h>
-
 #ifdef HAVE_POLL_H
 # include <poll.h>
 #else
@@ -128,67 +123,57 @@ typedef struct memcached_st Memcached;
 #endif
 
 #ifdef __cplusplus
-extern "C" {
+org::libmemcached::Instance* memcached_instance_fetch(memcached_st *ptr, uint32_t server_key);
 #endif
 
-typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context);
-
+/* These are private not to be installed headers */
+#include "libmemcached/error.hpp"
+#include "libmemcached/memory.h"
+#include "libmemcached/io.h"
 #ifdef __cplusplus
-} // extern "C"
+# include "libmemcached/string.hpp"
+# include "libmemcached/memcached/protocol_binary.h"
+# include "libmemcached/io.hpp"
+# include "libmemcached/udp.hpp"
+# include "libmemcached/do.hpp"
+# include "libmemcached/socket.hpp"
+# include "libmemcached/connect.hpp"
+# include "libmemcached/allocators.hpp"
+# include "libmemcached/hash.hpp"
+# include "libmemcached/quit.hpp"
+# include "libmemcached/instance.hpp"
+# include "libmemcached/server_instance.h"
+# include "libmemcached/server.hpp"
+# include "libmemcached/flag.hpp"
+# include "libmemcached/behavior.hpp"
+# include "libmemcached/sasl.hpp"
+# include "libmemcached/server_list.hpp"
 #endif
 
+#include "libmemcached/internal.h"
+#include "libmemcached/array.h"
+#include "libmemcached/libmemcached_probes.h"
+#include "libmemcached/byteorder.h"
+#include "libmemcached/initialize_query.h"
 
 #ifdef __cplusplus
-org::libmemcached::Instance* memcached_instance_fetch(memcached_st *ptr, uint32_t server_key);
+# include "libmemcached/response.h"
 #endif
 
-memcached_return_t memcached_server_execute(memcached_st *ptr,
-                                            memcached_server_execute_fn callback,
-                                            void *context);
-/* These are private not to be installed headers */
-#include <libmemcached/error.hpp>
-#include <libmemcached/memory.h>
-#include <libmemcached/io.h>
-#ifdef __cplusplus
-#include <libmemcached/string.hpp>
-#include <libmemcached/memcached/protocol_binary.h>
-#include <libmemcached/io.hpp>
-#include <libmemcached/udp.hpp>
-#include <libmemcached/do.hpp>
-#include <libmemcached/socket.hpp>
-#include <libmemcached/connect.hpp>
-#include <libmemcached/allocators.hpp>
-#include <libmemcached/hash.hpp>
-#include <libmemcached/quit.hpp>
-#include <libmemcached/instance.hpp>
-#include <libmemcached/server.hpp>
-#include <libmemcached/flag.hpp>
-#include <libmemcached/behavior.hpp>
-#include <libmemcached/sasl.hpp>
-#include <libmemcached/server_list.hpp>
-#endif
-#include <libmemcached/internal.h>
-#include <libmemcached/array.h>
-#include <libmemcached/libmemcached_probes.h>
-#include <libmemcached/byteorder.h>
-#include <libmemcached/initialize_query.h>
-#ifdef __cplusplus
-#include <libmemcached/response.h>
-#endif
-#include <libmemcached/namespace.h>
-#include <libmemcached/virtual_bucket.h>
+#include "libmemcached/namespace.h"
+#include "libmemcached/virtual_bucket.h"
 
 #ifdef __cplusplus
-#include <libmemcached/backtrace.hpp>
-#include <libmemcached/assert.hpp>
-#include <libmemcached/server.hpp>
-#include <libmemcached/key.hpp>
-#include <libmemcached/encoding_key.h>
-#include <libmemcached/result.h>
-#include <libmemcached/version.hpp>
+# include "libmemcached/backtrace.hpp"
+# include "libmemcached/assert.hpp"
+# include "libmemcached/server.hpp"
+# include "libmemcached/key.hpp"
+# include "libmemcached/encoding_key.h"
+# include "libmemcached/result.h"
+# include "libmemcached/version.hpp"
 #endif
 
-#include <libmemcached/continuum.hpp>
+#include "libmemcached/continuum.hpp"
 
 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
 
index d2580b5ef82879a349d212c2e0f160932b9c7317..1efeee57aee003ecd2a2e9538eef693d5e57ced7 100644 (file)
@@ -118,8 +118,7 @@ static memcached_return_t connect_poll(org::libmemcached::Instance* server)
           }
 
           assert_msg(server->fd != INVALID_SOCKET, "poll() was passed an invalid file descriptor");
-          (void)closesocket(server->fd);
-          server->fd= INVALID_SOCKET;
+          server->reset_socket();
           server->state= MEMCACHED_SERVER_STATE_NEW;
 
           return memcached_set_errno(*server, local_errno, MEMCACHED_AT);
@@ -463,7 +462,7 @@ static memcached_return_t unix_socket_connect(org::libmemcached::Instance* serve
       type|= SOCK_NONBLOCK;
     }
 
-    if ((server->fd= socket(AF_UNIX, type, 0)) < 0)
+    if ((server->fd= socket(AF_UNIX, type, 0)) == -1)
     {
       return memcached_set_errno(*server, errno, NULL);
     }
@@ -474,7 +473,7 @@ static memcached_return_t unix_socket_connect(org::libmemcached::Instance* serve
     servAddr.sun_family= AF_UNIX;
     strncpy(servAddr.sun_path, server->hostname, sizeof(servAddr.sun_path)); /* Copy filename */
 
-    if (connect(server->fd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
+    if (connect(server->fd, (struct sockaddr *)&servAddr, sizeof(servAddr)) == -1)
     {
       switch (errno)
       {
@@ -484,22 +483,19 @@ static memcached_return_t unix_socket_connect(org::libmemcached::Instance* serve
         break;
 
       case EINTR:
-        (void)closesocket(server->fd);
-        server->fd= INVALID_SOCKET;
+        server->reset_socket();
         continue;
 
       case EISCONN: /* We were spinning waiting on connect */
         {
           assert(0); // Programmer error
-          (void)closesocket(server->fd);
-          server->fd= INVALID_SOCKET;
+          server->reset_socket();
           continue;
         }
 
       default:
         WATCHPOINT_ERRNO(errno);
-        (void)closesocket(server->fd);
-        server->fd= INVALID_SOCKET;
+        server->reset_socket();
         return memcached_set_errno(*server, errno, MEMCACHED_AT);
       }
     }
@@ -575,7 +571,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server)
 
     if (set_socket_options(server) == false)
     {
-      (void)closesocket(server->fd);
+      server->reset_socket();
       return MEMCACHED_CONNECTION_FAILURE;
     }
 
@@ -624,8 +620,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server)
 
     case EINTR: // Special case, we retry ai_addr
       WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET);
-      (void)closesocket(server->fd);
-      server->fd= INVALID_SOCKET;
+      server->reset_socket();
       continue;
 
     case ECONNREFUSED:
@@ -636,8 +631,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server)
     }
 
     WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET);
-    (void)closesocket(server->fd);
-    server->fd= INVALID_SOCKET;
+    server->reset_socket();
     server->address_info_next= server->address_info_next->ai_next;
   }
 
@@ -645,11 +639,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server)
 
   if (timeout_error_occured)
   {
-    if (server->fd != INVALID_SOCKET)
-    {
-      (void)closesocket(server->fd);
-      server->fd= INVALID_SOCKET;
-    }
+    server->reset_socket();
   }
 
   WATCHPOINT_STRING("Never got a good file descriptor");
@@ -785,8 +775,7 @@ static memcached_return_t _memcached_connect(org::libmemcached::Instance* server
         if (memcached_failed(rc) and server->fd != INVALID_SOCKET)
         {
           WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET);
-          (void)closesocket(server->fd);
-          server->fd= INVALID_SOCKET;
+          server->reset_socket();
         }
       }
     }
index 8532678088a7d6f3f9f1133a78e0036c6d8fe8cc..f4ce85d0c70498383518502eb8d46950de0091a3 100644 (file)
@@ -36,7 +36,9 @@
  */
 
 #include <libmemcached/common.h>
+#include <cerrno>
 #include <cstdarg>
+#include <cstdio>
 
 #define MAX_ERROR_LENGTH 2048
 struct memcached_error_t
@@ -139,7 +141,7 @@ static void _set(memcached_st& memc, memcached_string_t *str, memcached_return_t
 
   if (local_errno)
   {
-#ifdef STRERROR_R_CHAR_P
+#if defined(STRERROR_R_CHAR_P) && STRERROR_R_CHAR_P
     errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg));
 #elif defined(HAVE_STRERROR_R) && HAVE_STRERROR_R
     strerror_r(local_errno, errmsg, sizeof(errmsg));
index 5b82d13ac48362050f920a95b0f91ea127c0b40a..cdbc6ab15e0718e684d7a3d5b6073bf50b34d390 100644 (file)
@@ -2,7 +2,7 @@
  * 
  *  LibMemcached
  *
- *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  *  All rights reserved.
  *
  *  Redistribution and use in source and binary forms, with or without
@@ -35,7 +35,7 @@
  *
  */
 
-#include <libmemcached/common.h>
+#include "libmemcached/common.h"
 
 #pragma once
 
index 8f66e96d044b27e467226df0bc1f078e27444377..bab2f9ea9f776ae2f358dc45237725d5d754f24f 100644 (file)
@@ -91,6 +91,7 @@ struct Instance {
 
   void start_close_socket();
   void close_socket();
+  void reset_socket();
 
   uint32_t response_count() const
   {
index 20233c7dc665cc668f524dfe56d03e25acac25a9..0660fe9db25fd1bad4b425c814078df4004c3159 100644 (file)
@@ -712,24 +712,35 @@ void org::libmemcached::Instance::start_close_socket()
   }
 }
 
+void org::libmemcached::Instance::reset_socket()
+{
+  if (fd != INVALID_SOCKET)
+  {
+    (void)closesocket(fd);
+    fd= INVALID_SOCKET;
+  }
+}
+
 void org::libmemcached::Instance::close_socket()
 {
   if (fd != INVALID_SOCKET)
   {
+    int shutdown_options= SHUT_RD;
+    if (options.is_shutting_down == false)
+    {
+      shutdown_options= SHUT_RDWR;
+    }
+
     /* in case of death shutdown to avoid blocking at close() */
-    if (shutdown(fd, SHUT_RD) == SOCKET_ERROR and get_socket_errno() != ENOTCONN)
+    if (shutdown(fd, shutdown_options) == SOCKET_ERROR and get_socket_errno() != ENOTCONN)
     {
       WATCHPOINT_NUMBER(fd);
       WATCHPOINT_ERRNO(get_socket_errno());
       WATCHPOINT_ASSERT(get_socket_errno());
     }
 
-    if (closesocket(fd) == SOCKET_ERROR)
-    {
-      WATCHPOINT_ERRNO(get_socket_errno());
-    }
+    reset_socket();
     state= MEMCACHED_SERVER_STATE_NEW;
-    fd= INVALID_SOCKET;
   }
 
   state= MEMCACHED_SERVER_STATE_NEW;
index c840384ab3a4216797eebfdc08e48a8c94a60a95..3b69b77b7f25ea488108df7b26ec55cd4d4e0d84 100644 (file)
 #pragma once
 
 #ifdef __cplusplus
+namespace org { namespace libmemcached { struct Instance; } }
 typedef struct org::libmemcached::Instance* memcached_server_write_instance_st;
 #else
 typedef void* memcached_server_write_instance_st;
 #endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context);
+
+memcached_return_t memcached_server_execute(memcached_st *ptr,
+                                            memcached_server_execute_fn callback,
+                                            void *context);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+
index 7826bd10d8840919b333d5d4e9a904db218534ad..ce96b98cba501ef57ff4552ae8045e44d11470ea 100644 (file)
@@ -115,7 +115,7 @@ memcached_string_st *memcached_string_create(Memcached *memc, memcached_string_s
     return NULL;
   }
 
-  self->options.is_initialized= true;
+  memcached_set_initialized(self, true);
 
   WATCHPOINT_ASSERT(self->string == self->end);
 
index 89eca0724342a46e06c32c22ad382e6d3ab9c3b1..44143983555b429fb7f52d1dbdcb959c1ed3a940 100644 (file)
@@ -39,7 +39,7 @@
 #include <libtest/test.hpp>
 
 #if defined(HAVE_LIBUUID) && HAVE_LIBUUID
-#include <uuid/uuid.h>
+# include <uuid/uuid.h>
 #endif
 
 /*
@@ -47,8 +47,8 @@
 */
 
 #include <libmemcached-1.0/memcached.h>
-#include <libmemcached/is.h>
-#include <libmemcached/server_instance.h>
+#include "libmemcached/is.h"
+#include "libmemcached/server_instance.h"
 
 #include <libhashkit-1.0/hashkit.h>
 
index 30cc9660a06e7ad1c244d50c01c1ee7464170d1a..050d29277112287fbbab367535af463c8bd718e2 100644 (file)
@@ -504,15 +504,9 @@ test_return_t regression_bug_962815(memcached_st *memc)
     test_compare(0, pthread_join(pid[x], NULL));
   }
 
-  if (pool)
-  {
-    memcached_pool_destroy(pool);
-  }
+  memcached_pool_destroy(pool);
 
-  if (master)
-  {
-    memcached_free(master);
-  }
+  memcached_free(master);
 
   return TEST_SUCCESS;
 }
index 976303cb32ef00b85f3c37bf2accf405f097cab5..5bf325e9b31f68d431d1d30aa27370a17417acc3 100644 (file)
@@ -9,8 +9,7 @@
  *          to avoid a bunch of ifdefs in the rest of the code
  *
  */
-#ifndef WIN32_WRAPPERS_H
-#define WIN32_WRAPPERS_H 1
+#pragma once 
 
 #include <inttypes.h>
 
@@ -76,5 +75,3 @@
 #define waitpid(a,b,c) (-1)
 #define fnmatch(a,b,c) (-1)
 #define sleep(a) Sleep(a*1000)
-
-#endif /* WIN32_WRAPPERS_H */