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