Merge Trunk
[awesomized/libmemcached] / libmemcached / response.cc
index 4035a5b952670f4c3829f919b9e13f2f209a5b56..ff1d77b1bd7954a4f69bf7c7b0d603baccf138c4 100644 (file)
@@ -62,9 +62,9 @@ memcached_return_t memcached_read_one_response(memcached_server_write_instance_s
   else
     rc= textual_read_one_response(ptr, buffer, buffer_length, result);
 
-  unlikely(rc == MEMCACHED_UNKNOWN_READ_FAILURE ||
-           rc == MEMCACHED_PROTOCOL_ERROR ||
-           rc == MEMCACHED_CLIENT_ERROR ||
+  unlikely(rc == MEMCACHED_UNKNOWN_READ_FAILURE or
+           rc == MEMCACHED_PROTOCOL_ERROR or
+           rc == MEMCACHED_CLIENT_ERROR or
            rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE)
     memcached_io_reset(ptr);
 
@@ -111,18 +111,15 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st
                                               char *buffer,
                                               memcached_result_st *result)
 {
-  memcached_return_t rc= MEMCACHED_SUCCESS;
   char *string_ptr;
   char *end_ptr;
   char *next_ptr;
   size_t value_length;
   size_t to_read;
-  char *value_ptr;
   ssize_t read_length= 0;
-  memcached_return_t rrc;
 
   if (ptr->root->flags.use_udp)
-    return MEMCACHED_NOT_SUPPORTED;
+    return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
 
   WATCHPOINT_ASSERT(ptr->root);
   end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE;
@@ -197,25 +194,33 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st
     goto read_error;
 
   /* We add two bytes so that we can walk the \r\n */
-  rc= memcached_string_check(&result->value, value_length+2);
-  if (rc != MEMCACHED_SUCCESS)
+  if (memcached_failed(memcached_string_check(&result->value, value_length +2)))
   {
     value_length= 0;
-    return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+    return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
   }
 
-  value_ptr= memcached_string_value_mutable(&result->value);
-  /*
-    We read the \r\n into the string since not doing so is more
-    cycles then the waster of memory to do so.
-
-    We are null terminating through, which will most likely make
-    some people lazy about using the return length.
-  */
-  to_read= (value_length) + 2;
-  rrc= memcached_io_read(ptr, value_ptr, to_read, &read_length);
-  if (rrc != MEMCACHED_SUCCESS)
-    return rrc;
+  {
+    char *value_ptr= memcached_string_value_mutable(&result->value);
+    /*
+      We read the \r\n into the string since not doing so is more
+      cycles then the waster of memory to do so.
+
+      We are null terminating through, which will most likely make
+      some people lazy about using the return length.
+    */
+    to_read= (value_length) + 2;
+    memcached_return_t rrc= memcached_io_read(ptr, value_ptr, to_read, &read_length);
+    if (memcached_failed(rrc) and rrc == MEMCACHED_IN_PROGRESS)
+    {
+      memcached_quit_server(ptr, true);
+      return memcached_set_error(*ptr, rrc, MEMCACHED_AT);
+    }
+    else if (memcached_failed(rrc))
+    {
+      return rrc;
+    }
+  }
 
   if (read_length != (ssize_t)(value_length + 2))
   {
@@ -227,7 +232,7 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st
     char *char_ptr;
     char_ptr= memcached_string_value_mutable(&result->value);;
     char_ptr[value_length]= 0;
-    char_ptr[value_length + 1]= 0;
+    char_ptr[value_length +1]= 0;
     memcached_string_set_length(&result->value, value_length);
   }
 
@@ -244,8 +249,10 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta
                                                     memcached_result_st *result)
 {
   memcached_return_t rc= memcached_io_readline(ptr, buffer, buffer_length);
-  if (rc != MEMCACHED_SUCCESS)
+  if (memcached_failed(rc))
+  {
     return rc;
+  }
 
   switch(buffer[0])
   {
@@ -386,7 +393,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan
   header.response.keylen= ntohs(header.response.keylen);
   header.response.status= ntohs(header.response.status);
   header.response.bodylen= ntohl(header.response.bodylen);
-  header.response.cas= ntohll(header.response.cas);
+  header.response.cas= memcached_ntohll(header.response.cas);
   uint32_t bodylen= header.response.bodylen;
 
   if (header.response.status == PROTOCOL_BINARY_RESPONSE_SUCCESS ||
@@ -452,7 +459,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan
           return MEMCACHED_UNKNOWN_READ_FAILURE;
         }
 
-        val= ntohll(val);
+        val= memcached_ntohll(val);
         memcpy(buffer, &val, sizeof(val));
       }
       break;
@@ -552,7 +559,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan
       if ((rc= memcached_safe_read(ptr, hole, nr)) != MEMCACHED_SUCCESS)
       {
         WATCHPOINT_ERROR(rc);
-        return MEMCACHED_UNKNOWN_READ_FAILURE;
+        return memcached_set_error(*ptr, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT);
       }
       bodylen-= (uint32_t) nr;
     }