9749b848306cafaae7b15c693196dd728e0cedf0
[awesomized/libmemcached] / libmemcached / memcached_quit.c
1 #include "common.h"
2
3 /*
4 This closes all connections (forces flush of input as well).
5
6 Maybe add a host specific, or key specific version?
7
8 The reason we send "quit" is that in case we have buffered IO, this
9 will force data to be completed.
10 */
11
12 void memcached_quit_server(memcached_server_st *ptr, uint8_t io_death)
13 {
14 if (ptr->fd != -1)
15 {
16 if (io_death == 0)
17 {
18 memcached_return rc;
19 ssize_t read_length;
20 char buffer[MEMCACHED_MAX_BUFFER];
21
22 rc= memcached_do(ptr, "quit\r\n", 6, 1);
23 WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_FETCH_NOTFINISHED);
24
25 /* read until socket is closed, or there is an error
26 * closing the socket before all data is read
27 * results in server throwing away all data which is
28 * not read
29 */
30 while ((read_length=
31 memcached_io_read(ptr, buffer, sizeof(buffer)/sizeof(*buffer)))
32 > 0);
33 }
34 memcached_io_close(ptr, io_death);
35
36 ptr->fd= -1;
37 ptr->write_buffer_offset= 0;
38 ptr->read_buffer_length= 0;
39 ptr->read_ptr= ptr->read_buffer;
40 memcached_server_response_reset(ptr);
41 }
42 }
43
44 void memcached_quit(memcached_st *ptr)
45 {
46 unsigned int x;
47
48 if (ptr->hosts == NULL ||
49 ptr->number_of_hosts == 0)
50 return;
51
52 if (ptr->hosts && ptr->number_of_hosts)
53 {
54 for (x= 0; x < ptr->number_of_hosts; x++)
55 memcached_quit_server(&ptr->hosts[x], 0);
56 }
57 }