OSX uses a different type in sendmesg() it seems.
[awesomized/libmemcached] / libmemcached / do.cc
index 854c611497c36b60a2286450cbb2b05f75861a54..db533680f77193f80ea3dd6ce37eb65bcb1f3639 100644 (file)
 
 #include <libmemcached/common.h>
 
-memcached_return_t memcached_do(memcached_server_write_instance_st ptr, const void *command,
-                                size_t command_length, bool with_flush)
+memcached_return_t memcached_vdo(org::libmemcached::Instance* instance,
+                                 libmemcached_io_vector_st vector[],
+                                 const size_t count,
+                                 const bool with_flush)
 {
   memcached_return_t rc;
-  ssize_t sent_length;
 
-  WATCHPOINT_ASSERT(command_length);
-  WATCHPOINT_ASSERT(command);
+  assert_msg(vector, "Invalid vector passed");
 
-  if (memcached_failed(rc= memcached_connect(ptr)))
+  if (memcached_failed(rc= memcached_connect(instance)))
   {
-    WATCHPOINT_ASSERT(rc == memcached_last_error(ptr->root));
     WATCHPOINT_ERROR(rc);
+    assert_msg(instance->error_messages, "memcached_connect() returned an error but the Instance showed none.");
     return rc;
   }
 
@@ -32,68 +32,65 @@ memcached_return_t memcached_do(memcached_server_write_instance_st ptr, const vo
   ** before they start writing, if there is any data in buffer, clear it out,
   ** otherwise we might get a partial write.
   **/
-  if (ptr->type == MEMCACHED_CONNECTION_UDP && with_flush && ptr->write_buffer_offset > UDP_DATAGRAM_HEADER_LENGTH)
+  if (memcached_is_udp(instance->root))
   {
-    memcached_io_write(ptr, NULL, 0, true);
+    if (vector[0].buffer or vector[0].length)
+    {
+      return memcached_set_error(*instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, 
+                                 memcached_literal_param("UDP messages was attempted, but vector was not setup for it"));
+    }
+
+    struct msghdr msg;
+    memset(&msg, 0, sizeof(msg));
+
+    increment_udp_message_id(instance);
+    vector[0].buffer= instance->write_buffer;
+    vector[0].length= UDP_DATAGRAM_HEADER_LENGTH;
+
+    msg.msg_iov= (struct iovec*)vector;
+#if defined(TARGET_OS_OSX) && TARGET_OS_OSX
+    msg.msg_iovlen= int(count);
+#else
+    msg.msg_iovlen= count;
+#endif
+
+    uint32_t retry= 5;
+    while (--retry)
+    {
+      ssize_t sendmsg_length= ::sendmsg(instance->fd, &msg, 0);
+      if (sendmsg_length > 0)
+      {
+        break;
+      }
+      else if (sendmsg_length < 0)
+      {
+        if (errno == EMSGSIZE)
+        {
+          return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
+        }
+
+        return memcached_set_errno(*instance, errno, MEMCACHED_AT);
+      }
+    }
+
+    return MEMCACHED_SUCCESS;
   }
 
-  sent_length= memcached_io_write(ptr, command, command_length, with_flush);
-
-  if (sent_length == -1 || (size_t)sent_length != command_length)
-  {
-    rc= MEMCACHED_WRITE_FAILURE;
-  }
-  else if ((ptr->root->flags.no_reply) == 0)
-  {
-    memcached_server_response_increment(ptr);
-  }
-
-  return rc;
-}
-
-memcached_return_t memcached_vdo(memcached_server_write_instance_st ptr,
-                                 const struct libmemcached_io_vector_st *vector, size_t count,
-                                 bool with_flush)
-{
-  memcached_return_t rc;
-  ssize_t sent_length;
-
-  WATCHPOINT_ASSERT(count);
-  WATCHPOINT_ASSERT(vector);
-
-  if ((rc= memcached_connect(ptr)) != MEMCACHED_SUCCESS)
+  bool sent_success= memcached_io_writev(instance, vector, count, with_flush);
+  if (sent_success == false)
   {
-    WATCHPOINT_ERROR(rc);
-    return rc;
-  }
-
-  /*
-  ** Since non buffering ops in UDP mode dont check to make sure they will fit
-  ** before they start writing, if there is any data in buffer, clear it out,
-  ** otherwise we might get a partial write.
-  **/
-  if (ptr->type == MEMCACHED_CONNECTION_UDP && with_flush && ptr->write_buffer_offset > UDP_DATAGRAM_HEADER_LENGTH)
-  {
-    memcached_io_write(ptr, NULL, 0, true);
-  }
-
-  sent_length= memcached_io_writev(ptr, vector, count, with_flush);
-
-  size_t command_length= 0;
-  for (uint32_t x= 0; x < count; ++x, vector++)
-  {
-    command_length+= vector->length;
-  }
-
-  if (sent_length == -1 || (size_t)sent_length != command_length)
-  {
-    rc= MEMCACHED_WRITE_FAILURE;
-    WATCHPOINT_ERROR(rc);
-    WATCHPOINT_ERRNO(errno);
+    if (memcached_last_error(instance->root) == MEMCACHED_SUCCESS)
+    {
+      return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
+    }
+    else
+    {
+      rc= MEMCACHED_WRITE_FAILURE;
+    }
   }
-  else if ((ptr->root->flags.no_reply) == 0)
+  else if (memcached_is_replying(instance->root))
   {
-    memcached_server_response_increment(ptr);
+    memcached_server_response_increment(instance);
   }
 
   return rc;