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
*)malloc(sizeof(memcached_server_st
));
13 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
15 memset(ptr
, 0, sizeof(memcached_server_st
));
16 ptr
->is_allocated
= true;
19 memset(ptr
, 0, sizeof(memcached_server_st
));
26 memcached_server_st
*memcached_server_create_with(memcached_st
*memc
, memcached_server_st
*host
,
27 const char *hostname
, unsigned int port
,
28 uint32_t weight
, memcached_connection type
)
30 host
= memcached_server_create(memc
, host
);
35 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
36 host
->root
= memc
? memc
: NULL
;
41 host
->read_ptr
= host
->read_buffer
;
43 host
->next_retry
= memc
->retry_timeout
;
44 if (type
== MEMCACHED_CONNECTION_UDP
)
46 host
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
47 memcached_io_init_udp_header(host
, 0);
53 void memcached_server_free(memcached_server_st
*ptr
)
55 memcached_quit_server(ptr
, 0);
57 if (ptr
->address_info
)
59 freeaddrinfo(ptr
->address_info
);
60 ptr
->address_info
= NULL
;
63 if (ptr
->is_allocated
)
65 if (ptr
->root
&& ptr
->root
->call_free
)
66 ptr
->root
->call_free(ptr
->root
, ptr
);
71 memset(ptr
, 0, sizeof(memcached_server_st
));
75 If we do not have a valid object to clone from, we toss an error.
77 memcached_server_st
*memcached_server_clone(memcached_server_st
*clone
, memcached_server_st
*ptr
)
79 /* We just do a normal create if ptr is missing */
83 /* TODO We should check return type */
84 return memcached_server_create_with(ptr
->root
, clone
,
85 ptr
->hostname
, ptr
->port
, ptr
->weight
,
89 memcached_return
memcached_server_cursor(memcached_st
*ptr
,
90 memcached_server_function
*callback
,
92 unsigned int number_of_callbacks
)
96 for (y
= 0; y
< ptr
->number_of_hosts
; y
++)
100 for (x
= 0; x
< number_of_callbacks
; x
++)
102 unsigned int iferror
;
104 iferror
= (*callback
[x
])(ptr
, &ptr
->hosts
[y
], context
);
111 return MEMCACHED_SUCCESS
;
114 memcached_server_st
*memcached_server_by_key(memcached_st
*ptr
, const char *key
, size_t key_length
, memcached_return
*error
)
118 *error
= memcached_validate_key_length(key_length
,
119 ptr
->flags
& MEM_BINARY_PROTOCOL
);
120 unlikely (*error
!= MEMCACHED_SUCCESS
)
123 unlikely (ptr
->number_of_hosts
== 0)
125 *error
= MEMCACHED_NO_SERVERS
;
129 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test((char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
131 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
135 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
137 return memcached_server_clone(NULL
, &ptr
->hosts
[server_key
]);