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