MEMCACHED_BAD_KEY_PROVIDED has been added as an error type. When libmemcached
[m6w6/libmemcached] / lib / memcached_behavior.c
1 #include "common.h"
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netinet/tcp.h>
5
6 /*
7 This function is used to modify the behabior of running client.
8
9 We quit all connections so we can reset the sockets.
10 */
11
12 void set_behavior_flag(memcached_st *ptr, memcached_flags temp_flag, void *data)
13 {
14 unsigned int *truefalse= (unsigned int *)data;
15
16 memcached_quit(ptr);
17 if (truefalse)
18 ptr->flags|= temp_flag;
19 else
20 ptr->flags+= temp_flag;
21 }
22
23 memcached_return memcached_behavior_set(memcached_st *ptr,
24 memcached_behavior flag,
25 void *data)
26 {
27 switch (flag)
28 {
29 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
30 set_behavior_flag(ptr, MEM_SUPPORT_CAS, data);
31 break;
32 case MEMCACHED_BEHAVIOR_NO_BLOCK:
33 set_behavior_flag(ptr, MEM_NO_BLOCK, data);
34 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
35 set_behavior_flag(ptr, MEM_BUFFER_REQUESTS, data);
36 break;
37 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
38 set_behavior_flag(ptr, MEM_TCP_NODELAY, data);
39 break;
40 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
41 ptr->distribution= *(memcached_server_distribution *)(data);
42 break;
43 case MEMCACHED_BEHAVIOR_HASH:
44 ptr->hash= *(memcached_hash *)(data);
45 break;
46 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
47 set_behavior_flag(ptr, MEM_USE_CACHE_LOOKUPS, data);
48 break;
49 case MEMCACHED_BEHAVIOR_KETAMA:
50 set_behavior_flag(ptr, MEM_USE_KETAMA, data);
51 break;
52 case MEMCACHED_BEHAVIOR_USER_DATA:
53 ptr->user_data= data;
54 break;
55 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
56 {
57 int32_t timeout= (*((int32_t *)data));
58
59 ptr->poll_timeout= timeout;
60 break;
61 }
62 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
63 {
64 ptr->send_size= (*((int *)data));
65 memcached_quit(ptr);
66 break;
67 }
68 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
69 {
70 ptr->recv_size= (*((int *)data));
71 memcached_quit(ptr);
72 break;
73 }
74
75
76 }
77
78 return MEMCACHED_SUCCESS;
79 }
80
81 unsigned long long memcached_behavior_get(memcached_st *ptr,
82 memcached_behavior flag)
83 {
84 memcached_flags temp_flag= 0;
85
86 switch (flag)
87 {
88 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
89 temp_flag= MEM_SUPPORT_CAS;
90 break;
91 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
92 temp_flag= MEM_USE_CACHE_LOOKUPS;
93 break;
94 case MEMCACHED_BEHAVIOR_NO_BLOCK:
95 temp_flag= MEM_NO_BLOCK;
96 break;
97 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
98 temp_flag= MEM_BUFFER_REQUESTS;
99 break;
100 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
101 temp_flag= MEM_TCP_NODELAY;
102 break;
103 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
104 return ptr->distribution;
105 case MEMCACHED_BEHAVIOR_HASH:
106 return ptr->hash;
107 case MEMCACHED_BEHAVIOR_KETAMA:
108 temp_flag= MEM_USE_KETAMA;
109 break;
110 case MEMCACHED_BEHAVIOR_USER_DATA:
111 return 0;
112 //return (unsigned long long)ptr->user_data;
113 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
114 {
115 return (unsigned long long)ptr->poll_timeout;
116 }
117 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
118 {
119 int sock_size;
120 socklen_t sock_length= sizeof(int);
121
122 /* REFACTOR */
123 /* We just try the first host, and if it is down we return zero */
124 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
125 return 0;
126
127 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
128 SO_SNDBUF, &sock_size, &sock_length))
129 return 0; /* Zero means error */
130
131 return sock_size;
132 }
133 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
134 {
135 int sock_size;
136 socklen_t sock_length= sizeof(int);
137
138 /* REFACTOR */
139 /* We just try the first host, and if it is down we return zero */
140 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
141 return 0;
142
143 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
144 SO_RCVBUF, &sock_size, &sock_length))
145 return 0; /* Zero means error */
146
147 return sock_size;
148 }
149 }
150
151 WATCHPOINT_ASSERT(temp_flag); /* Programming mistake if it gets this far */
152 if (ptr->flags & temp_flag)
153 return 1;
154 else
155 return 0;
156
157 return MEMCACHED_SUCCESS;
158 }