X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fmemcached_hosts.c;h=d5ec56064534af7b1ff02de9797138eb59b3015a;hb=331a25a55b57056f81690a6573e0ef06158ed108;hp=0fcf4cdb9e7c7c38048ba470a96e68f249618224;hpb=7ef83f5c1f71a8527a5f3001d72772ac692bcf14;p=awesomized%2Flibmemcached diff --git a/lib/memcached_hosts.c b/lib/memcached_hosts.c index 0fcf4cdb..d5ec5606 100644 --- a/lib/memcached_hosts.c +++ b/lib/memcached_hosts.c @@ -1,13 +1,21 @@ #include #include "common.h" -static void host_reset(memcached_server_st *host, char *new_hostname, unsigned int port) +/* Protoypes (static) */ +static memcached_return server_add(memcached_st *ptr, char *hostname, + unsigned int port, + memcached_connection type); + +static void host_reset(memcached_server_st *host, char *new_hostname, unsigned int port, + memcached_connection type) { - host->stack_responses= 0; - host->cursor_active= 0; + memset(host, 0, sizeof(memcached_server_st)); host->hostname= new_hostname; host->port= port; host->fd= -1; + host->type= type; + host->read_ptr= host->read_buffer; + host->write_ptr= host->write_buffer; } memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list) @@ -37,25 +45,57 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l ptr->hosts[ptr->number_of_hosts].fd= list[x].fd; ptr->hosts[ptr->number_of_hosts].stack_responses= list[x].stack_responses; ptr->hosts[ptr->number_of_hosts].cursor_active= list[x].cursor_active; + ptr->hosts[ptr->number_of_hosts].type= list[x].type; ptr->number_of_hosts++; } - host_reset(&ptr->hosts[ptr->number_of_hosts], NULL, 0); + host_reset(&ptr->hosts[ptr->number_of_hosts], NULL, 0, + MEMCACHED_CONNECTION_UNKNOWN); return MEMCACHED_SUCCESS; } -memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port) +memcached_return memcached_server_add_unix_socket(memcached_st *ptr, char *filename) { - memcached_server_st *new_host_list; - char *new_hostname; - LIBMEMCACHED_MEMCACHED_SERVER_ADD_START(); + if (!filename) + return MEMCACHED_FAILURE; + + return server_add(ptr, filename, 0, MEMCACHED_CONNECTION_UNIX_SOCKET); +} +memcached_return memcached_server_add_udp(memcached_st *ptr, + char *hostname, + unsigned int port) +{ if (!port) port= MEMCACHED_DEFAULT_PORT; if (!hostname) hostname= "localhost"; + return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_UDP); +} + +memcached_return memcached_server_add(memcached_st *ptr, + char *hostname, + unsigned int port) +{ + if (!port) + port= MEMCACHED_DEFAULT_PORT; + + if (!hostname) + hostname= "localhost"; + + return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_TCP); +} + +static memcached_return server_add(memcached_st *ptr, char *hostname, + unsigned int port, + memcached_connection type) +{ + memcached_server_st *new_host_list; + char *new_hostname; + LIBMEMCACHED_MEMCACHED_SERVER_ADD_START(); + if (ptr->number_of_hosts) { @@ -63,7 +103,8 @@ memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigne sizeof(memcached_server_st) * (ptr->number_of_hosts+1)); if (!new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - host_reset(&new_host_list[ptr->number_of_hosts], NULL, 0); + host_reset(&new_host_list[ptr->number_of_hosts], NULL, 0, + MEMCACHED_CONNECTION_UNKNOWN); } else { @@ -71,8 +112,8 @@ memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigne (memcached_server_st *)malloc(sizeof(memcached_server_st) * 2); if (!new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - host_reset(&new_host_list[0], NULL, 0); - host_reset(&new_host_list[1], NULL, 0); + host_reset(&new_host_list[0], NULL, 0, MEMCACHED_CONNECTION_UNKNOWN); + host_reset(&new_host_list[1], NULL, 0, MEMCACHED_CONNECTION_UNKNOWN); } ptr->hosts= new_host_list; @@ -83,9 +124,8 @@ memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigne if (!new_hostname) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - memset(new_hostname, 0, strlen(hostname)+1); memcpy(new_hostname, hostname, strlen(hostname)); - host_reset(&ptr->hosts[ptr->number_of_hosts], new_hostname, port); + host_reset(&ptr->hosts[ptr->number_of_hosts], new_hostname, port, type); ptr->number_of_hosts++; LIBMEMCACHED_MEMCACHED_SERVER_ADD_END(); @@ -115,7 +155,7 @@ memcached_server_st *memcached_server_list_append(memcached_server_st *ptr, new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count); if (!new_host_list) goto error; - host_reset(&new_host_list[count-1], NULL, 0); + host_reset(&new_host_list[count-1], NULL, 0, MEMCACHED_CONNECTION_UNKNOWN); } else { @@ -123,8 +163,8 @@ memcached_server_st *memcached_server_list_append(memcached_server_st *ptr, new_host_list= (memcached_server_st *)malloc(sizeof(memcached_server_st) * count); if (!new_host_list) goto error; - host_reset(&new_host_list[0], NULL, 0); - host_reset(&new_host_list[1], NULL, 0); + host_reset(&new_host_list[0], NULL, 0, MEMCACHED_CONNECTION_UNKNOWN); + host_reset(&new_host_list[1], NULL, 0, MEMCACHED_CONNECTION_UNKNOWN); } new_hostname= strdup(hostname); @@ -132,7 +172,7 @@ memcached_server_st *memcached_server_list_append(memcached_server_st *ptr, if (!new_hostname) goto error; - host_reset(&new_host_list[count-2], new_hostname, port); + host_reset(&new_host_list[count-2], new_hostname, port, MEMCACHED_CONNECTION_TCP); *error= MEMCACHED_SUCCESS; return new_host_list;