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