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