This is a rewrite of some of the IO code to handle larger loads of set data
[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 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 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
45 if (memcached_server_response_count(ptr, server_key))
46 memcached_server_response_decrement(ptr, server_key);
47 }
48
49 switch(buffer[0])
50 {
51 case 'V': /* VALUE */
52 return MEMCACHED_SUCCESS;
53 case 'O': /* OK */
54 return MEMCACHED_SUCCESS;
55 case 'S': /* STORED STATS SERVER_ERROR */
56 {
57 if (buffer[2] == 'A') /* STORED STATS */
58 return MEMCACHED_STAT;
59 else if (buffer[1] == 'E')
60 return MEMCACHED_SERVER_ERROR;
61 else if (buffer[1] == 'T')
62 return MEMCACHED_STORED;
63 else
64 return MEMCACHED_UNKNOWN_READ_FAILURE;
65 }
66 case 'D': /* DELETED */
67 return MEMCACHED_DELETED;
68 case 'N': /* NOT_FOUND */
69 {
70 if (buffer[4] == 'F')
71 return MEMCACHED_NOTFOUND;
72 else if (buffer[4] == 'S')
73 return MEMCACHED_NOTSTORED;
74 else
75 return MEMCACHED_UNKNOWN_READ_FAILURE;
76 }
77 case 'E': /* PROTOCOL ERROR or END */
78 {
79 if (buffer[1] == 'N')
80 return MEMCACHED_END;
81 else if (buffer[1] == 'R')
82 return MEMCACHED_PROTOCOL_ERROR;
83 else
84 return MEMCACHED_UNKNOWN_READ_FAILURE;
85 }
86 case 'C': /* CLIENT ERROR */
87 return MEMCACHED_CLIENT_ERROR;
88 default:
89 return MEMCACHED_UNKNOWN_READ_FAILURE;
90
91 }
92
93 return MEMCACHED_SUCCESS;
94 }