Merge in fixes for UDP
[awesomized/libmemcached] / libmemcached / flush.cc
index 5895f030fa5d7e4133c7bc2f33bee49e4426d28b..f2c490ba6726c074f84a71e44bbc71a3c57a942b 100644 (file)
@@ -51,54 +51,68 @@ memcached_return_t memcached_flush(memcached_st *ptr, time_t expiration)
 
   LIBMEMCACHED_MEMCACHED_FLUSH_START();
   if (ptr->flags.binary_protocol)
+  {
     rc= memcached_flush_binary(ptr, expiration);
+  }
   else
+  {
     rc= memcached_flush_textual(ptr, expiration);
+  }
   LIBMEMCACHED_MEMCACHED_FLUSH_END();
+
   return rc;
 }
 
 static memcached_return_t memcached_flush_textual(memcached_st *ptr, 
                                                   time_t expiration)
 {
-  unlikely (memcached_server_count(ptr) == 0)
-    return MEMCACHED_NO_SERVERS;
+  bool reply= ptr->flags.no_reply ? false : true;
 
-  for (unsigned int x= 0; x < memcached_server_count(ptr); x++)
+  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+  int send_length;
+  if (expiration)
+  {
+    send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
+                          "flush_all %llu%s\r\n",
+                          (unsigned long long)expiration, reply ? "" :  " noreply");
+  }
+  else
   {
-    memcached_return_t rc;
-    char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+    send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
+                          "flush_all%s\r\n", reply ? "" : " noreply");
+  }
 
-    bool no_reply= ptr->flags.no_reply;
-    memcached_server_write_instance_st instance=
-      memcached_server_instance_fetch(ptr, x);
+  if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE or send_length < 0)
+  {
+    return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
+                               memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
+  }
 
-    int send_length;
-    if (expiration)
-    {
-      send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
-                            "flush_all %llu%s\r\n",
-                            (unsigned long long)expiration, no_reply ? " noreply" : "");
-    }
-    else
+
+  memcached_return_t rc= MEMCACHED_SUCCESS;
+  for (unsigned int x= 0; x < memcached_server_count(ptr); x++)
+  {
+    memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
+
+    memcached_return_t rrc= memcached_do(instance, buffer, (size_t)send_length, true);
+    if (rrc == MEMCACHED_SUCCESS and reply == true)
     {
-      send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
-                            "flush_all%s\r\n", no_reply ? " noreply" : "");
+      char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+      rrc= memcached_response(instance, response_buffer, sizeof(response_buffer), NULL);
     }
 
-    if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
+    if (memcached_failed(rrc))
     {
-      return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
-                                 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
+      // If an error has already been reported, then don't add to it
+      if (instance->error_messages == NULL)
+      {
+        memcached_set_error(*instance, rrc, MEMCACHED_AT);
+      }
+      rc= MEMCACHED_SOME_ERRORS;
     }
-
-    rc= memcached_do(instance, buffer, (size_t)send_length, true);
-
-    if (rc == MEMCACHED_SUCCESS && !no_reply)
-      (void)memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
   }
 
-  return MEMCACHED_SUCCESS;
+  return rc;
 }
 
 static memcached_return_t memcached_flush_binary(memcached_st *ptr, 
@@ -106,9 +120,6 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr,
 {
   protocol_binary_request_flush request= {};
 
-  unlikely (memcached_server_count(ptr) == 0)
-    return MEMCACHED_NO_SERVERS;
-
   request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ;
   request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
   request.message.header.request.extlen= 4;
@@ -116,10 +127,11 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr,
   request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
   request.message.body.expiration= htonl((uint32_t) expiration);
 
+  memcached_return_t rc= MEMCACHED_SUCCESS;
+
   for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
   {
-    memcached_server_write_instance_st instance=
-      memcached_server_instance_fetch(ptr, x);
+    memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
 
     if (ptr->flags.no_reply)
     {
@@ -130,21 +142,24 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr,
       request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
     }
 
-    if (memcached_do(instance, request.bytes, sizeof(request.bytes), true) != MEMCACHED_SUCCESS) 
+    memcached_return_t rrc;
+    if ((rrc= memcached_do(instance, request.bytes, sizeof(request.bytes), true)))
     {
+      memcached_set_error(*instance, rrc, MEMCACHED_AT);
       memcached_io_reset(instance);
-      return MEMCACHED_WRITE_FAILURE;
+      rc= MEMCACHED_SOME_ERRORS;
     } 
   }
 
   for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
   {
-    memcached_server_write_instance_st instance=
-      memcached_server_instance_fetch(ptr, x);
+    memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
 
     if (memcached_server_response_count(instance) > 0)
+    {
       (void)memcached_response(instance, NULL, 0, NULL);
+    }
   }
 
-  return MEMCACHED_SUCCESS;
+  return rc;
 }