99052c070894a9bce8b05ee4e56a462c6fa95ef0
[awesomized/libmemcached] / src / libmemcached / udp.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 /*
19 * The udp request id consists of two seperate sections
20 * 1) The thread id
21 * 2) The message number
22 * The thread id should only be set when the memcached_st struct is created
23 * and should not be changed.
24 *
25 * The message num is incremented for each new message we send, this function
26 * extracts the message number from message_id, increments it and then
27 * writes the new value back into the header
28 */
29 void increment_udp_message_id(memcached_instance_st *ptr) {
30 struct udp_datagram_header_st *header = (struct udp_datagram_header_st *) ptr->write_buffer;
31 uint16_t cur_req = get_udp_datagram_request_id(header);
32 int msg_num = get_msg_num_from_request_id(cur_req);
33 int thread_id = get_thread_id_from_request_id(cur_req);
34
35 if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK))
36 msg_num = 0;
37
38 header->request_id = htons((uint16_t)(thread_id | msg_num));
39 }
40
41 bool memcached_io_init_udp_header(memcached_instance_st *ptr, const uint16_t thread_id) {
42 if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID) {
43 return MEMCACHED_FAILURE;
44 }
45
46 struct udp_datagram_header_st *header = (struct udp_datagram_header_st *) ptr->write_buffer;
47 header->request_id = htons(uint16_t((generate_udp_request_thread_id(thread_id))));
48 header->num_datagrams = htons(1);
49 header->sequence_number = htons(0);
50
51 return MEMCACHED_SUCCESS;
52 }