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
:
57 set_behavior_flag(ptr
, MEM_USE_SORT_HOSTS
, data
);
59 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
60 ptr
->poll_timeout
= (int32_t)data
;
62 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
63 ptr
->connect_timeout
= (int32_t)data
;
65 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
66 ptr
->retry_timeout
= (int32_t)data
;
68 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
69 ptr
->send_size
= (int32_t)data
;
72 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
73 ptr
->recv_size
= (int32_t)data
;
76 case MEMCACHED_BEHAVIOR_USER_DATA
:
77 return MEMCACHED_FAILURE
;
80 return MEMCACHED_SUCCESS
;
83 uint64_t memcached_behavior_get(memcached_st
*ptr
,
84 memcached_behavior flag
)
86 memcached_flags temp_flag
= 0;
90 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
91 temp_flag
= MEM_SUPPORT_CAS
;
93 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
94 temp_flag
= MEM_USE_CACHE_LOOKUPS
;
96 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
97 temp_flag
= MEM_NO_BLOCK
;
99 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
100 temp_flag
= MEM_BUFFER_REQUESTS
;
102 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
103 temp_flag
= MEM_TCP_NODELAY
;
105 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
106 temp_flag
= MEM_VERIFY_KEY
;
108 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
109 return ptr
->distribution
;
110 case MEMCACHED_BEHAVIOR_HASH
:
112 case MEMCACHED_BEHAVIOR_KETAMA
:
113 temp_flag
= MEM_USE_KETAMA
;
115 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
116 temp_flag
= MEM_USE_SORT_HOSTS
;
118 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
120 return (unsigned long long)ptr
->poll_timeout
;
122 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
124 return (unsigned long long)ptr
->connect_timeout
;
126 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
128 return (unsigned long long)ptr
->retry_timeout
;
130 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
133 socklen_t sock_length
= sizeof(int);
136 /* We just try the first host, and if it is down we return zero */
137 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
140 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
141 SO_SNDBUF
, &sock_size
, &sock_length
))
142 return 0; /* Zero means error */
146 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
149 socklen_t sock_length
= sizeof(int);
152 /* We just try the first host, and if it is down we return zero */
153 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
156 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
157 SO_RCVBUF
, &sock_size
, &sock_length
))
158 return 0; /* Zero means error */
162 case MEMCACHED_BEHAVIOR_USER_DATA
:
163 return MEMCACHED_FAILURE
;
166 WATCHPOINT_ASSERT(temp_flag
); /* Programming mistake if it gets this far */
167 if (ptr
->flags
& temp_flag
)
172 return MEMCACHED_SUCCESS
;