Fix APPLE identifier.
[awesomized/libmemcached] / libmemcached / do.cc
index a51472ba66562a50a18353b1c843bd5c3a3afd7c..73ef06d34f11063859e19c92e331a4f27288a47a 100644 (file)
 
 #include <libmemcached/common.h>
 
-memcached_return_t memcached_vdo(memcached_server_write_instance_st instance,
-                                 libmemcached_io_vector_st *vector,
+static memcached_return_t _vdo_udp(org::libmemcached::Instance* instance,
+                                   libmemcached_io_vector_st vector[],
+                                   const size_t count)
+{
+#ifndef __MINGW32__
+  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;
+#ifdef __APPLE__
+  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;
+#else
+  (void)instance;
+  (void)vector;
+  (void)count;
+  return MEMCACHED_FAILURE;
+#endif
+}
+
+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;
 
-  WATCHPOINT_ASSERT(count);
-  WATCHPOINT_ASSERT(vector);
+  assert_msg(vector, "Invalid vector passed");
 
   if (memcached_failed(rc= memcached_connect(instance)))
   {
     WATCHPOINT_ERROR(rc);
-    assert_msg(instance->error_messages, "memcached_connect() returned an error but the memcached_server_write_instance_st showed none.");
+    assert_msg(instance->error_messages, "memcached_connect() returned an error but the Instance showed none.");
     return rc;
   }
 
@@ -35,34 +87,22 @@ memcached_return_t memcached_vdo(memcached_server_write_instance_st instance,
   **/
   if (memcached_is_udp(instance->root))
   {
-    if (vector->buffer or vector->length)
+    return _vdo_udp(instance, vector, count);
+  }
+
+  bool sent_success= memcached_io_writev(instance, vector, count, with_flush);
+  if (sent_success == false)
+  {
+    assert(memcached_last_error(instance->root) == MEMCACHED_SUCCESS);
+    if (memcached_last_error(instance->root) == MEMCACHED_SUCCESS)
     {
-      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"));
+      assert(memcached_last_error(instance->root) != MEMCACHED_SUCCESS);
+      return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
     }
-
-    size_t write_length= io_vector_total_size(vector, 11) +UDP_DATAGRAM_HEADER_LENGTH;
-
-    if (write_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
+    else
     {
-      return MEMCACHED_WRITE_FAILURE;
+      rc= memcached_last_error(instance->root);
     }
-
-    return MEMCACHED_NOT_SUPPORTED;
-  }
-
-  ssize_t sent_length= memcached_io_writev(instance, 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 or size_t(sent_length) != command_length)
-  {
-    rc= MEMCACHED_WRITE_FAILURE;
-    WATCHPOINT_ERROR(rc);
-    WATCHPOINT_ERRNO(errno);
   }
   else if (memcached_is_replying(instance->root))
   {