Refactor (1st piece) of logic around creating messages and recieving
[awesomized/libmemcached] / lib / memcached_response.c
1 /*
2 Memcached library
3
4 memcached_response() is used to determine the return result
5 from an issued command.
6 */
7
8 #include "common.h"
9 #include "memcached_io.h"
10
11 memcached_return memcached_response(memcached_st *ptr,
12 char *buffer, size_t buffer_length,
13 unsigned int server_key)
14 {
15 unsigned int x;
16 size_t send_length;
17 char *buffer_ptr;
18 unsigned int max_messages;
19
20
21 send_length= 0;
22
23 max_messages= memcached_server_response_count(ptr, server_key);
24 for (x= 0; x < max_messages; x++)
25 {
26 size_t total_length= 0;
27 buffer_ptr= buffer;
28
29 while (1)
30 {
31 unsigned int read_length;
32
33 read_length= memcached_io_read(ptr, server_key,
34 buffer_ptr, 1);
35
36 if (read_length != 1)
37 return MEMCACHED_UNKNOWN_READ_FAILURE;
38
39 if (*buffer_ptr == '\n')
40 break;
41 else
42 buffer_ptr++;
43
44 total_length++;
45 WATCHPOINT_ASSERT(total_length <= buffer_length);
46
47 if (total_length >= buffer_length)
48 return MEMCACHED_PROTOCOL_ERROR;
49 }
50 buffer_ptr++;
51 *buffer_ptr= 0;
52
53 memcached_server_response_decrement(ptr, server_key);
54 }
55
56 switch(buffer[0])
57 {
58 case 'V': /* VALUE || VERSION */
59 if (buffer[1] == 'A') /* VALUE */
60 {
61 /* We add back in one because we will need to search for END */
62 memcached_server_response_increment(ptr, server_key);
63 return MEMCACHED_SUCCESS;
64 }
65 else if (buffer[1] == 'E') /* VERSION */
66 {
67 return MEMCACHED_SUCCESS;
68 }
69 else
70 {
71 WATCHPOINT_STRING(buffer);
72 WATCHPOINT_ASSERT(0);
73 return MEMCACHED_UNKNOWN_READ_FAILURE;
74 }
75 case 'O': /* OK */
76 return MEMCACHED_SUCCESS;
77 case 'S': /* STORED STATS SERVER_ERROR */
78 {
79 if (buffer[2] == 'A') /* STORED STATS */
80 {
81 memcached_server_response_increment(ptr, server_key);
82 return MEMCACHED_STAT;
83 }
84 else if (buffer[1] == 'E')
85 return MEMCACHED_SERVER_ERROR;
86 else if (buffer[1] == 'T')
87 return MEMCACHED_STORED;
88 else
89 {
90 WATCHPOINT_STRING(buffer);
91 WATCHPOINT_ASSERT(0);
92 return MEMCACHED_UNKNOWN_READ_FAILURE;
93 }
94 }
95 case 'D': /* DELETED */
96 return MEMCACHED_DELETED;
97 case 'N': /* NOT_FOUND */
98 {
99 if (buffer[4] == 'F')
100 return MEMCACHED_NOTFOUND;
101 else if (buffer[4] == 'S')
102 return MEMCACHED_NOTSTORED;
103 else
104 return MEMCACHED_UNKNOWN_READ_FAILURE;
105 }
106 case 'E': /* PROTOCOL ERROR or END */
107 {
108 if (buffer[1] == 'N')
109 return MEMCACHED_END;
110 else if (buffer[1] == 'R')
111 return MEMCACHED_PROTOCOL_ERROR;
112 else
113 return MEMCACHED_UNKNOWN_READ_FAILURE;
114 }
115 case 'C': /* CLIENT ERROR */
116 return MEMCACHED_CLIENT_ERROR;
117 default:
118 return MEMCACHED_UNKNOWN_READ_FAILURE;
119
120 }
121
122 return MEMCACHED_SUCCESS;
123 }
124
125 char *memcached_result_value(memcached_result_st *ptr)
126 {
127 memcached_string_st *sptr= &ptr->value;
128 return memcached_string_value(sptr);
129 }
130
131 size_t memcached_result_length(memcached_result_st *ptr)
132 {
133 memcached_string_st *sptr= &ptr->value;
134 return memcached_string_length(sptr);
135 }