Fix case where we initialize a packet we will never actually use.
[awesomized/libmemcached] / libmemcached / io.cc
index 60614f5cfdf08b21b7994671cf41bdacc727c6fa..1cd03769fadfe932fc6d69c1de8a3c3ed1168d69 100644 (file)
@@ -40,7 +40,7 @@
 #include <libmemcached/common.h>
 
 #ifdef HAVE_SYS_SOCKET_H
-#  include <sys/socket.h>
+# include <sys/socket.h>
 #endif
 
 void initialize_binary_request(org::libmemcached::Instance* server, protocol_binary_request_header& header)
@@ -148,10 +148,10 @@ static bool process_input_buffer(org::libmemcached::Instance* instance)
    */
     memcached_callback_st cb= *instance->root->callbacks;
 
-    memcached_set_processing_input((memcached_st *)instance->root, true);
+    memcached_set_processing_input((Memcached *)instance->root, true);
 
     char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
-    memcached_st *root= (memcached_st *)instance->root;
+    Memcached *root= (Memcached *)instance->root;
     memcached_return_t error= memcached_response(instance, buffer, sizeof(buffer), &root->result);
 
     memcached_set_processing_input(root, false);
@@ -212,8 +212,7 @@ static memcached_return_t io_wait(org::libmemcached::Instance* instance,
 
   if (instance->root->poll_timeout == 0) // Mimic 0 causes timeout behavior (not all platforms do this)
   {
-    instance->io_wait_count.timeouts++;
-    return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT);
+    return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_literal_param("poll_timeout() was set to zero"));
   }
 
   size_t loop_max= 5;
@@ -258,8 +257,7 @@ static memcached_return_t io_wait(org::libmemcached::Instance* instance,
 
     if (active_fd == 0)
     {
-      instance->io_wait_count.timeouts++;
-      return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT);
+      return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_literal_param("No active_fd were found"));
     }
 
     // Only an error should result in this code being called.
@@ -712,24 +710,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;
@@ -746,7 +755,7 @@ void org::libmemcached::Instance::close_socket()
   major_version= minor_version= micro_version= UINT8_MAX;
 }
 
-org::libmemcached::Instance* memcached_io_get_readable_server(memcached_st *memc, memcached_return_t&)
+org::libmemcached::Instance* memcached_io_get_readable_server(Memcached *memc, memcached_return_t&)
 {
 #define MAX_SERVERS_TO_POLL 100
   struct pollfd fds[MAX_SERVERS_TO_POLL];