Small cleanup in string.h and converted result_st macro to inline.
[awesomized/libmemcached] / libmemcached / purge.c
1 #include "common.h"
2
3 memcached_return_t memcached_purge(memcached_server_instance_st *ptr)
4 {
5 uint32_t x;
6 memcached_return_t ret= MEMCACHED_SUCCESS;
7
8 if (memcached_is_purging(ptr->root) || /* already purging */
9 (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark &&
10 ptr->io_bytes_sent < ptr->root->io_bytes_watermark) ||
11 (ptr->io_bytes_sent >= ptr->root->io_bytes_watermark &&
12 memcached_server_response_count(ptr) < 2))
13 {
14 return MEMCACHED_SUCCESS;
15 }
16
17 /* memcached_io_write and memcached_response may call memcached_purge
18 so we need to be able stop any recursion.. */
19 memcached_set_purging(ptr->root, true);
20
21 WATCHPOINT_ASSERT(ptr->fd != -1);
22 /* Force a flush of the buffer to ensure that we don't have the n-1 pending
23 requests buffered up.. */
24 if (memcached_io_write(ptr, NULL, 0, 1) == -1)
25 {
26 memcached_set_purging(ptr->root, true);
27
28 return MEMCACHED_WRITE_FAILURE;
29 }
30 WATCHPOINT_ASSERT(ptr->fd != -1);
31
32 uint32_t no_msg= memcached_server_response_count(ptr) - 1;
33 if (no_msg > 0)
34 {
35 memcached_result_st result;
36 memcached_result_st *result_ptr;
37 char buffer[SMALL_STRING_LEN];
38
39 /*
40 * We need to increase the timeout, because we might be waiting for
41 * data to be sent from the server (the commands was in the output buffer
42 * and just flushed
43 */
44 int32_t timeo= ptr->root->poll_timeout;
45 ptr->root->poll_timeout= 2000;
46
47 result_ptr= memcached_result_create(ptr->root, &result);
48 WATCHPOINT_ASSERT(result_ptr);
49
50 for (x= 0; x < no_msg; x++)
51 {
52 memcached_result_reset(result_ptr);
53 memcached_return_t rc= memcached_read_one_response(ptr, buffer,
54 sizeof (buffer),
55 result_ptr);
56 /*
57 * Purge doesn't care for what kind of command results that is received.
58 * The only kind of errors I care about if is I'm out of sync with the
59 * protocol or have problems reading data from the network..
60 */
61 if (rc== MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_UNKNOWN_READ_FAILURE)
62 {
63 WATCHPOINT_ERROR(rc);
64 ret = rc;
65 memcached_io_reset(ptr);
66 }
67
68 if (ptr->root->callbacks != NULL)
69 {
70 memcached_callback_st cb = *ptr->root->callbacks;
71 if (rc == MEMCACHED_SUCCESS)
72 {
73 for (uint32_t y= 0; y < cb.number_of_callback; y++)
74 {
75 rc = (*cb.callback[y])(ptr->root, result_ptr, cb.context);
76 if (rc != MEMCACHED_SUCCESS)
77 break;
78 }
79 }
80 }
81 }
82
83 memcached_result_free(result_ptr);
84 ptr->root->poll_timeout= timeo;
85 }
86 memcached_set_purging(ptr->root, false);
87
88 return ret;
89 }