e51692fcd7fbf9fc99170f818a03f01321145c4a
[awesomized/libmemcached] / lib / 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_st *ptr, unsigned int server_key)
13 {
14 if (ptr->hosts == NULL ||
15 ptr->number_of_hosts == 0 ||
16 server_key > ptr->number_of_hosts)
17 return;
18
19 if (ptr->hosts[server_key].fd != -1)
20 {
21 if (ptr->flags & MEM_NO_BLOCK && ptr->hosts[server_key].stack_responses)
22 memcached_io_flush(ptr, server_key);
23
24 memcached_io_write(ptr, server_key, "quit\r\n", 6, 1);
25
26 close(ptr->hosts[server_key].fd);
27 ptr->hosts[server_key].fd= -1;
28 ptr->hosts[server_key].stack_responses= 0;
29 ptr->hosts[server_key].cursor_active= 0;
30 ptr->hosts[server_key].write_buffer_offset= 0;
31 ptr->hosts[server_key].read_buffer_length= 0;
32 ptr->hosts[server_key].read_ptr= ptr->hosts[server_key].read_buffer;
33 }
34
35 ptr->connected--;
36 }
37
38 void memcached_quit(memcached_st *ptr)
39 {
40 unsigned int x;
41
42 if (ptr->hosts && ptr->number_of_hosts)
43 {
44 for (x= 0; x < ptr->number_of_hosts; x++)
45 memcached_quit_server(ptr, x);
46 }
47
48 ptr->connected= 0;
49 }