d289c479fa71b41cb7129d920c7fd77081575cac
[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_vdo(memcached_server_write_instance_st instance,
15 libmemcached_io_vector_st vector[],
16 const size_t count,
17 const bool with_flush)
18 {
19 memcached_return_t rc;
20
21 assert_msg(vector, "Invalid vector passed");
22
23 if (memcached_failed(rc= memcached_connect(instance)))
24 {
25 WATCHPOINT_ERROR(rc);
26 assert_msg(instance->error_messages, "memcached_connect() returned an error but the memcached_server_write_instance_st showed none.");
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 (memcached_is_udp(instance->root))
36 {
37 if (vector[0].buffer or vector[0].length)
38 {
39 return memcached_set_error(*instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
40 memcached_literal_param("UDP messages was attempted, but vector was not setup for it"));
41 }
42
43 return MEMCACHED_NOT_SUPPORTED;
44 }
45
46 ssize_t sent_length= memcached_io_writev(instance, vector, count, with_flush);
47 size_t command_length= 0;
48 for (uint32_t x= 0; x < count; ++x, vector++)
49 {
50 command_length+= vector->length;
51 }
52
53 if (sent_length == -1 or size_t(sent_length) != command_length)
54 {
55 rc= MEMCACHED_WRITE_FAILURE;
56 WATCHPOINT_ERROR(rc);
57 WATCHPOINT_ERRNO(errno);
58 }
59 else if (memcached_is_replying(instance->root))
60 {
61 memcached_server_response_increment(instance);
62 }
63
64 return rc;
65 }