c++: fix -Wimplicit-fallthrough
[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(memcached_instance_st* 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(memcached_instance_st* 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 __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(memcached_instance_st* 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(memcached_instance_st* instance,
180 const short events)
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 (events & POLLOUT)
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= events;
201 fds.revents= 0;
202
203 if (fds.events & POLLOUT) /* write */
204 {
205 instance->io_wait_count.write++;
206 }
207 else
208 {
209 instance->io_wait_count.read++;
210 }
211
212 if (instance->root->poll_timeout == 0) // Mimic 0 causes timeout behavior (not all platforms do this)
213 {
214 return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_literal_param("poll_timeout() was set to zero"));
215 }
216
217 size_t loop_max= 5;
218 while (--loop_max) // While loop is for ERESTART or EINTR
219 {
220 int active_fd= poll(&fds, 1, instance->root->poll_timeout);
221
222 if (active_fd >= 1)
223 {
224 assert_msg(active_fd == 1 , "poll() returned an unexpected number of active file descriptors");
225 if (fds.revents & POLLIN or fds.revents & POLLOUT)
226 {
227 return MEMCACHED_SUCCESS;
228 }
229
230 if (fds.revents & POLLHUP)
231 {
232 return memcached_set_error(*instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT,
233 memcached_literal_param("poll() detected hang up"));
234 }
235
236 if (fds.revents & POLLERR)
237 {
238 int local_errno= EINVAL;
239 int err;
240 socklen_t len= sizeof (err);
241 if (getsockopt(instance->fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == 0)
242 {
243 if (err == 0) // treat this as EINTR
244 {
245 continue;
246 }
247 local_errno= err;
248 }
249 memcached_quit_server(instance, true);
250 return memcached_set_errno(*instance, local_errno, MEMCACHED_AT,
251 memcached_literal_param("poll() returned POLLHUP"));
252 }
253
254 return memcached_set_error(*instance, MEMCACHED_FAILURE, MEMCACHED_AT, memcached_literal_param("poll() returned a value that was not dealt with"));
255 }
256
257 if (active_fd == 0)
258 {
259 return memcached_set_error(*instance, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_literal_param("No active_fd were found"));
260 }
261
262 // Only an error should result in this code being called.
263 int local_errno= get_socket_errno(); // We cache in case memcached_quit_server() modifies errno
264 assert_msg(active_fd == -1 , "poll() returned an unexpected value");
265 switch (local_errno)
266 {
267 #ifdef __linux
268 case ERESTART:
269 #endif
270 case EINTR:
271 continue;
272
273 case EFAULT:
274 case ENOMEM:
275 memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
276 break;
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 break;
281
282 default:
283 memcached_set_errno(*instance, local_errno, MEMCACHED_AT, memcached_literal_param("poll"));
284 }
285
286 break;
287 }
288
289 memcached_quit_server(instance, true);
290
291 if (memcached_has_error(instance))
292 {
293 return memcached_instance_error_return(instance);
294 }
295
296 return memcached_set_error(*instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT,
297 memcached_literal_param("number of attempts to call io_wait() failed"));
298 }
299
300 static bool io_flush(memcached_instance_st* instance,
301 const bool with_flush,
302 memcached_return_t& error)
303 {
304 /*
305 ** We might want to purge the input buffer if we haven't consumed
306 ** any output yet... The test for the limits is the purge is inline
307 ** in the purge function to avoid duplicating the logic..
308 */
309 {
310 WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET);
311
312 if (memcached_purge(instance) == false)
313 {
314 return false;
315 }
316 }
317 char *local_write_ptr= instance->write_buffer;
318 size_t write_length= instance->write_buffer_offset;
319
320 error= MEMCACHED_SUCCESS;
321
322 WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET);
323
324 /* Looking for memory overflows */
325 #if defined(DEBUG)
326 if (write_length == MEMCACHED_MAX_BUFFER)
327 WATCHPOINT_ASSERT(instance->write_buffer == local_write_ptr);
328 WATCHPOINT_ASSERT((instance->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
329 #endif
330
331 while (write_length)
332 {
333 WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET);
334 WATCHPOINT_ASSERT(write_length > 0);
335
336 int flags;
337 if (with_flush)
338 {
339 flags= MSG_NOSIGNAL;
340 }
341 else
342 {
343 flags= MSG_NOSIGNAL|MSG_MORE;
344 }
345
346 ssize_t sent_length= ::send(instance->fd, local_write_ptr, write_length, flags);
347 int local_errno= get_socket_errno(); // We cache in case memcached_quit_server() modifies errno
348
349 if (sent_length == SOCKET_ERROR)
350 {
351 #if 0 // @todo I should look at why we hit this bit of code hard frequently
352 WATCHPOINT_ERRNO(get_socket_errno());
353 WATCHPOINT_NUMBER(get_socket_errno());
354 #endif
355 switch (get_socket_errno())
356 {
357 case ENOBUFS:
358 continue;
359
360 #if EWOULDBLOCK != EAGAIN
361 case EWOULDBLOCK:
362 #endif
363 case EAGAIN:
364 {
365 /*
366 * We may be blocked on write because the input buffer
367 * is full. Let's check if we have room in our input
368 * buffer for more data and retry the write before
369 * waiting..
370 */
371 if (repack_input_buffer(instance) or process_input_buffer(instance))
372 {
373 continue;
374 }
375
376 memcached_return_t rc= io_wait(instance, POLLOUT);
377 if (memcached_success(rc))
378 {
379 continue;
380 }
381 else if (rc == MEMCACHED_TIMEOUT)
382 {
383 return false;
384 }
385
386 memcached_quit_server(instance, true);
387 error= memcached_set_errno(*instance, local_errno, MEMCACHED_AT);
388 return false;
389 }
390 case ENOTCONN:
391 case EPIPE:
392 default:
393 memcached_quit_server(instance, true);
394 error= memcached_set_errno(*instance, local_errno, MEMCACHED_AT);
395 WATCHPOINT_ASSERT(instance->fd == INVALID_SOCKET);
396 return false;
397 }
398 }
399
400 instance->io_bytes_sent+= uint32_t(sent_length);
401
402 local_write_ptr+= sent_length;
403 write_length-= uint32_t(sent_length);
404 }
405
406 WATCHPOINT_ASSERT(write_length == 0);
407 instance->write_buffer_offset= 0;
408
409 return true;
410 }
411
412 memcached_return_t memcached_io_wait_for_write(memcached_instance_st* instance)
413 {
414 return io_wait(instance, POLLOUT);
415 }
416
417 memcached_return_t memcached_io_wait_for_read(memcached_instance_st* instance)
418 {
419 return io_wait(instance, POLLIN);
420 }
421
422 static memcached_return_t _io_fill(memcached_instance_st* instance)
423 {
424 ssize_t data_read;
425 do
426 {
427 data_read= ::recv(instance->fd, instance->read_buffer, MEMCACHED_MAX_BUFFER, MSG_NOSIGNAL);
428 int local_errno= get_socket_errno(); // We cache in case memcached_quit_server() modifies errno
429
430 if (data_read == SOCKET_ERROR)
431 {
432 switch (get_socket_errno())
433 {
434 case EINTR: // We just retry
435 continue;
436
437 case ETIMEDOUT: // OSX
438 #if EWOULDBLOCK != EAGAIN
439 case EWOULDBLOCK:
440 #endif
441 case EAGAIN:
442 #ifdef __linux
443 case ERESTART:
444 #endif
445 {
446 memcached_return_t io_wait_ret;
447 if (memcached_success(io_wait_ret= io_wait(instance, POLLIN)))
448 {
449 continue;
450 }
451
452 return io_wait_ret;
453 }
454
455 /* fall through */
456
457 case ENOTCONN: // Programmer Error
458 WATCHPOINT_ASSERT(0);
459 case ENOTSOCK:
460 WATCHPOINT_ASSERT(0);
461 case EBADF:
462 assert_msg(instance->fd != INVALID_SOCKET, "Programmer error, invalid socket");
463 /* fall through */
464 case EINVAL:
465 case EFAULT:
466 case ECONNREFUSED:
467 default:
468 memcached_quit_server(instance, true);
469 memcached_set_errno(*instance, local_errno, MEMCACHED_AT);
470 break;
471 }
472
473 return memcached_instance_error_return(instance);
474 }
475 else if (data_read == 0)
476 {
477 /*
478 EOF. Any data received so far is incomplete
479 so discard it. This always reads by byte in case of TCP
480 and protocol enforcement happens at memcached_response()
481 looking for '\n'. We do not care for UDB which requests 8 bytes
482 at once. Generally, this means that connection went away. Since
483 for blocking I/O we do not return 0 and for non-blocking case
484 it will return EGAIN if data is not immediatly available.
485 */
486 memcached_quit_server(instance, true);
487 return memcached_set_error(*instance, MEMCACHED_CONNECTION_FAILURE, MEMCACHED_AT,
488 memcached_literal_param("::rec() returned zero, server has disconnected"));
489 }
490 instance->io_wait_count._bytes_read+= data_read;
491 } while (data_read <= 0);
492
493 instance->io_bytes_sent= 0;
494 instance->read_data_length= (size_t) data_read;
495 instance->read_buffer_length= (size_t) data_read;
496 instance->read_ptr= instance->read_buffer;
497
498 return MEMCACHED_SUCCESS;
499 }
500
501 memcached_return_t memcached_io_read(memcached_instance_st* instance,
502 void *buffer, size_t length, ssize_t& nread)
503 {
504 assert(memcached_is_udp(instance->root) == false);
505 assert_msg(instance, "Programmer error, memcached_io_read() recieved an invalid Instance"); // Programmer error
506 char *buffer_ptr= static_cast<char *>(buffer);
507
508 if (instance->fd == INVALID_SOCKET)
509 {
510 #if 0
511 assert_msg(int(instance->state) <= int(MEMCACHED_SERVER_STATE_ADDRINFO), "Programmer error, invalid socket state");
512 #endif
513 return MEMCACHED_CONNECTION_FAILURE;
514 }
515
516 while (length)
517 {
518 if (instance->read_buffer_length == 0)
519 {
520 memcached_return_t io_fill_ret;
521 if (memcached_fatal(io_fill_ret= _io_fill(instance)))
522 {
523 nread= -1;
524 return io_fill_ret;
525 }
526 }
527
528 if (length > 1)
529 {
530 size_t difference= (length > instance->read_buffer_length) ? instance->read_buffer_length : length;
531
532 memcpy(buffer_ptr, instance->read_ptr, difference);
533 length -= difference;
534 instance->read_ptr+= difference;
535 instance->read_buffer_length-= difference;
536 buffer_ptr+= difference;
537 }
538 else
539 {
540 *buffer_ptr= *instance->read_ptr;
541 instance->read_ptr++;
542 instance->read_buffer_length--;
543 buffer_ptr++;
544 break;
545 }
546 }
547
548 nread= ssize_t(buffer_ptr - (char*)buffer);
549
550 return MEMCACHED_SUCCESS;
551 }
552
553 memcached_return_t memcached_io_slurp(memcached_instance_st* instance)
554 {
555 assert_msg(instance, "Programmer error, invalid Instance");
556 assert(memcached_is_udp(instance->root) == false);
557
558 if (instance->fd == INVALID_SOCKET)
559 {
560 assert_msg(int(instance->state) <= int(MEMCACHED_SERVER_STATE_ADDRINFO), "Invalid socket state");
561 return MEMCACHED_CONNECTION_FAILURE;
562 }
563
564 ssize_t data_read;
565 char buffer[MEMCACHED_MAX_BUFFER];
566 do
567 {
568 data_read= ::recv(instance->fd, instance->read_buffer, sizeof(buffer), MSG_NOSIGNAL);
569 if (data_read == SOCKET_ERROR)
570 {
571 switch (get_socket_errno())
572 {
573 case EINTR: // We just retry
574 continue;
575
576 case ETIMEDOUT: // OSX
577 #if EWOULDBLOCK != EAGAIN
578 case EWOULDBLOCK:
579 #endif
580 case EAGAIN:
581 #ifdef __linux
582 case ERESTART:
583 #endif
584 if (memcached_success(io_wait(instance, POLLIN)))
585 {
586 continue;
587 }
588 return MEMCACHED_IN_PROGRESS;
589
590 /* fall through */
591
592 case ENOTCONN: // Programmer Error
593 assert(0);
594 case ENOTSOCK:
595 assert(0);
596 case EBADF:
597 assert_msg(instance->fd != INVALID_SOCKET, "Invalid socket state");
598 /* fall through */
599 case EINVAL:
600 case EFAULT:
601 case ECONNREFUSED:
602 default:
603 return MEMCACHED_CONNECTION_FAILURE; // We want this!
604 }
605 }
606 } while (data_read > 0);
607
608 return MEMCACHED_CONNECTION_FAILURE;
609 }
610
611 static bool _io_write(memcached_instance_st* instance,
612 const void *buffer, size_t length, bool with_flush,
613 size_t& written)
614 {
615 assert(instance->fd != INVALID_SOCKET);
616 assert(memcached_is_udp(instance->root) == false);
617
618 const char *buffer_ptr= static_cast<const char *>(buffer);
619
620 const size_t original_length= length;
621
622 while (length)
623 {
624 char *write_ptr;
625 size_t buffer_end= MEMCACHED_MAX_BUFFER;
626 size_t should_write= buffer_end -instance->write_buffer_offset;
627 should_write= (should_write < length) ? should_write : length;
628
629 write_ptr= instance->write_buffer + instance->write_buffer_offset;
630 memcpy(write_ptr, buffer_ptr, should_write);
631 instance->write_buffer_offset+= should_write;
632 buffer_ptr+= should_write;
633 length-= should_write;
634
635 if (instance->write_buffer_offset == buffer_end)
636 {
637 WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET);
638
639 memcached_return_t rc;
640 if (io_flush(instance, with_flush, rc) == false)
641 {
642 written= original_length -length;
643 return false;
644 }
645 }
646 }
647
648 if (with_flush)
649 {
650 memcached_return_t rc;
651 WATCHPOINT_ASSERT(instance->fd != INVALID_SOCKET);
652 if (io_flush(instance, with_flush, rc) == false)
653 {
654 written= original_length -length;
655 return false;
656 }
657 }
658
659 written= original_length -length;
660
661 return true;
662 }
663
664 bool memcached_io_write(memcached_instance_st* instance)
665 {
666 size_t written;
667 return _io_write(instance, NULL, 0, true, written);
668 }
669
670 ssize_t memcached_io_write(memcached_instance_st* instance,
671 const void *buffer, const size_t length, const bool with_flush)
672 {
673 size_t written;
674
675 if (_io_write(instance, buffer, length, with_flush, written) == false)
676 {
677 return -1;
678 }
679
680 return ssize_t(written);
681 }
682
683 bool memcached_io_writev(memcached_instance_st* instance,
684 libmemcached_io_vector_st vector[],
685 const size_t number_of, const bool with_flush)
686 {
687 ssize_t complete_total= 0;
688 ssize_t total= 0;
689
690 for (size_t x= 0; x < number_of; x++, vector++)
691 {
692 complete_total+= vector->length;
693 if (vector->length)
694 {
695 size_t written;
696 if ((_io_write(instance, vector->buffer, vector->length, false, written)) == false)
697 {
698 return false;
699 }
700 total+= written;
701 }
702 }
703
704 if (with_flush)
705 {
706 if (memcached_io_write(instance) == false)
707 {
708 return false;
709 }
710 }
711
712 return (complete_total == total);
713 }
714
715 void memcached_instance_st::start_close_socket()
716 {
717 if (fd != INVALID_SOCKET)
718 {
719 shutdown(fd, SHUT_WR);
720 options.is_shutting_down= true;
721 }
722 }
723
724 void memcached_instance_st::reset_socket()
725 {
726 if (fd != INVALID_SOCKET)
727 {
728 (void)closesocket(fd);
729 fd= INVALID_SOCKET;
730 }
731 }
732
733 void memcached_instance_st::close_socket()
734 {
735 if (fd != INVALID_SOCKET)
736 {
737 int shutdown_options= SHUT_RD;
738 if (options.is_shutting_down == false)
739 {
740 shutdown_options= SHUT_RDWR;
741 }
742
743 /* in case of death shutdown to avoid blocking at close() */
744 if (shutdown(fd, shutdown_options) == SOCKET_ERROR and get_socket_errno() != ENOTCONN)
745 {
746 WATCHPOINT_NUMBER(fd);
747 WATCHPOINT_ERRNO(get_socket_errno());
748 WATCHPOINT_ASSERT(get_socket_errno());
749 }
750
751 reset_socket();
752 state= MEMCACHED_SERVER_STATE_NEW;
753 }
754
755 state= MEMCACHED_SERVER_STATE_NEW;
756 cursor_active_= 0;
757 io_bytes_sent= 0;
758 write_buffer_offset= size_t(root and memcached_is_udp(root) ? UDP_DATAGRAM_HEADER_LENGTH : 0);
759 read_buffer_length= 0;
760 read_ptr= read_buffer;
761 options.is_shutting_down= false;
762 memcached_server_response_reset(this);
763
764 // We reset the version so that if we end up talking to a different server
765 // we don't have stale server version information.
766 major_version= minor_version= micro_version= UINT8_MAX;
767 }
768
769 memcached_instance_st* memcached_io_get_readable_server(Memcached *memc, memcached_return_t&)
770 {
771 #define MAX_SERVERS_TO_POLL 100
772 struct pollfd fds[MAX_SERVERS_TO_POLL];
773 nfds_t host_index= 0;
774
775 for (uint32_t x= 0; x < memcached_server_count(memc) and host_index < MAX_SERVERS_TO_POLL; ++x)
776 {
777 memcached_instance_st* instance= memcached_instance_fetch(memc, x);
778
779 if (instance->read_buffer_length > 0) /* I have data in the buffer */
780 {
781 return instance;
782 }
783
784 if (instance->response_count() > 0)
785 {
786 fds[host_index].events= POLLIN;
787 fds[host_index].revents= 0;
788 fds[host_index].fd= instance->fd;
789 ++host_index;
790 }
791 }
792
793 if (host_index < 2)
794 {
795 /* We have 0 or 1 server with pending events.. */
796 for (uint32_t x= 0; x< memcached_server_count(memc); ++x)
797 {
798 memcached_instance_st* instance= memcached_instance_fetch(memc, x);
799
800 if (instance->response_count() > 0)
801 {
802 return instance;
803 }
804 }
805
806 return NULL;
807 }
808
809 int error= poll(fds, host_index, memc->poll_timeout);
810 switch (error)
811 {
812 case -1:
813 memcached_set_errno(*memc, get_socket_errno(), MEMCACHED_AT);
814 /* FALLTHROUGH */
815 case 0:
816 break;
817
818 default:
819 for (nfds_t x= 0; x < host_index; ++x)
820 {
821 if (fds[x].revents & POLLIN)
822 {
823 for (uint32_t y= 0; y < memcached_server_count(memc); ++y)
824 {
825 memcached_instance_st* instance= memcached_instance_fetch(memc, y);
826
827 if (instance->fd == fds[x].fd)
828 {
829 return instance;
830 }
831 }
832 }
833 }
834 }
835
836 return NULL;
837 }
838
839 /*
840 Eventually we will just kill off the server with the problem.
841 */
842 void memcached_io_reset(memcached_instance_st* instance)
843 {
844 memcached_quit_server(instance, true);
845 }
846
847 /**
848 * Read a given number of bytes from the server and place it into a specific
849 * buffer. Reset the IO channel on this server if an error occurs.
850 */
851 memcached_return_t memcached_safe_read(memcached_instance_st* instance,
852 void *dta,
853 const size_t size)
854 {
855 size_t offset= 0;
856 char *data= static_cast<char *>(dta);
857
858 while (offset < size)
859 {
860 ssize_t nread;
861 memcached_return_t rc;
862
863 while (memcached_continue(rc= memcached_io_read(instance, data + offset, size - offset, nread))) { };
864
865 if (memcached_failed(rc))
866 {
867 return rc;
868 }
869
870 offset+= size_t(nread);
871 }
872
873 return MEMCACHED_SUCCESS;
874 }
875
876 memcached_return_t memcached_io_readline(memcached_instance_st* instance,
877 char *buffer_ptr,
878 size_t size,
879 size_t& total_nr)
880 {
881 total_nr= 0;
882 bool line_complete= false;
883
884 while (line_complete == false)
885 {
886 if (instance->read_buffer_length == 0)
887 {
888 /*
889 * We don't have any data in the buffer, so let's fill the read
890 * buffer. Call the standard read function to avoid duplicating
891 * the logic.
892 */
893 ssize_t nread;
894 memcached_return_t rc= memcached_io_read(instance, buffer_ptr, 1, nread);
895 if (memcached_failed(rc) and rc == MEMCACHED_IN_PROGRESS)
896 {
897 memcached_quit_server(instance, true);
898 return memcached_set_error(*instance, rc, MEMCACHED_AT);
899 }
900 else if (memcached_failed(rc))
901 {
902 return rc;
903 }
904
905 if (*buffer_ptr == '\n')
906 {
907 line_complete= true;
908 }
909
910 ++buffer_ptr;
911 ++total_nr;
912 }
913
914 /* Now let's look in the buffer and copy as we go! */
915 while (instance->read_buffer_length and total_nr < size and line_complete == false)
916 {
917 *buffer_ptr = *instance->read_ptr;
918 if (*buffer_ptr == '\n')
919 {
920 line_complete = true;
921 }
922 --instance->read_buffer_length;
923 ++instance->read_ptr;
924 ++total_nr;
925 ++buffer_ptr;
926 }
927
928 if (total_nr == size)
929 {
930 return MEMCACHED_PROTOCOL_ERROR;
931 }
932 }
933
934 return MEMCACHED_SUCCESS;
935 }