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
->address_info
)
58 freeaddrinfo(ptr
->address_info
);
59 ptr
->address_info
= NULL
;
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 /* We just do a normal create if ptr is missing */
77 /* TODO We should check return type */
78 return memcached_server_create_with(ptr
->root
, clone
,
79 ptr
->hostname
, ptr
->port
, ptr
->weight
,
83 memcached_return
memcached_server_cursor(memcached_st
*ptr
,
84 memcached_server_function
*callback
,
86 unsigned int number_of_callbacks
)
90 for (y
= 0; y
< ptr
->number_of_hosts
; y
++)
94 for (x
= 0; x
< number_of_callbacks
; x
++)
98 iferror
= (*callback
[x
])(ptr
, &ptr
->hosts
[y
], context
);
105 return MEMCACHED_SUCCESS
;
108 memcached_server_st
*memcached_server_by_key(memcached_st
*ptr
, const char *key
, size_t key_length
, memcached_return
*error
)
112 *error
= memcached_validate_key_length(key_length
,
113 ptr
->flags
& MEM_BINARY_PROTOCOL
);
114 unlikely (*error
!= MEMCACHED_SUCCESS
)
117 unlikely (ptr
->number_of_hosts
== 0)
119 *error
= MEMCACHED_NO_SERVERS
;
123 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcached_key_test((char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
125 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
129 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
131 return memcached_server_clone(NULL
, &ptr
->hosts
[server_key
]);