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
->number_of_hosts
= 0;
23 self
->cursor_active
= 0;
25 self
->cached_errno
= 0;
27 self
->io_bytes_sent
= 0;
28 self
->server_failure_counter
= 0;
30 self
->state
.is_corked
= false;
31 self
->major_version
= 0;
32 self
->micro_version
= 0;
33 self
->minor_version
= 0;
35 self
->read_ptr
= self
->read_buffer
;
36 self
->cached_server_error
= NULL
;
37 self
->read_buffer_length
= 0;
38 self
->read_data_length
= 0;
39 self
->write_buffer_offset
= 0;
40 self
->address_info
= NULL
;
44 self
->next_retry
= root
->retry_timeout
;
52 self
->limit_maxbytes
= 0;
56 strncpy(self
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
59 static memcached_server_st
*_server_create(memcached_server_st
*self
, const memcached_st
*memc
)
63 self
= (memcached_server_st
*)libmemcached_malloc(memc
, sizeof(memcached_server_st
));
66 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
68 self
->options
.is_allocated
= true;
72 self
->options
.is_allocated
= false;
75 self
->options
.is_initialized
= true;
80 memcached_server_st
*memcached_server_create_with(const memcached_st
*memc
, memcached_server_st
*self
,
81 const char *hostname
, in_port_t port
,
82 uint32_t weight
, memcached_connection_t type
)
84 self
= _server_create(self
, memc
);
89 _server_init(self
, memc
, hostname
, port
, weight
, type
);
92 if (type
== MEMCACHED_CONNECTION_UDP
)
94 self
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
95 memcached_io_init_udp_header(self
, 0);
101 void memcached_server_free(memcached_server_st
*self
)
103 memcached_quit_server(self
, 0);
105 if (self
->cached_server_error
)
106 free(self
->cached_server_error
);
108 if (self
->address_info
)
109 freeaddrinfo(self
->address_info
);
111 if (memcached_is_allocated(self
))
113 libmemcached_free(self
->root
, self
);
117 self
->options
.is_initialized
= false;
122 If we do not have a valid object to clone from, we toss an error.
124 memcached_server_st
*memcached_server_clone(memcached_server_st
*destination
,
125 const memcached_server_st
*source
)
127 /* We just do a normal create if source is missing */
131 destination
= memcached_server_create_with(source
->root
, destination
,
132 source
->hostname
, source
->port
, source
->weight
,
134 if (destination
!= NULL
)
136 destination
->cached_errno
= source
->cached_errno
;
138 if (source
->cached_server_error
)
139 destination
->cached_server_error
= strdup(source
->cached_server_error
);
146 memcached_return_t
memcached_server_cursor(memcached_st
*ptr
,
147 memcached_server_fn
*callback
,
149 uint32_t number_of_callbacks
)
153 for (y
= 0; y
< memcached_server_count(ptr
); y
++)
156 memcached_server_instance_st
*instance
=
157 memcached_server_instance_fetch(ptr
, y
);
159 for (x
= 0; x
< number_of_callbacks
; x
++)
161 unsigned int iferror
;
163 iferror
= (*callback
[x
])(ptr
, instance
, context
);
170 return MEMCACHED_SUCCESS
;
173 memcached_server_st
*memcached_server_by_key(memcached_st
*ptr
, const char *key
, size_t key_length
, memcached_return_t
*error
)
176 memcached_server_instance_st
*instance
;
178 *error
= memcached_validate_key_length(key_length
,
179 ptr
->flags
.binary_protocol
);
180 unlikely (*error
!= MEMCACHED_SUCCESS
)
183 unlikely (memcached_server_count(ptr
) == 0)
185 *error
= MEMCACHED_NO_SERVERS
;
189 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
191 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
195 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
196 instance
= memcached_server_instance_fetch(ptr
, server_key
);
198 return memcached_server_clone(NULL
, instance
);
202 const char *memcached_server_error(memcached_server_st
*ptr
)
205 ? ptr
->cached_server_error
209 void memcached_server_error_reset(memcached_server_st
*ptr
)
211 ptr
->cached_server_error
[0]= 0;
214 memcached_server_st
*memcached_server_get_last_disconnect(memcached_st
*ptr
)
216 return ptr
->last_disconnected_server
;
219 uint32_t memcached_server_list_count(memcached_server_st
*ptr
)
223 : memcached_servers_count(ptr
);
226 void memcached_server_list_free(memcached_server_st
*ptr
)
228 server_list_free(NULL
, ptr
);