2 This is a partial implementation for fetching/creating memcached_server_st objects.
6 memcached_server_st
*memcached_server_create(memcached_st
*memc
, memcached_server_st
*ptr
)
10 ptr
= (memcached_server_st
*)calloc(1, sizeof(memcached_server_st
));
13 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
15 ptr
->is_allocated
= true;
18 memset(ptr
, 0, sizeof(memcached_server_st
));
25 memcached_server_st
*memcached_server_create_with(memcached_st
*memc
, memcached_server_st
*host
,
26 const char *hostname
, unsigned int port
,
27 uint32_t weight
, memcached_connection type
)
29 host
= memcached_server_create(memc
, host
);
34 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
35 host
->root
= memc
? memc
: NULL
;
40 host
->read_ptr
= host
->read_buffer
;
42 host
->next_retry
= memc
->retry_timeout
;
43 if (type
== MEMCACHED_CONNECTION_UDP
)
45 host
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
46 memcached_io_init_udp_header(host
, 0);
52 void memcached_server_free(memcached_server_st
*ptr
)
54 memcached_quit_server(ptr
, 0);
56 if (ptr
->cached_server_error
)
57 free(ptr
->cached_server_error
);
59 if (ptr
->address_info
)
60 freeaddrinfo(ptr
->address_info
);
62 if (ptr
->is_allocated
)
63 ptr
->root
->call_free(ptr
->root
, ptr
);
65 memset(ptr
, 0, sizeof(memcached_server_st
));
69 If we do not have a valid object to clone from, we toss an error.
71 memcached_server_st
*memcached_server_clone(memcached_server_st
*clone
, memcached_server_st
*ptr
)
73 memcached_server_st
*rv
= NULL
;
75 /* We just do a normal create if ptr is missing */
79 rv
= memcached_server_create_with(ptr
->root
, clone
,
80 ptr
->hostname
, ptr
->port
, ptr
->weight
,
84 rv
->cached_errno
= ptr
->cached_errno
;
85 if (ptr
->cached_server_error
)
86 rv
->cached_server_error
= strdup(ptr
->cached_server_error
);
93 memcached_return
memcached_server_cursor(memcached_st
*ptr
,
94 memcached_server_function
*callback
,
96 unsigned int number_of_callbacks
)
100 for (y
= 0; y
< ptr
->number_of_hosts
; y
++)
104 for (x
= 0; x
< number_of_callbacks
; x
++)
106 unsigned int iferror
;
108 iferror
= (*callback
[x
])(ptr
, &ptr
->hosts
[y
], context
);
115 return MEMCACHED_SUCCESS
;
118 memcached_server_st
*memcached_server_by_key(memcached_st
*ptr
, const char *key
, size_t key_length
, memcached_return
*error
)
122 *error
= memcached_validate_key_length(key_length
,
123 ptr
->flags
& MEM_BINARY_PROTOCOL
);
124 unlikely (*error
!= MEMCACHED_SUCCESS
)
127 unlikely (ptr
->number_of_hosts
== 0)
129 *error
= MEMCACHED_NO_SERVERS
;
133 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
135 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
139 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
141 return memcached_server_clone(NULL
, &ptr
->hosts
[server_key
]);
145 const char *memcached_server_error(memcached_server_st
*ptr
)
148 return ptr
->cached_server_error
;
153 void memcached_server_error_reset(memcached_server_st
*ptr
)
155 ptr
->cached_server_error
[0]= 0;