Updating for 1.0.2 release
[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 (memcached_is_udp(ptr->root) and with_flush and 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 (memcached_is_udp(ptr->root) and with_flush and ptr->write_buffer_offset > UDP_DATAGRAM_HEADER_LENGTH)
74 {
75 if (memcached_io_write(ptr, NULL, 0, true) == -1)
76 {
77 memcached_io_reset(ptr);
78 return memcached_set_error(*ptr, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
79 }
80 }
81
82 ssize_t sent_length= memcached_io_writev(ptr, vector, count, with_flush);
83
84 size_t command_length= 0;
85 for (uint32_t x= 0; x < count; ++x, vector++)
86 {
87 command_length+= vector->length;
88 }
89
90 if (sent_length == -1 or size_t(sent_length) != command_length)
91 {
92 rc= MEMCACHED_WRITE_FAILURE;
93 WATCHPOINT_ERROR(rc);
94 WATCHPOINT_ERRNO(errno);
95 }
96 else if ((ptr->root->flags.no_reply) == 0)
97 {
98 memcached_server_response_increment(ptr);
99 }
100
101 return rc;
102 }