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 memcached_server_st
*memcached_server_create(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(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
= memcached_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
;
55 host
->next_retry
= memc
->retry_timeout
;
56 if (type
== MEMCACHED_CONNECTION_UDP
)
58 host
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
59 memcached_io_init_udp_header(host
, 0);
65 void memcached_server_free(memcached_server_st
*ptr
)
67 memcached_quit_server(ptr
, 0);
69 if (ptr
->cached_server_error
)
70 free(ptr
->cached_server_error
);
72 if (ptr
->address_info
)
73 freeaddrinfo(ptr
->address_info
);
76 if (memcached_is_allocated(ptr
))
78 ptr
->root
->call_free(ptr
->root
, ptr
);
82 memset(ptr
, 0, sizeof(memcached_server_st
));
87 If we do not have a valid object to clone from, we toss an error.
89 memcached_server_st
*memcached_server_clone(memcached_server_st
*clone
, memcached_server_st
*ptr
)
91 memcached_server_st
*rv
= NULL
;
93 /* We just do a normal create if ptr is missing */
97 rv
= memcached_server_create_with(ptr
->root
, clone
,
98 ptr
->hostname
, ptr
->port
, ptr
->weight
,
102 rv
->cached_errno
= ptr
->cached_errno
;
103 if (ptr
->cached_server_error
)
104 rv
->cached_server_error
= strdup(ptr
->cached_server_error
);
111 memcached_return_t
memcached_server_cursor(memcached_st
*ptr
,
112 memcached_server_fn
*callback
,
114 uint32_t number_of_callbacks
)
118 for (y
= 0; y
< memcached_server_count(ptr
); y
++)
122 for (x
= 0; x
< number_of_callbacks
; x
++)
124 unsigned int iferror
;
126 iferror
= (*callback
[x
])(ptr
, &ptr
->hosts
[y
], context
);
133 return MEMCACHED_SUCCESS
;
136 memcached_server_st
*memcached_server_by_key(memcached_st
*ptr
, const char *key
, size_t key_length
, memcached_return_t
*error
)
140 *error
= memcached_validate_key_length(key_length
,
141 ptr
->flags
.binary_protocol
);
142 unlikely (*error
!= MEMCACHED_SUCCESS
)
145 unlikely (memcached_server_count(ptr
) == 0)
147 *error
= MEMCACHED_NO_SERVERS
;
151 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
153 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
157 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
159 return memcached_server_clone(NULL
, &ptr
->hosts
[server_key
]);
163 const char *memcached_server_error(memcached_server_st
*ptr
)
166 ? ptr
->cached_server_error
170 void memcached_server_error_reset(memcached_server_st
*ptr
)
172 ptr
->cached_server_error
[0]= 0;
175 memcached_server_st
*memcached_server_get_last_disconnect(memcached_st
*ptr
)
177 return ptr
->last_disconnected_server
;
180 uint32_t memcached_server_list_count(memcached_server_st
*ptr
)
184 : memcached_servers_count(ptr
);
187 void memcached_server_list_free(memcached_server_st
*ptr
)
189 server_list_free(NULL
, ptr
);