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