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
, void *data
)
14 unsigned int *truefalse
= (unsigned int *)data
;
18 ptr
->flags
|= temp_flag
;
20 ptr
->flags
+= temp_flag
;
23 memcached_return
memcached_behavior_set(memcached_st
*ptr
,
24 memcached_behavior flag
,
29 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
30 set_behavior_flag(ptr
, MEM_SUPPORT_CAS
, data
);
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
);
37 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
38 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
);
49 case MEMCACHED_BEHAVIOR_KETAMA
:
50 set_behavior_flag(ptr
, MEM_USE_KETAMA
, data
);
52 case MEMCACHED_BEHAVIOR_USER_DATA
:
55 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
57 int32_t timeout
= (*((int32_t *)data
));
59 ptr
->poll_timeout
= timeout
;
62 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
64 ptr
->send_size
= (*((int *)data
));
68 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
70 ptr
->recv_size
= (*((int *)data
));
78 return MEMCACHED_SUCCESS
;
81 unsigned long long memcached_behavior_get(memcached_st
*ptr
,
82 memcached_behavior flag
)
84 memcached_flags temp_flag
= 0;
88 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
89 temp_flag
= MEM_SUPPORT_CAS
;
91 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
92 temp_flag
= MEM_USE_CACHE_LOOKUPS
;
94 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
95 temp_flag
= MEM_NO_BLOCK
;
97 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
98 temp_flag
= MEM_BUFFER_REQUESTS
;
100 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
101 temp_flag
= MEM_TCP_NODELAY
;
103 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
104 return ptr
->distribution
;
105 case MEMCACHED_BEHAVIOR_HASH
:
107 case MEMCACHED_BEHAVIOR_KETAMA
:
108 temp_flag
= MEM_USE_KETAMA
;
110 case MEMCACHED_BEHAVIOR_USER_DATA
:
112 //return (unsigned long long)ptr->user_data;
113 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
115 return (unsigned long long)ptr
->poll_timeout
;
117 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
120 socklen_t sock_length
= sizeof(int);
123 /* We just try the first host, and if it is down we return zero */
124 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
127 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
128 SO_SNDBUF
, &sock_size
, &sock_length
))
129 return 0; /* Zero means error */
133 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
136 socklen_t sock_length
= sizeof(int);
139 /* We just try the first host, and if it is down we return zero */
140 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
143 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
144 SO_RCVBUF
, &sock_size
, &sock_length
))
145 return 0; /* Zero means error */
151 WATCHPOINT_ASSERT(temp_flag
); /* Programming mistake if it gets this far */
152 if (ptr
->flags
& temp_flag
)
157 return MEMCACHED_SUCCESS
;