ef63927b55e01fd294bfc4c91ff3869a58250acb
[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 static memcached_return_t _vdo_udp(org::libmemcached::Instance* instance,
15 libmemcached_io_vector_st vector[],
16 const size_t count)
17 {
18 #ifndef __MINGW32__
19 if (vector[0].buffer or vector[0].length)
20 {
21 return memcached_set_error(*instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
22 memcached_literal_param("UDP messages was attempted, but vector was not setup for it"));
23 }
24
25 struct msghdr msg;
26 memset(&msg, 0, sizeof(msg));
27
28 increment_udp_message_id(instance);
29 vector[0].buffer= instance->write_buffer;
30 vector[0].length= UDP_DATAGRAM_HEADER_LENGTH;
31
32 msg.msg_iov= (struct iovec*)vector;
33 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
34 msg.msg_iovlen= int(count);
35 #else
36 msg.msg_iovlen= count;
37 #endif
38
39 uint32_t retry= 5;
40 while (--retry)
41 {
42 ssize_t sendmsg_length= ::sendmsg(instance->fd, &msg, 0);
43 if (sendmsg_length > 0)
44 {
45 break;
46 }
47 else if (sendmsg_length < 0)
48 {
49 if (errno == EMSGSIZE)
50 {
51 return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
52 }
53
54 return memcached_set_errno(*instance, errno, MEMCACHED_AT);
55 }
56 }
57
58 return MEMCACHED_SUCCESS;
59 #else
60 return MEMCACHED_FAILURE;
61 #endif
62 }
63
64 memcached_return_t memcached_vdo(org::libmemcached::Instance* instance,
65 libmemcached_io_vector_st vector[],
66 const size_t count,
67 const bool with_flush)
68 {
69 memcached_return_t rc;
70
71 assert_msg(vector, "Invalid vector passed");
72
73 if (memcached_failed(rc= memcached_connect(instance)))
74 {
75 WATCHPOINT_ERROR(rc);
76 assert_msg(instance->error_messages, "memcached_connect() returned an error but the Instance showed none.");
77 return rc;
78 }
79
80 /*
81 ** Since non buffering ops in UDP mode dont check to make sure they will fit
82 ** before they start writing, if there is any data in buffer, clear it out,
83 ** otherwise we might get a partial write.
84 **/
85 if (memcached_is_udp(instance->root))
86 {
87 return _vdo_udp(instance, vector, count);
88 }
89
90 bool sent_success= memcached_io_writev(instance, vector, count, with_flush);
91 if (sent_success == false)
92 {
93 assert(memcached_last_error(instance->root) == MEMCACHED_SUCCESS);
94 if (memcached_last_error(instance->root) == MEMCACHED_SUCCESS)
95 {
96 assert(memcached_last_error(instance->root) != MEMCACHED_SUCCESS);
97 return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
98 }
99 else
100 {
101 rc= memcached_last_error(instance->root);
102 }
103 }
104 else if (memcached_is_replying(instance->root))
105 {
106 memcached_server_response_increment(instance);
107 }
108
109 return rc;
110 }