3 #include <sys/socket.h>
4 #include <netinet/tcp.h>
7 This function is used to modify the behabior of running client.
9 We quit all connections so we can reset the sockets.
12 void set_behavior_flag(memcached_st
*ptr
, memcached_flags temp_flag
, uint64_t data
)
15 ptr
->flags
|= temp_flag
;
17 ptr
->flags
&= ~temp_flag
;
20 memcached_return
memcached_behavior_set(memcached_st
*ptr
,
21 memcached_behavior flag
,
26 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
27 set_behavior_flag(ptr
, MEM_SUPPORT_CAS
, data
);
29 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
30 set_behavior_flag(ptr
, MEM_NO_BLOCK
, data
);
32 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
33 set_behavior_flag(ptr
, MEM_BUFFER_REQUESTS
, data
);
36 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
37 set_behavior_flag(ptr
, MEM_TCP_NODELAY
, data
);
40 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
41 ptr
->distribution
= (memcached_server_distribution
)(data
);
43 case MEMCACHED_BEHAVIOR_HASH
:
44 ptr
->hash
= (memcached_hash
)(data
);
46 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
47 set_behavior_flag(ptr
, MEM_USE_CACHE_LOOKUPS
, data
);
50 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
51 set_behavior_flag(ptr
, MEM_VERIFY_KEY
, data
);
53 case MEMCACHED_BEHAVIOR_KETAMA
:
54 set_behavior_flag(ptr
, MEM_USE_KETAMA
, data
);
56 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
58 set_behavior_flag(ptr
, MEM_USE_SORT_HOSTS
, data
);
60 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
63 run_distribution(ptr
);
68 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
69 ptr
->poll_timeout
= (int32_t)data
;
71 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
72 ptr
->connect_timeout
= (int32_t)data
;
74 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
75 ptr
->retry_timeout
= (int32_t)data
;
77 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
78 ptr
->send_size
= (int32_t)data
;
81 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
82 ptr
->recv_size
= (int32_t)data
;
85 case MEMCACHED_BEHAVIOR_USER_DATA
:
86 return MEMCACHED_FAILURE
;
89 return MEMCACHED_SUCCESS
;
92 uint64_t memcached_behavior_get(memcached_st
*ptr
,
93 memcached_behavior flag
)
95 memcached_flags temp_flag
= 0;
99 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
100 temp_flag
= MEM_SUPPORT_CAS
;
102 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
103 temp_flag
= MEM_USE_CACHE_LOOKUPS
;
105 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
106 temp_flag
= MEM_NO_BLOCK
;
108 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
109 temp_flag
= MEM_BUFFER_REQUESTS
;
111 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
112 temp_flag
= MEM_TCP_NODELAY
;
114 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
115 temp_flag
= MEM_VERIFY_KEY
;
117 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
118 return ptr
->distribution
;
119 case MEMCACHED_BEHAVIOR_HASH
:
121 case MEMCACHED_BEHAVIOR_KETAMA
:
122 temp_flag
= MEM_USE_KETAMA
;
124 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
125 temp_flag
= MEM_USE_SORT_HOSTS
;
127 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
129 return (unsigned long long)ptr
->poll_timeout
;
131 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
133 return (unsigned long long)ptr
->connect_timeout
;
135 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
137 return (unsigned long long)ptr
->retry_timeout
;
139 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
142 socklen_t sock_length
= sizeof(int);
145 /* We just try the first host, and if it is down we return zero */
146 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
149 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
150 SO_SNDBUF
, &sock_size
, &sock_length
))
151 return 0; /* Zero means error */
155 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
158 socklen_t sock_length
= sizeof(int);
161 /* We just try the first host, and if it is down we return zero */
162 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
165 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
166 SO_RCVBUF
, &sock_size
, &sock_length
))
167 return 0; /* Zero means error */
171 case MEMCACHED_BEHAVIOR_USER_DATA
:
172 return MEMCACHED_FAILURE
;
175 WATCHPOINT_ASSERT(temp_flag
); /* Programming mistake if it gets this far */
176 if (ptr
->flags
& temp_flag
)
181 return MEMCACHED_SUCCESS
;