Fix OSX failure, and have memcached_do just use memcached_vdo
[awesomized/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_do(memcached_server_write_instance_st ptr,
15 const void *command,
16 size_t command_length,
17 bool with_flush)
18 {
19 assert_msg(command_length, "Programming error, somehow a command had a length of zero");
20 assert_msg(command, "Programming error, somehow a command was NULL");
21
22 libmemcached_io_vector_st vector[1]= { { command, command_length } };
23
24 return memcached_vdo(ptr, vector, 1, with_flush);
25 }
26
27 memcached_return_t memcached_vdo(memcached_server_write_instance_st ptr,
28 const struct libmemcached_io_vector_st *vector,
29 size_t count,
30 bool with_flush)
31 {
32 memcached_return_t rc;
33
34 WATCHPOINT_ASSERT(count);
35 WATCHPOINT_ASSERT(vector);
36
37 if (memcached_failed(rc= memcached_connect(ptr)))
38 {
39 WATCHPOINT_ERROR(rc);
40 assert_msg(ptr->error_messages, "memcached_connect() returned an error but the memcached_server_write_instance_st showed none.");
41 return rc;
42 }
43
44 /*
45 ** Since non buffering ops in UDP mode dont check to make sure they will fit
46 ** before they start writing, if there is any data in buffer, clear it out,
47 ** otherwise we might get a partial write.
48 **/
49 if (memcached_is_udp(ptr->root) and with_flush and ptr->write_buffer_offset > UDP_DATAGRAM_HEADER_LENGTH)
50 {
51 if (memcached_io_write(ptr, NULL, 0, true) == -1)
52 {
53 memcached_io_reset(ptr);
54 return memcached_set_error(*ptr, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
55 }
56 }
57
58 ssize_t sent_length= memcached_io_writev(ptr, vector, count, with_flush);
59
60 size_t command_length= 0;
61 for (uint32_t x= 0; x < count; ++x, vector++)
62 {
63 command_length+= vector->length;
64 }
65
66 if (sent_length == -1 or size_t(sent_length) != command_length)
67 {
68 rc= MEMCACHED_WRITE_FAILURE;
69 WATCHPOINT_ERROR(rc);
70 WATCHPOINT_ERRNO(errno);
71 }
72 else if ((ptr->root->flags.no_reply) == 0)
73 {
74 memcached_server_response_increment(ptr);
75 }
76
77 return rc;
78 }