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 memcached_server_st
*_server_create(const memcached_st
*memc
, memcached_server_st
*ptr
)
21 ptr
= (memcached_server_st
*)memc
->call_calloc(memc
, 1, sizeof(memcached_server_st
));
24 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
26 ptr
->options
.is_allocated
= true;
30 memset(ptr
, 0, sizeof(memcached_server_st
));
38 memcached_server_st
*memcached_server_create_with(const memcached_st
*memc
, memcached_server_st
*host
,
39 const char *hostname
, in_port_t port
,
40 uint32_t weight
, memcached_connection_t type
)
42 host
= _server_create(memc
, host
);
47 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
48 host
->root
= memc
? memc
: NULL
;
53 host
->read_ptr
= host
->read_buffer
;
54 host
->state
.is_corked
= 0;
56 host
->next_retry
= memc
->retry_timeout
;
57 if (type
== MEMCACHED_CONNECTION_UDP
)
59 host
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
60 memcached_io_init_udp_header(host
, 0);
66 void memcached_server_free(memcached_server_st
*ptr
)
68 memcached_quit_server(ptr
, 0);
70 if (ptr
->cached_server_error
)
71 free(ptr
->cached_server_error
);
73 if (ptr
->address_info
)
74 freeaddrinfo(ptr
->address_info
);
77 if (memcached_is_allocated(ptr
))
79 ptr
->root
->call_free(ptr
->root
, ptr
);
83 memset(ptr
, 0, sizeof(memcached_server_st
));
88 If we do not have a valid object to clone from, we toss an error.
90 memcached_server_st
*memcached_server_clone(memcached_server_st
*clone
, memcached_server_st
*ptr
)
92 memcached_server_st
*rv
= NULL
;
94 /* We just do a normal create if ptr is missing */
98 rv
= memcached_server_create_with(ptr
->root
, clone
,
99 ptr
->hostname
, ptr
->port
, ptr
->weight
,
103 rv
->cached_errno
= ptr
->cached_errno
;
104 if (ptr
->cached_server_error
)
105 rv
->cached_server_error
= strdup(ptr
->cached_server_error
);
112 memcached_return_t
memcached_server_cursor(memcached_st
*ptr
,
113 memcached_server_fn
*callback
,
115 uint32_t number_of_callbacks
)
119 for (y
= 0; y
< memcached_server_count(ptr
); y
++)
122 memcached_server_instance_st
*instance
=
123 memcached_server_instance_fetch(ptr
, y
);
125 for (x
= 0; x
< number_of_callbacks
; x
++)
127 unsigned int iferror
;
129 iferror
= (*callback
[x
])(ptr
, instance
, context
);
136 return MEMCACHED_SUCCESS
;
139 memcached_server_st
*memcached_server_by_key(memcached_st
*ptr
, const char *key
, size_t key_length
, memcached_return_t
*error
)
142 memcached_server_instance_st
*instance
;
144 *error
= memcached_validate_key_length(key_length
,
145 ptr
->flags
.binary_protocol
);
146 unlikely (*error
!= MEMCACHED_SUCCESS
)
149 unlikely (memcached_server_count(ptr
) == 0)
151 *error
= MEMCACHED_NO_SERVERS
;
155 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
157 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
161 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
162 instance
= memcached_server_instance_fetch(ptr
, server_key
);
164 return memcached_server_clone(NULL
, instance
);
168 const char *memcached_server_error(memcached_server_st
*ptr
)
171 ? ptr
->cached_server_error
175 void memcached_server_error_reset(memcached_server_st
*ptr
)
177 ptr
->cached_server_error
[0]= 0;
180 memcached_server_st
*memcached_server_get_last_disconnect(memcached_st
*ptr
)
182 return ptr
->last_disconnected_server
;
185 uint32_t memcached_server_list_count(memcached_server_st
*ptr
)
189 : memcached_servers_count(ptr
);
192 void memcached_server_list_free(memcached_server_st
*ptr
)
194 server_list_free(NULL
, ptr
);