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