Fix for OSX
[m6w6/libmemcached] / libmemcached / do.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include <libmemcached/common.h>
13
14 memcached_return_t memcached_vdo(org::libmemcached::Instance* instance,
15 libmemcached_io_vector_st vector[],
16 const size_t count,
17 const bool with_flush)
18 {
19 memcached_return_t rc;
20
21 assert_msg(vector, "Invalid vector passed");
22
23 if (memcached_failed(rc= memcached_connect(instance)))
24 {
25 WATCHPOINT_ERROR(rc);
26 assert_msg(instance->error_messages, "memcached_connect() returned an error but the Instance showed none.");
27 return rc;
28 }
29
30 /*
31 ** Since non buffering ops in UDP mode dont check to make sure they will fit
32 ** before they start writing, if there is any data in buffer, clear it out,
33 ** otherwise we might get a partial write.
34 **/
35 if (memcached_is_udp(instance->root))
36 {
37 #ifndef __MINGW32__
38 if (vector[0].buffer or vector[0].length)
39 {
40 return memcached_set_error(*instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
41 memcached_literal_param("UDP messages was attempted, but vector was not setup for it"));
42 }
43
44 struct msghdr msg;
45 memset(&msg, 0, sizeof(msg));
46
47 increment_udp_message_id(instance);
48 vector[0].buffer= instance->write_buffer;
49 vector[0].length= UDP_DATAGRAM_HEADER_LENGTH;
50
51 msg.msg_iov= (struct iovec*)vector;
52 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
53 msg.msg_iovlen= int(count);
54 #else
55 msg.msg_iovlen= count;
56 #endif
57
58 uint32_t retry= 5;
59 while (--retry)
60 {
61 ssize_t sendmsg_length= ::sendmsg(instance->fd, &msg, 0);
62 if (sendmsg_length > 0)
63 {
64 break;
65 }
66 else if (sendmsg_length < 0)
67 {
68 if (errno == EMSGSIZE)
69 {
70 return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
71 }
72
73 return memcached_set_errno(*instance, errno, MEMCACHED_AT);
74 }
75 }
76
77 return MEMCACHED_SUCCESS;
78 #else
79 return MEMCACHED_FAILURE;
80 #endif
81 }
82
83 bool sent_success= memcached_io_writev(instance, vector, count, with_flush);
84 if (sent_success == false)
85 {
86 if (memcached_last_error(instance->root) == MEMCACHED_SUCCESS)
87 {
88 return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
89 }
90 else
91 {
92 rc= MEMCACHED_WRITE_FAILURE;
93 }
94 }
95 else if (memcached_is_replying(instance->root))
96 {
97 memcached_server_response_increment(instance);
98 }
99
100 return rc;
101 }