9c77e71e0180dc552bff27f8922b231073a6d4a9
[m6w6/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, uint8_t io_death)
13 {
14 if (server_key > ptr->number_of_hosts)
15 {
16 WATCHPOINT_ASSERT(0);
17 return;
18 }
19
20 if (ptr->hosts[server_key].fd != -1)
21 {
22 if (io_death == 0)
23 {
24 memcached_return rc;
25 rc= memcached_do(ptr, server_key, "quit\r\n", 6, 1);
26 WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_FETCH_NOTFINISHED);
27
28 memcached_io_close(ptr, server_key);
29 }
30 else
31 WATCHPOINT_ASSERT(0);
32
33 ptr->hosts[server_key].fd= -1;
34 ptr->hosts[server_key].stack_responses= 0;
35 ptr->hosts[server_key].cursor_active= 0;
36 ptr->hosts[server_key].write_buffer_offset= 0;
37 ptr->hosts[server_key].read_buffer_length= 0;
38 ptr->hosts[server_key].read_ptr= ptr->hosts[server_key].read_buffer;
39 ptr->hosts[server_key].write_ptr= ptr->hosts[server_key].write_buffer;
40 }
41
42 ptr->connected--;
43 }
44
45 void memcached_quit(memcached_st *ptr)
46 {
47 unsigned int x;
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, x, 0);
56 }
57
58 ptr->connected= 0;
59 }