msvc support
[m6w6/libmemcached] / src / libmemcached / do.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17
18 static memcached_return_t _vdo_udp(memcached_instance_st *instance,
19 libmemcached_io_vector_st vector[], const size_t count) {
20 #if HAVE_SENDMSG && HAVE_STRUCT_MSGHDR
21 if (vector[0].buffer or vector[0].length) {
22 return memcached_set_error(
23 *instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
24 memcached_literal_param("UDP messages was attempted, but vector was not setup for it"));
25 }
26
27 struct msghdr msg;
28 memset(&msg, 0, sizeof(msg));
29
30 increment_udp_message_id(instance);
31 vector[0].buffer = instance->write_buffer;
32 vector[0].length = UDP_DATAGRAM_HEADER_LENGTH;
33
34 msg.msg_iov = (struct iovec *) vector;
35 # ifdef __APPLE__
36 msg.msg_iovlen = int(count);
37 # else
38 msg.msg_iovlen = count;
39 # endif
40
41 uint32_t retry = 5;
42 while (--retry) {
43 ssize_t sendmsg_length = ::sendmsg(instance->fd, &msg, 0);
44 if (sendmsg_length > 0) {
45 break;
46 } else if (sendmsg_length < 0) {
47 if (errno == EMSGSIZE) {
48 return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
49 }
50
51 return memcached_set_errno(*instance, errno, MEMCACHED_AT);
52 }
53 }
54
55 return MEMCACHED_SUCCESS;
56 #else
57 (void) instance;
58 (void) vector;
59 (void) count;
60 return MEMCACHED_FAILURE;
61 #endif
62 }
63
64 memcached_return_t memcached_vdo(memcached_instance_st *instance,
65 libmemcached_io_vector_st vector[], const size_t count,
66 const bool with_flush) {
67 memcached_return_t rc;
68
69 assert_msg(vector, "Invalid vector passed");
70
71 if (memcached_failed(rc = memcached_connect(instance))) {
72 WATCHPOINT_ERROR(rc);
73 assert_msg(instance->error_messages,
74 "memcached_connect() returned an error but the Instance showed none.");
75 return rc;
76 }
77
78 /*
79 ** Since non buffering ops in UDP mode dont check to make sure they will fit
80 ** before they start writing, if there is any data in buffer, clear it out,
81 ** otherwise we might get a partial write.
82 **/
83 bool sent_success;
84 if (memcached_is_udp(instance->root)) {
85 sent_success = memcached_success(rc = _vdo_udp(instance, vector, count));
86 } else {
87 sent_success = memcached_io_writev(instance, vector, count, with_flush);
88 }
89 if (sent_success == false) {
90 rc = memcached_last_error(instance->root);
91 if (rc == MEMCACHED_SUCCESS) {
92 memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
93 }
94 memcached_io_reset(instance);
95 } else if (memcached_is_replying(instance->root) && !memcached_is_udp(instance->root)) {
96 memcached_server_response_increment(instance);
97 }
98
99 return rc;
100 }