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
= 0;
36 self
->micro_version
= 0;
37 self
->minor_version
= 0;
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
))
118 libmemcached_free(self
->root
, self
);
122 self
->options
.is_initialized
= false;
127 If we do not have a valid object to clone from, we toss an error.
129 memcached_server_st
*memcached_server_clone(memcached_server_st
*destination
,
130 const memcached_server_st
*source
)
132 /* We just do a normal create if source is missing */
136 destination
= memcached_server_create_with(source
->root
, destination
,
137 source
->hostname
, source
->port
, source
->weight
,
139 if (destination
!= NULL
)
141 destination
->cached_errno
= source
->cached_errno
;
143 if (source
->cached_server_error
)
144 destination
->cached_server_error
= strdup(source
->cached_server_error
);
151 memcached_return_t
memcached_server_cursor(const memcached_st
*ptr
,
152 const memcached_server_fn
*callback
,
154 uint32_t number_of_callbacks
)
156 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
158 memcached_server_instance_st instance
=
159 memcached_server_instance_by_position(ptr
, x
);
161 for (uint32_t y
= 0; y
< number_of_callbacks
; y
++)
163 unsigned int iferror
;
165 iferror
= (*callback
[y
])(ptr
, instance
, context
);
172 return MEMCACHED_SUCCESS
;
175 memcached_return_t
memcached_server_execute(memcached_st
*ptr
,
176 memcached_server_execute_fn callback
,
179 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
181 memcached_server_write_instance_st instance
=
182 memcached_server_instance_fetch(ptr
, x
);
184 unsigned int iferror
;
186 iferror
= (*callback
)(ptr
, instance
, context
);
192 return MEMCACHED_SUCCESS
;
195 memcached_server_instance_st
memcached_server_by_key(const memcached_st
*ptr
,
198 memcached_return_t
*error
)
201 memcached_server_instance_st instance
;
203 *error
= memcached_validate_key_length(key_length
,
204 ptr
->flags
.binary_protocol
);
205 unlikely (*error
!= MEMCACHED_SUCCESS
)
208 unlikely (memcached_server_count(ptr
) == 0)
210 *error
= MEMCACHED_NO_SERVERS
;
214 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
216 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
220 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
221 instance
= memcached_server_instance_by_position(ptr
, server_key
);
227 void memcached_server_error_reset(memcached_server_st
*ptr
)
229 ptr
->cached_server_error
[0]= 0;
232 memcached_server_instance_st
memcached_server_get_last_disconnect(const memcached_st
*ptr
)
234 return ptr
->last_disconnected_server
;
237 void memcached_server_list_free(memcached_server_list_st self
)
242 const memcached_st
*root
= self
->root
;
244 for (uint32_t x
= 0; x
< memcached_server_list_count(self
); x
++)
246 if (self
[x
].address_info
)
248 freeaddrinfo(self
[x
].address_info
);
249 self
[x
].address_info
= NULL
;
255 libmemcached_free(root
, self
);
263 uint32_t memcached_servers_set_count(memcached_server_st
*servers
, uint32_t count
)
265 return servers
->number_of_hosts
= count
;
268 uint32_t memcached_server_count(const memcached_st
*self
)
270 return self
->number_of_hosts
;
273 const char *memcached_server_name(memcached_server_instance_st self
)
275 return self
->hostname
;
278 in_port_t
memcached_server_port(memcached_server_instance_st self
)
283 uint32_t memcached_server_response_count(memcached_server_instance_st self
)
285 return self
->cursor_active
;
288 const char *memcached_server_error(memcached_server_instance_st ptr
)
291 ? ptr
->cached_server_error