5a9a7320f4d691ec2b5bc7a7004a824e1f98d6f5
[awesomized/libmemcached] / libmemcached / io.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Server IO, Not public!
9 *
10 */
11
12
13 #include "common.h"
14
15 typedef enum {
16 MEM_READ,
17 MEM_WRITE
18 } memc_read_or_write;
19
20 static ssize_t io_flush(memcached_server_write_instance_st ptr,
21 memcached_return_t *error);
22 static void increment_udp_message_id(memcached_server_write_instance_st ptr);
23
24 static memcached_return_t io_wait(memcached_server_write_instance_st ptr,
25 memc_read_or_write read_or_write)
26 {
27 struct pollfd fds= {
28 .fd= ptr->fd,
29 .events = POLLIN
30 };
31 int error;
32
33 if (read_or_write == MEM_WRITE) /* write */
34 {
35 fds.events= POLLOUT;
36 WATCHPOINT_SET(ptr->io_wait_count.write++);
37 }
38 else
39 {
40 WATCHPOINT_SET(ptr->io_wait_count.read++);
41 }
42
43 /*
44 ** We are going to block on write, but at least on Solaris we might block
45 ** on write if we haven't read anything from our input buffer..
46 ** Try to purge the input buffer if we don't do any flow control in the
47 ** application layer (just sending a lot of data etc)
48 ** The test is moved down in the purge function to avoid duplication of
49 ** the test.
50 */
51 if (read_or_write == MEM_WRITE)
52 {
53 memcached_return_t rc= memcached_purge(ptr);
54 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
55 return MEMCACHED_FAILURE;
56 }
57
58 size_t loop_max= 5;
59 while (--loop_max) // While loop is for ERESTART or EINTR
60 {
61 error= poll(&fds, 1, ptr->root->poll_timeout);
62
63 switch (error)
64 {
65 case 1: // Success!
66 WATCHPOINT_IF_LABELED_NUMBER(read_or_write && loop_max < 4, "read() times we had to loop, decremented down from 5", loop_max);
67 WATCHPOINT_IF_LABELED_NUMBER(!read_or_write && loop_max < 4, "write() times we had to loop, decremented down from 5", loop_max);
68
69 return MEMCACHED_SUCCESS;
70 case 0: // Timeout occured, we let the while() loop do its thing.
71 return MEMCACHED_TIMEOUT;
72 default:
73 WATCHPOINT_ERRNO(get_socket_errno());
74 switch (get_socket_errno())
75 {
76 #ifdef TARGET_OS_LINUX
77 case ERESTART:
78 #endif
79 case EINTR:
80 break;
81 default:
82 if (fds.revents & POLLERR)
83 {
84 int err;
85 socklen_t len= sizeof (err);
86 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
87 ptr->cached_errno= (err == 0) ? get_socket_errno() : err;
88 }
89 else
90 {
91 ptr->cached_errno= get_socket_errno();
92 }
93 memcached_quit_server(ptr, true);
94
95 return MEMCACHED_FAILURE;
96 }
97 }
98 }
99
100 /* Imposssible for anything other then -1 */
101 WATCHPOINT_ASSERT(error == -1);
102 ptr->cached_errno= get_socket_errno();
103 memcached_quit_server(ptr, true);
104
105 return MEMCACHED_FAILURE;
106 }
107
108 memcached_return_t memcached_io_wait_for_write(memcached_server_write_instance_st ptr)
109 {
110 return io_wait(ptr, MEM_WRITE);
111 }
112
113 /**
114 * Try to fill the input buffer for a server with as much
115 * data as possible.
116 *
117 * @param ptr the server to pack
118 */
119 static bool repack_input_buffer(memcached_server_write_instance_st ptr)
120 {
121 if (ptr->read_ptr != ptr->read_buffer)
122 {
123 /* Move all of the data to the beginning of the buffer so
124 ** that we can fit more data into the buffer...
125 */
126 memmove(ptr->read_buffer, ptr->read_ptr, ptr->read_buffer_length);
127 ptr->read_ptr= ptr->read_buffer;
128 ptr->read_data_length= ptr->read_buffer_length;
129 }
130
131 /* There is room in the buffer, try to fill it! */
132 if (ptr->read_buffer_length != MEMCACHED_MAX_BUFFER)
133 {
134 /* Just try a single read to grab what's available */
135 ssize_t nr= recv(ptr->fd,
136 ptr->read_ptr + ptr->read_data_length,
137 MEMCACHED_MAX_BUFFER - ptr->read_data_length,
138 0);
139
140 if (nr > 0)
141 {
142 ptr->read_data_length+= (size_t)nr;
143 ptr->read_buffer_length+= (size_t)nr;
144 return true;
145 }
146 }
147 return false;
148 }
149
150 /**
151 * If the we have callbacks connected to this server structure
152 * we may start process the input queue and fire the callbacks
153 * for the incomming messages. This function is _only_ called
154 * when the input buffer is full, so that we _know_ that we have
155 * at least _one_ message to process.
156 *
157 * @param ptr the server to star processing iput messages for
158 * @return true if we processed anything, false otherwise
159 */
160 static bool process_input_buffer(memcached_server_write_instance_st ptr)
161 {
162 /*
163 ** We might be able to process some of the response messages if we
164 ** have a callback set up
165 */
166 if (ptr->root->callbacks != NULL && ptr->root->flags.use_udp == false)
167 {
168 /*
169 * We might have responses... try to read them out and fire
170 * callbacks
171 */
172 memcached_callback_st cb= *ptr->root->callbacks;
173
174 memcached_set_processing_input((memcached_st *)ptr->root, true);
175
176 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
177 memcached_return_t error;
178 memcached_st *root= (memcached_st *)ptr->root;
179 error= memcached_response(ptr, buffer, sizeof(buffer),
180 &root->result);
181
182 memcached_set_processing_input(root, false);
183
184 if (error == MEMCACHED_SUCCESS)
185 {
186 for (unsigned int x= 0; x < cb.number_of_callback; x++)
187 {
188 error= (*cb.callback[x])(ptr->root, &root->result, cb.context);
189 if (error != MEMCACHED_SUCCESS)
190 break;
191 }
192
193 /* @todo what should I do with the error message??? */
194 }
195 /* @todo what should I do with other error messages?? */
196 return true;
197 }
198
199 return false;
200 }
201
202 static inline void memcached_io_cork_push(memcached_server_st *ptr)
203 {
204 (void)ptr;
205 #ifdef CORK
206 if (ptr->root->flags.cork == false || ptr->state.is_corked)
207 return;
208
209 int enable= 1;
210 int err= setsockopt(ptr->fd, IPPROTO_TCP, CORK,
211 &enable, (socklen_t)sizeof(int));
212 if (! err)
213 ptr->state.is_corked= true;
214
215 WATCHPOINT_ASSERT(ptr->state.is_corked == true);
216 #endif
217 }
218
219 static inline void memcached_io_cork_pop(memcached_server_st *ptr)
220 {
221 (void)ptr;
222 #ifdef CORK
223 if (ptr->root->flags.cork == false || ptr->state.is_corked == false)
224 return;
225
226 int enable= 0;
227 int err= setsockopt(ptr->fd, IPPROTO_TCP, CORK,
228 &enable, (socklen_t)sizeof(int));
229 if (! err)
230 ptr->state.is_corked= false;
231
232 WATCHPOINT_ASSERT(ptr->state.is_corked == false);
233 #endif
234 }
235
236 #if 0 // Dead code, this should be removed.
237 void memcached_io_preread(memcached_st *ptr)
238 {
239 unsigned int x;
240
241 return;
242
243 for (x= 0; x < memcached_server_count(ptr); x++)
244 {
245 if (memcached_server_response_count(ptr, x) &&
246 ptr->hosts[x].read_data_length < MEMCACHED_MAX_BUFFER )
247 {
248 size_t data_read;
249
250 data_read= recv(ptr->hosts[x].fd,
251 ptr->hosts[x].read_ptr + ptr->hosts[x].read_data_length,
252 MEMCACHED_MAX_BUFFER - ptr->hosts[x].read_data_length, 0);
253 if (data_read == SOCKET_ERROR)
254 continue;
255
256 ptr->hosts[x].read_buffer_length+= data_read;
257 ptr->hosts[x].read_data_length+= data_read;
258 }
259 }
260 }
261 #endif
262
263 memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
264 void *buffer, size_t length, ssize_t *nread)
265 {
266 char *buffer_ptr;
267
268 buffer_ptr= buffer;
269
270 while (length)
271 {
272 if (!ptr->read_buffer_length)
273 {
274 ssize_t data_read;
275
276 while (1)
277 {
278 data_read= recv(ptr->fd, ptr->read_buffer, MEMCACHED_MAX_BUFFER, 0);
279 if (data_read > 0)
280 {
281 break;
282 }
283 else if (data_read == SOCKET_ERROR)
284 {
285 ptr->cached_errno= get_socket_errno();
286 memcached_return_t rc= MEMCACHED_ERRNO;
287 switch (get_socket_errno())
288 {
289 case EWOULDBLOCK:
290 #ifdef USE_EAGAIN
291 case EAGAIN:
292 #endif
293 case EINTR:
294 #ifdef TARGET_OS_LINUX
295 case ERESTART:
296 #endif
297 if ((rc= io_wait(ptr, MEM_READ)) == MEMCACHED_SUCCESS)
298 continue;
299 /* fall through */
300
301 default:
302 {
303 memcached_quit_server(ptr, true);
304 *nread= -1;
305 return rc;
306 }
307 }
308 }
309 else
310 {
311 /*
312 EOF. Any data received so far is incomplete
313 so discard it. This always reads by byte in case of TCP
314 and protocol enforcement happens at memcached_response()
315 looking for '\n'. We do not care for UDB which requests 8 bytes
316 at once. Generally, this means that connection went away. Since
317 for blocking I/O we do not return 0 and for non-blocking case
318 it will return EGAIN if data is not immediatly available.
319 */
320 WATCHPOINT_STRING("We had a zero length recv()");
321 memcached_quit_server(ptr, true);
322 *nread= -1;
323 return MEMCACHED_UNKNOWN_READ_FAILURE;
324 }
325 }
326
327 ptr->io_bytes_sent = 0;
328 ptr->read_data_length= (size_t) data_read;
329 ptr->read_buffer_length= (size_t) data_read;
330 ptr->read_ptr= ptr->read_buffer;
331 }
332
333 if (length > 1)
334 {
335 size_t difference;
336
337 difference= (length > ptr->read_buffer_length) ? ptr->read_buffer_length : length;
338
339 memcpy(buffer_ptr, ptr->read_ptr, difference);
340 length -= difference;
341 ptr->read_ptr+= difference;
342 ptr->read_buffer_length-= difference;
343 buffer_ptr+= difference;
344 }
345 else
346 {
347 *buffer_ptr= *ptr->read_ptr;
348 ptr->read_ptr++;
349 ptr->read_buffer_length--;
350 buffer_ptr++;
351 break;
352 }
353 }
354
355 ptr->server_failure_counter= 0;
356 *nread = (ssize_t)(buffer_ptr - (char*)buffer);
357 return MEMCACHED_SUCCESS;
358 }
359
360 static ssize_t _io_write(memcached_server_write_instance_st ptr,
361 const void *buffer, size_t length, bool with_flush)
362 {
363 size_t original_length;
364 const char* buffer_ptr;
365
366 WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
367
368 original_length= length;
369 buffer_ptr= buffer;
370
371 /* more writable data is coming if a flush isn't required, so delay send */
372 if (! with_flush)
373 {
374 memcached_io_cork_push(ptr);
375 }
376
377 while (length)
378 {
379 char *write_ptr;
380 size_t should_write;
381 size_t buffer_end;
382
383 if (ptr->type == MEMCACHED_CONNECTION_UDP)
384 {
385 //UDP does not support partial writes
386 buffer_end= MAX_UDP_DATAGRAM_LENGTH;
387 should_write= length;
388 if (ptr->write_buffer_offset + should_write > buffer_end)
389 {
390 return -1;
391 }
392 }
393 else
394 {
395 buffer_end= MEMCACHED_MAX_BUFFER;
396 should_write= buffer_end - ptr->write_buffer_offset;
397 should_write= (should_write < length) ? should_write : length;
398 }
399
400 write_ptr= ptr->write_buffer + ptr->write_buffer_offset;
401 memcpy(write_ptr, buffer_ptr, should_write);
402 ptr->write_buffer_offset+= should_write;
403 buffer_ptr+= should_write;
404 length-= should_write;
405
406 if (ptr->write_buffer_offset == buffer_end && ptr->type != MEMCACHED_CONNECTION_UDP)
407 {
408 memcached_return_t rc;
409 ssize_t sent_length;
410
411 WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
412 sent_length= io_flush(ptr, &rc);
413 if (sent_length == -1)
414 {
415 return -1;
416 }
417
418 /* If io_flush calls memcached_purge, sent_length may be 0 */
419 unlikely (sent_length != 0)
420 {
421 WATCHPOINT_ASSERT(sent_length == (ssize_t)buffer_end);
422 }
423 }
424 }
425
426 if (with_flush)
427 {
428 memcached_return_t rc;
429 WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
430 if (io_flush(ptr, &rc) == -1)
431 {
432 return -1;
433 }
434
435 memcached_io_cork_pop(ptr);
436 }
437
438 return (ssize_t) original_length;
439 }
440
441 ssize_t memcached_io_write(memcached_server_write_instance_st ptr,
442 const void *buffer, size_t length, bool with_flush)
443 {
444 return _io_write(ptr, buffer, length, with_flush);
445 }
446
447 ssize_t memcached_io_writev(memcached_server_write_instance_st ptr,
448 const struct libmemcached_io_vector_st *vector,
449 size_t number_of, bool with_flush)
450 {
451 ssize_t total= 0;
452
453 for (size_t x= 0; x < number_of; x++, vector++)
454 {
455 ssize_t returnable;
456
457 if ((returnable= _io_write(ptr, vector->buffer, vector->length, false)) == -1)
458 {
459 return -1;
460 }
461 total+= returnable;
462 }
463
464 if (with_flush)
465 {
466 if (memcached_io_write(ptr, NULL, 0, true) == -1)
467 {
468 return -1;
469 }
470 }
471
472 return total;
473 }
474
475
476 memcached_return_t memcached_io_close(memcached_server_write_instance_st ptr)
477 {
478 if (ptr->fd == INVALID_SOCKET)
479 {
480 return MEMCACHED_SUCCESS;
481 }
482
483 /* in case of death shutdown to avoid blocking at close() */
484 if (shutdown(ptr->fd, SHUT_RDWR) == SOCKET_ERROR && get_socket_errno() != ENOTCONN)
485 {
486 WATCHPOINT_NUMBER(ptr->fd);
487 WATCHPOINT_ERRNO(get_socket_errno());
488 WATCHPOINT_ASSERT(get_socket_errno());
489 }
490
491 if (closesocket(ptr->fd) == SOCKET_ERROR)
492 {
493 WATCHPOINT_ERRNO(get_socket_errno());
494 }
495
496 return MEMCACHED_SUCCESS;
497 }
498
499 memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc)
500 {
501 #define MAX_SERVERS_TO_POLL 100
502 struct pollfd fds[MAX_SERVERS_TO_POLL];
503 unsigned int host_index= 0;
504
505 for (uint32_t x= 0;
506 x< memcached_server_count(memc) && host_index < MAX_SERVERS_TO_POLL;
507 ++x)
508 {
509 memcached_server_write_instance_st instance=
510 memcached_server_instance_fetch(memc, x);
511
512 if (instance->read_buffer_length > 0) /* I have data in the buffer */
513 return instance;
514
515 if (memcached_server_response_count(instance) > 0)
516 {
517 fds[host_index].events = POLLIN;
518 fds[host_index].revents = 0;
519 fds[host_index].fd = instance->fd;
520 ++host_index;
521 }
522 }
523
524 if (host_index < 2)
525 {
526 /* We have 0 or 1 server with pending events.. */
527 for (uint32_t x= 0; x< memcached_server_count(memc); ++x)
528 {
529 memcached_server_write_instance_st instance=
530 memcached_server_instance_fetch(memc, x);
531
532 if (memcached_server_response_count(instance) > 0)
533 {
534 return instance;
535 }
536 }
537
538 return NULL;
539 }
540
541 int err= poll(fds, host_index, memc->poll_timeout);
542 switch (err) {
543 case -1:
544 memcached_set_errno(memc, get_socket_errno(), NULL);
545 /* FALLTHROUGH */
546 case 0:
547 break;
548 default:
549 for (size_t x= 0; x < host_index; ++x)
550 {
551 if (fds[x].revents & POLLIN)
552 {
553 for (uint32_t y= 0; y < memcached_server_count(memc); ++y)
554 {
555 memcached_server_write_instance_st instance=
556 memcached_server_instance_fetch(memc, y);
557
558 if (instance->fd == fds[x].fd)
559 return instance;
560 }
561 }
562 }
563 }
564
565 return NULL;
566 }
567
568 static ssize_t io_flush(memcached_server_write_instance_st ptr,
569 memcached_return_t *error)
570 {
571 /*
572 ** We might want to purge the input buffer if we haven't consumed
573 ** any output yet... The test for the limits is the purge is inline
574 ** in the purge function to avoid duplicating the logic..
575 */
576 {
577 memcached_return_t rc;
578 WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
579 rc= memcached_purge(ptr);
580
581 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
582 {
583 return -1;
584 }
585 }
586 ssize_t sent_length;
587 size_t return_length;
588 char *local_write_ptr= ptr->write_buffer;
589 size_t write_length= ptr->write_buffer_offset;
590
591 *error= MEMCACHED_SUCCESS;
592
593 WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
594
595 // UDP Sanity check, make sure that we are not sending somthing too big
596 if (ptr->type == MEMCACHED_CONNECTION_UDP && write_length > MAX_UDP_DATAGRAM_LENGTH)
597 {
598 return -1;
599 }
600
601 if (ptr->write_buffer_offset == 0 || (ptr->type == MEMCACHED_CONNECTION_UDP
602 && ptr->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH))
603 return 0;
604
605 /* Looking for memory overflows */
606 #if defined(DEBUG)
607 if (write_length == MEMCACHED_MAX_BUFFER)
608 WATCHPOINT_ASSERT(ptr->write_buffer == local_write_ptr);
609 WATCHPOINT_ASSERT((ptr->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
610 #endif
611
612 return_length= 0;
613 while (write_length)
614 {
615 WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
616 WATCHPOINT_ASSERT(write_length > 0);
617 sent_length= 0;
618 if (ptr->type == MEMCACHED_CONNECTION_UDP)
619 increment_udp_message_id(ptr);
620
621 WATCHPOINT_ASSERT(ptr->fd != -1);
622 sent_length= send(ptr->fd, local_write_ptr, write_length, MSG_NOSIGNAL|MSG_DONTWAIT);
623 if (sent_length == SOCKET_ERROR)
624 {
625 ptr->cached_errno= get_socket_errno();
626 #if 0 // @todo I should look at why we hit this bit of code hard frequently
627 WATCHPOINT_ERRNO(get_socket_errno());
628 WATCHPOINT_NUMBER(get_socket_errno());
629 #endif
630 switch (get_socket_errno())
631 {
632 case ENOBUFS:
633 continue;
634 case EWOULDBLOCK:
635 #ifdef USE_EAGAIN
636 case EAGAIN:
637 #endif
638 {
639 /*
640 * We may be blocked on write because the input buffer
641 * is full. Let's check if we have room in our input
642 * buffer for more data and retry the write before
643 * waiting..
644 */
645 if (repack_input_buffer(ptr) ||
646 process_input_buffer(ptr))
647 continue;
648
649 memcached_return_t rc;
650 rc= io_wait(ptr, MEM_WRITE);
651
652 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_TIMEOUT)
653 continue;
654
655 memcached_quit_server(ptr, true);
656 return -1;
657 }
658 case ENOTCONN:
659 case EPIPE:
660 default:
661 memcached_quit_server(ptr, true);
662 *error= MEMCACHED_ERRNO;
663 WATCHPOINT_ASSERT(ptr->fd == -1);
664 return -1;
665 }
666 }
667
668 if (ptr->type == MEMCACHED_CONNECTION_UDP &&
669 (size_t)sent_length != write_length)
670 {
671 memcached_quit_server(ptr, true);
672 return -1;
673 }
674
675 ptr->io_bytes_sent += (uint32_t) sent_length;
676
677 local_write_ptr+= sent_length;
678 write_length-= (uint32_t) sent_length;
679 return_length+= (uint32_t) sent_length;
680 }
681
682 WATCHPOINT_ASSERT(write_length == 0);
683 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
684 // ptr->write_buffer_offset);
685
686 // if we are a udp server, the begining of the buffer is reserverd for
687 // the upd frame header
688 if (ptr->type == MEMCACHED_CONNECTION_UDP)
689 ptr->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
690 else
691 ptr->write_buffer_offset= 0;
692
693 return (ssize_t) return_length;
694 }
695
696 /*
697 Eventually we will just kill off the server with the problem.
698 */
699 void memcached_io_reset(memcached_server_write_instance_st ptr)
700 {
701 memcached_quit_server(ptr, true);
702 }
703
704 /**
705 * Read a given number of bytes from the server and place it into a specific
706 * buffer. Reset the IO channel on this server if an error occurs.
707 */
708 memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr,
709 void *dta,
710 size_t size)
711 {
712 size_t offset= 0;
713 char *data= dta;
714
715 while (offset < size)
716 {
717 ssize_t nread;
718 memcached_return_t rc= memcached_io_read(ptr, data + offset, size - offset,
719 &nread);
720 if (rc != MEMCACHED_SUCCESS)
721 return rc;
722
723 offset+= (size_t) nread;
724 }
725
726 return MEMCACHED_SUCCESS;
727 }
728
729 memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr,
730 char *buffer_ptr,
731 size_t size)
732 {
733 bool line_complete= false;
734 size_t total_nr= 0;
735
736 while (!line_complete)
737 {
738 if (ptr->read_buffer_length == 0)
739 {
740 /*
741 * We don't have any data in the buffer, so let's fill the read
742 * buffer. Call the standard read function to avoid duplicating
743 * the logic.
744 */
745 ssize_t nread;
746 memcached_return_t rc= memcached_io_read(ptr, buffer_ptr, 1, &nread);
747 if (rc != MEMCACHED_SUCCESS)
748 return rc;
749
750 if (*buffer_ptr == '\n')
751 line_complete= true;
752
753 ++buffer_ptr;
754 ++total_nr;
755 }
756
757 /* Now let's look in the buffer and copy as we go! */
758 while (ptr->read_buffer_length && total_nr < size && !line_complete)
759 {
760 *buffer_ptr = *ptr->read_ptr;
761 if (*buffer_ptr == '\n')
762 line_complete = true;
763 --ptr->read_buffer_length;
764 ++ptr->read_ptr;
765 ++total_nr;
766 ++buffer_ptr;
767 }
768
769 if (total_nr == size)
770 return MEMCACHED_PROTOCOL_ERROR;
771 }
772
773 return MEMCACHED_SUCCESS;
774 }
775
776 /*
777 * The udp request id consists of two seperate sections
778 * 1) The thread id
779 * 2) The message number
780 * The thread id should only be set when the memcached_st struct is created
781 * and should not be changed.
782 *
783 * The message num is incremented for each new message we send, this function
784 * extracts the message number from message_id, increments it and then
785 * writes the new value back into the header
786 */
787 static void increment_udp_message_id(memcached_server_write_instance_st ptr)
788 {
789 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
790 uint16_t cur_req= get_udp_datagram_request_id(header);
791 int msg_num= get_msg_num_from_request_id(cur_req);
792 int thread_id= get_thread_id_from_request_id(cur_req);
793
794 if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK) != 0)
795 msg_num= 0;
796
797 header->request_id= htons((uint16_t) (thread_id | msg_num));
798 }
799
800 memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr, uint16_t thread_id)
801 {
802 if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID)
803 return MEMCACHED_FAILURE;
804
805 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
806 header->request_id= htons((uint16_t) (generate_udp_request_thread_id(thread_id)));
807 header->num_datagrams= htons(1);
808 header->sequence_number= htons(0);
809
810 return MEMCACHED_SUCCESS;
811 }