Merge from build trunk.
[m6w6/libmemcached] / libmemcached / udp.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * LibMemcached
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39
40 /*
41 * The udp request id consists of two seperate sections
42 * 1) The thread id
43 * 2) The message number
44 * The thread id should only be set when the memcached_st struct is created
45 * and should not be changed.
46 *
47 * The message num is incremented for each new message we send, this function
48 * extracts the message number from message_id, increments it and then
49 * writes the new value back into the header
50 */
51 void increment_udp_message_id(memcached_server_write_instance_st ptr)
52 {
53 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
54 uint16_t cur_req= get_udp_datagram_request_id(header);
55 int msg_num= get_msg_num_from_request_id(cur_req);
56 int thread_id= get_thread_id_from_request_id(cur_req);
57
58 if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK) != 0)
59 msg_num= 0;
60
61 header->request_id= htons((uint16_t) (thread_id | msg_num));
62 }
63
64 bool memcached_io_init_udp_header(memcached_server_write_instance_st ptr, const uint16_t thread_id)
65 {
66 if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID)
67 {
68 return MEMCACHED_FAILURE;
69 }
70
71 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
72 header->request_id= htons(uint16_t((generate_udp_request_thread_id(thread_id))));
73 header->num_datagrams= htons(1);
74 header->sequence_number= htons(0);
75
76 return MEMCACHED_SUCCESS;
77 }