Created a bigger buffer for write/read.
[m6w6/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 memset(buffer, 0, buffer_length);
22 send_length= 0;
23
24 max_messages= memcached_server_response_count(ptr, server_key);
25 for (x= 0; x <= max_messages; x++)
26 {
27 size_t total_length= 0;
28 buffer_ptr= buffer;
29
30 while (1)
31 {
32 unsigned int read_length;
33
34 read_length= memcached_io_read(ptr, server_key,
35 buffer_ptr, 1);
36
37 if (read_length != 1)
38 return MEMCACHED_UNKNOWN_READ_FAILURE;
39
40 if (*buffer_ptr == '\n')
41 break;
42 else
43 buffer_ptr++;
44
45 total_length++;
46 assert(total_length < buffer_length);
47 }
48
49 if (memcached_server_response_count(ptr, server_key))
50 memcached_server_response_decrement(ptr, server_key);
51 }
52
53 switch(buffer[0])
54 {
55 case 'V': /* VALUE */
56 return MEMCACHED_SUCCESS;
57 case 'O': /* OK */
58 return MEMCACHED_SUCCESS;
59 case 'S': /* STORED STATS SERVER_ERROR */
60 {
61 if (buffer[2] == 'A') /* STORED STATS */
62 return MEMCACHED_STAT;
63 else if (buffer[1] == 'E')
64 return MEMCACHED_SERVER_ERROR;
65 else if (buffer[1] == 'T')
66 return MEMCACHED_STORED;
67 else
68 return MEMCACHED_UNKNOWN_READ_FAILURE;
69 }
70 case 'D': /* DELETED */
71 return MEMCACHED_DELETED;
72 case 'N': /* NOT_FOUND */
73 {
74 if (buffer[4] == 'F')
75 return MEMCACHED_NOTFOUND;
76 else if (buffer[4] == 'S')
77 return MEMCACHED_NOTSTORED;
78 else
79 return MEMCACHED_UNKNOWN_READ_FAILURE;
80 }
81 case 'E': /* PROTOCOL ERROR or END */
82 {
83 if (buffer[1] == 'N')
84 return MEMCACHED_END;
85 else if (buffer[1] == 'R')
86 return MEMCACHED_PROTOCOL_ERROR;
87 else
88 return MEMCACHED_UNKNOWN_READ_FAILURE;
89 }
90 case 'C': /* CLIENT ERROR */
91 return MEMCACHED_CLIENT_ERROR;
92 default:
93 return MEMCACHED_UNKNOWN_READ_FAILURE;
94
95 }
96
97 return MEMCACHED_SUCCESS;
98 }