attempt to fix #12, #49 and #65
[awesomized/libmemcached] / libmemcached / purge.cc
index 38e4552a0dd97b698cbe88bb12cc9719f0a1dcb6..4dbe24d05cb090e733f6d1a07d2b197c92d28af3 100644 (file)
 
 #include <libmemcached/common.h>
 
+#define memcached_set_purging(__object, __value) ((__object)->state.is_purging= (__value))
 
-bool memcached_purge(memcached_server_write_instance_st ptr)
+class Purge
 {
-  memcached_st *root= (memcached_st *)ptr->root;
+public:
+  Purge(Memcached* arg) :
+    _memc(arg)
+  {
+    memcached_set_purging(_memc, true);
+  }
+
+  ~Purge()
+  {
+    memcached_set_purging(_memc, false);
+  }
+
+private:
+  Memcached* _memc;
+};
+
+class PollTimeout
+{
+public:
+  PollTimeout(Memcached* arg) :
+    _timeout(arg->poll_timeout),
+    _origin(arg->poll_timeout)
+  {
+    _origin = 2000;
+  }
+
+  ~PollTimeout()
+  {
+    _origin= _timeout;
+  }
+
+private:
+  int32_t _timeout;
+  int32_t& _origin;
+};
+
+bool memcached_purge(memcached_instance_st* ptr)
+{
+  Memcached *root= (Memcached *)ptr->root;
 
   if (memcached_is_purging(ptr->root) || /* already purging */
       (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark &&
@@ -57,7 +96,7 @@ bool memcached_purge(memcached_server_write_instance_st ptr)
     memcached_io_write and memcached_response may call memcached_purge
     so we need to be able stop any recursion.. 
   */
-  memcached_set_purging(root, true);
+  Purge set_purge(root);
 
   WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
   /* 
@@ -66,32 +105,29 @@ bool memcached_purge(memcached_server_write_instance_st ptr)
   */
   if (memcached_io_write(ptr) == false)
   {
-    memcached_set_purging(root, true);
-
+    memcached_io_reset(ptr);
     memcached_set_error(*ptr, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
     return false;
   }
   WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
 
   bool is_successful= true;
-  uint32_t no_msg= memcached_server_response_count(ptr) - 1;
-  if (no_msg > 0)
+  uint32_t no_msg= memcached_server_response_count(ptr);
+  if (no_msg > 1)
   {
     memcached_result_st result;
-    memcached_result_st *result_ptr;
 
     /*
      * We need to increase the timeout, because we might be waiting for
      * data to be sent from the server (the commands was in the output buffer
      * and just flushed
    */
-    const int32_t timeo= ptr->root->poll_timeout;
-    root->poll_timeout= 2000;
+    PollTimeout poll_timeout(ptr->root);
 
-    result_ptr= memcached_result_create(root, &result);
-    WATCHPOINT_ASSERT(result_ptr);
+    memcached_result_st* result_ptr= memcached_result_create(root, &result);
+    assert(result_ptr);
 
-    for (uint32_t x= 0; x < no_msg; x++)
+    for (uint32_t x= 0; x < no_msg - 1; x++)
     {
       memcached_result_reset(result_ptr);
       memcached_return_t rc= memcached_read_one_response(ptr, result_ptr);
@@ -103,7 +139,6 @@ bool memcached_purge(memcached_server_write_instance_st ptr)
       if (rc== MEMCACHED_PROTOCOL_ERROR or rc == MEMCACHED_UNKNOWN_READ_FAILURE or rc == MEMCACHED_READ_FAILURE)
       {
         WATCHPOINT_ERROR(rc);
-        memcached_io_reset(ptr);
         is_successful= false;
       }
 
@@ -124,9 +159,7 @@ bool memcached_purge(memcached_server_write_instance_st ptr)
     }
 
     memcached_result_free(result_ptr);
-    root->poll_timeout= timeo;
   }
-  memcached_set_purging(root, false);
 
   return is_successful;
 }