Getting everything ready for non-blocking IO.
[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 size_t send_length;
16 char *buffer_ptr;
17
18 memset(buffer, 0, buffer_length);
19 send_length= 0;
20
21 buffer_ptr= buffer;
22 while (1)
23 {
24 unsigned int read_length;
25
26 read_length= memcached_io_read(ptr, server_key,
27 buffer_ptr, 1);
28
29 if (read_length != 1)
30 return MEMCACHED_UNKNOWN_READ_FAILURE;
31
32 if (*buffer_ptr == '\n')
33 break;
34 else
35 buffer_ptr++;
36 }
37
38 switch(buffer[0])
39 {
40 case 'V': /* VALUE */
41 return MEMCACHED_SUCCESS;
42 case 'O': /* OK */
43 return MEMCACHED_SUCCESS;
44 case 'S': /* STORED STATS SERVER_ERROR */
45 {
46 if (buffer[2] == 'A') /* STORED STATS */
47 return MEMCACHED_SUCCESS;
48 else if (buffer[1] == 'E')
49 return MEMCACHED_SERVER_ERROR;
50 else if (buffer[1] == 'T')
51 return MEMCACHED_STORED;
52 else
53 return MEMCACHED_UNKNOWN_READ_FAILURE;
54 }
55 case 'D': /* DELETED */
56 return MEMCACHED_DELETED;
57 case 'N': /* NOT_FOUND */
58 {
59 if (buffer[4] == 'F')
60 return MEMCACHED_NOTFOUND;
61 else if (buffer[4] == 'S')
62 return MEMCACHED_NOTSTORED;
63 else
64 return MEMCACHED_UNKNOWN_READ_FAILURE;
65 }
66 case 'E': /* PROTOCOL ERROR or END */
67 {
68 if (buffer[1] == 'N')
69 return MEMCACHED_END;
70 else if (buffer[1] == 'R')
71 return MEMCACHED_PROTOCOL_ERROR;
72 else
73 return MEMCACHED_UNKNOWN_READ_FAILURE;
74 }
75 case 'C': /* CLIENT ERROR */
76 return MEMCACHED_CLIENT_ERROR;
77 default:
78 return MEMCACHED_UNKNOWN_READ_FAILURE;
79
80 }
81
82 return MEMCACHED_SUCCESS;
83 }