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