2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: String structure used for libmemcached.
13 This is a partial implementation for fetching/creating memcached_server_st objects.
17 static inline void _server_init(memcached_server_st
*self
, const memcached_st
*root
,
18 const char *hostname
, in_port_t port
,
19 uint32_t weight
, memcached_connection_t type
)
21 self
->options
.sockaddr_inited
= false;
22 self
->options
.is_shutting_down
= false;
23 self
->number_of_hosts
= 0;
24 self
->cursor_active
= 0;
26 self
->cached_errno
= 0;
28 self
->io_bytes_sent
= 0;
29 self
->server_failure_counter
= 0;
31 self
->state
.is_corked
= false;
32 self
->state
.is_dead
= false;
33 WATCHPOINT_SET(self
->io_wait_count
.read
= 0);
34 WATCHPOINT_SET(self
->io_wait_count
.write
= 0);
35 self
->major_version
= UINT8_MAX
;
36 self
->micro_version
= UINT8_MAX
;
37 self
->minor_version
= UINT8_MAX
;
39 self
->read_ptr
= self
->read_buffer
;
40 self
->cached_server_error
= NULL
;
41 self
->read_buffer_length
= 0;
42 self
->read_data_length
= 0;
43 self
->write_buffer_offset
= 0;
44 self
->address_info
= NULL
;
48 self
->next_retry
= root
->retry_timeout
;
56 self
->limit_maxbytes
= 0;
60 strncpy(self
->hostname
, hostname
, NI_MAXHOST
- 1);
63 static memcached_server_st
*_server_create(memcached_server_st
*self
, const memcached_st
*memc
)
67 self
= (memcached_server_st
*)libmemcached_malloc(memc
, sizeof(memcached_server_st
));
70 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
72 self
->options
.is_allocated
= true;
76 self
->options
.is_allocated
= false;
79 self
->options
.is_initialized
= true;
84 memcached_server_st
*memcached_server_create_with(const memcached_st
*memc
,
85 memcached_server_write_instance_st self
,
86 const char *hostname
, in_port_t port
,
87 uint32_t weight
, memcached_connection_t type
)
89 self
= _server_create(self
, memc
);
94 _server_init(self
, memc
, hostname
, port
, weight
, type
);
97 if (type
== MEMCACHED_CONNECTION_UDP
)
99 self
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
100 memcached_io_init_udp_header(self
, 0);
106 void memcached_server_free(memcached_server_st
*self
)
108 memcached_quit_server(self
, false);
110 if (self
->cached_server_error
)
111 free(self
->cached_server_error
);
113 if (self
->address_info
)
114 freeaddrinfo(self
->address_info
);
116 if (memcached_is_allocated(self
))
120 libmemcached_free(self
->root
, self
);
129 self
->options
.is_initialized
= false;
134 If we do not have a valid object to clone from, we toss an error.
136 memcached_server_st
*memcached_server_clone(memcached_server_st
*destination
,
137 const memcached_server_st
*source
)
139 /* We just do a normal create if source is missing */
143 destination
= memcached_server_create_with(source
->root
, destination
,
144 source
->hostname
, source
->port
, source
->weight
,
146 if (destination
!= NULL
)
148 destination
->cached_errno
= source
->cached_errno
;
150 if (source
->cached_server_error
)
151 destination
->cached_server_error
= strdup(source
->cached_server_error
);
158 memcached_return_t
memcached_server_cursor(const memcached_st
*ptr
,
159 const memcached_server_fn
*callback
,
161 uint32_t number_of_callbacks
)
163 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
165 memcached_server_instance_st instance
=
166 memcached_server_instance_by_position(ptr
, x
);
168 for (uint32_t y
= 0; y
< number_of_callbacks
; y
++)
170 unsigned int iferror
;
172 iferror
= (*callback
[y
])(ptr
, instance
, context
);
179 return MEMCACHED_SUCCESS
;
182 memcached_return_t
memcached_server_execute(memcached_st
*ptr
,
183 memcached_server_execute_fn callback
,
186 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
188 memcached_server_write_instance_st instance
=
189 memcached_server_instance_fetch(ptr
, x
);
191 unsigned int iferror
;
193 iferror
= (*callback
)(ptr
, instance
, context
);
199 return MEMCACHED_SUCCESS
;
202 memcached_server_instance_st
memcached_server_by_key(const memcached_st
*ptr
,
205 memcached_return_t
*error
)
208 memcached_server_instance_st instance
;
210 *error
= memcached_validate_key_length(key_length
,
211 ptr
->flags
.binary_protocol
);
212 unlikely (*error
!= MEMCACHED_SUCCESS
)
215 unlikely (memcached_server_count(ptr
) == 0)
217 *error
= MEMCACHED_NO_SERVERS
;
221 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
223 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
227 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
228 instance
= memcached_server_instance_by_position(ptr
, server_key
);
234 void memcached_server_error_reset(memcached_server_st
*ptr
)
236 ptr
->cached_server_error
[0]= 0;
239 memcached_server_instance_st
memcached_server_get_last_disconnect(const memcached_st
*ptr
)
241 return ptr
->last_disconnected_server
;
244 void memcached_server_list_free(memcached_server_list_st self
)
249 const memcached_st
*root
= self
->root
;
251 for (uint32_t x
= 0; x
< memcached_server_list_count(self
); x
++)
253 if (self
[x
].address_info
)
255 freeaddrinfo(self
[x
].address_info
);
256 self
[x
].address_info
= NULL
;
262 libmemcached_free(root
, self
);
270 uint32_t memcached_servers_set_count(memcached_server_st
*servers
, uint32_t count
)
272 return servers
->number_of_hosts
= count
;
275 uint32_t memcached_server_count(const memcached_st
*self
)
277 return self
->number_of_hosts
;
280 const char *memcached_server_name(memcached_server_instance_st self
)
282 return self
->hostname
;
285 in_port_t
memcached_server_port(memcached_server_instance_st self
)
290 uint32_t memcached_server_response_count(memcached_server_instance_st self
)
292 return self
->cursor_active
;
295 const char *memcached_server_error(memcached_server_instance_st ptr
)
298 ? ptr
->cached_server_error