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 +--------------------------------------------------------------------+
16 #include "libmemcached/common.h"
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"));
28 memset(&msg
, 0, sizeof(msg
));
30 increment_udp_message_id(instance
);
31 vector
[0].buffer
= instance
->write_buffer
;
32 vector
[0].length
= UDP_DATAGRAM_HEADER_LENGTH
;
34 msg
.msg_iov
= (struct iovec
*) vector
;
36 msg
.msg_iovlen
= int(count
);
38 msg
.msg_iovlen
= count
;
43 ssize_t sendmsg_length
= ::sendmsg(instance
->fd
, &msg
, 0);
44 if (sendmsg_length
> 0) {
46 } else if (sendmsg_length
< 0) {
47 if (errno
== EMSGSIZE
) {
48 return memcached_set_error(*instance
, MEMCACHED_WRITE_FAILURE
, MEMCACHED_AT
);
51 return memcached_set_errno(*instance
, errno
, MEMCACHED_AT
);
55 return MEMCACHED_SUCCESS
;
60 return MEMCACHED_FAILURE
;
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
;
69 assert_msg(vector
, "Invalid vector passed");
71 if (memcached_failed(rc
= memcached_connect(instance
))) {
73 assert_msg(instance
->error_messages
,
74 "memcached_connect() returned an error but the Instance showed none.");
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.
84 if (memcached_is_udp(instance
->root
)) {
85 sent_success
= memcached_success(rc
= _vdo_udp(instance
, vector
, count
));
87 sent_success
= memcached_io_writev(instance
, vector
, count
, with_flush
);
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
);
94 memcached_io_reset(instance
);
95 } else if (memcached_is_replying(instance
->root
) && !memcached_is_udp(instance
->root
)) {
96 memcached_server_response_increment(instance
);