1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include "libmemcached/common.h"
41 * The udp request id consists of two seperate sections
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.
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
51 void increment_udp_message_id(memcached_instance_st
* ptr
)
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
);
58 if (((++msg_num
) & UDP_REQUEST_ID_THREAD_MASK
) != 0)
61 header
->request_id
= htons((uint16_t) (thread_id
| msg_num
));
64 bool memcached_io_init_udp_header(memcached_instance_st
* ptr
, const uint16_t thread_id
)
66 if (thread_id
> UDP_REQUEST_ID_MAX_THREAD_ID
)
68 return MEMCACHED_FAILURE
;
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);
76 return MEMCACHED_SUCCESS
;