4 /* Protoypes (static) */
5 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
7 memcached_connection type
);
9 static void host_reset(memcached_server_st
*host
, char *new_hostname
, unsigned int port
,
10 memcached_connection type
)
12 memset(host
, 0, sizeof(memcached_server_st
));
13 host
->hostname
= new_hostname
;
17 host
->read_ptr
= host
->read_buffer
;
18 host
->write_ptr
= host
->write_buffer
;
19 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
22 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
26 memcached_server_st
*new_host_list
;
29 return MEMCACHED_SUCCESS
;
31 for (count
= 0; list
[count
].hostname
; count
++);
34 (memcached_server_st
*)realloc(ptr
->hosts
,
35 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
+ 1));
38 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
40 ptr
->hosts
= new_host_list
;
42 for (x
= 0; list
[x
].hostname
; x
++)
44 ptr
->hosts
[ptr
->number_of_hosts
].hostname
= strdup(list
[x
].hostname
);
45 ptr
->hosts
[ptr
->number_of_hosts
].port
= list
[x
].port
;
46 ptr
->hosts
[ptr
->number_of_hosts
].fd
= list
[x
].fd
;
47 ptr
->hosts
[ptr
->number_of_hosts
].stack_responses
= list
[x
].stack_responses
;
48 ptr
->hosts
[ptr
->number_of_hosts
].cursor_active
= list
[x
].cursor_active
;
49 ptr
->hosts
[ptr
->number_of_hosts
].type
= list
[x
].type
;
50 ptr
->number_of_hosts
++;
52 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], NULL
, 0,
53 MEMCACHED_CONNECTION_UNKNOWN
);
55 return MEMCACHED_SUCCESS
;
58 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
61 return MEMCACHED_FAILURE
;
63 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
66 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
71 port
= MEMCACHED_DEFAULT_PORT
;
74 hostname
= "localhost";
76 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
79 memcached_return
memcached_server_add(memcached_st
*ptr
,
84 port
= MEMCACHED_DEFAULT_PORT
;
87 hostname
= "localhost";
89 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
92 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
94 memcached_connection type
)
96 memcached_server_st
*new_host_list
;
98 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
101 if (ptr
->number_of_hosts
)
103 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
104 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
106 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
107 host_reset(&new_host_list
[ptr
->number_of_hosts
], NULL
, 0,
108 MEMCACHED_CONNECTION_UNKNOWN
);
113 (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * 2);
115 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
116 host_reset(&new_host_list
[0], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
117 host_reset(&new_host_list
[1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
120 ptr
->hosts
= new_host_list
;
123 (char *)malloc(sizeof(char) * (strlen(hostname
)+1));
126 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
128 memcpy(new_hostname
, hostname
, strlen(hostname
));
129 new_hostname
[strlen(hostname
)]= 0;
130 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], new_hostname
, port
, type
);
131 ptr
->number_of_hosts
++;
133 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
135 return MEMCACHED_SUCCESS
;
138 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
139 char *hostname
, unsigned int port
,
140 memcached_return
*error
)
143 memcached_server_st
*new_host_list
;
150 port
= MEMCACHED_DEFAULT_PORT
;
152 /* Always count so that we keep a free host at the end */
155 for (count
= 0; ptr
[count
].hostname
; count
++);
157 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
160 host_reset(&new_host_list
[count
-1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
165 new_host_list
= (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * count
);
168 host_reset(&new_host_list
[0], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
169 host_reset(&new_host_list
[1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
172 new_hostname
= strdup(hostname
);
177 host_reset(&new_host_list
[count
-2], new_hostname
, port
, MEMCACHED_CONNECTION_TCP
);
179 *error
= MEMCACHED_SUCCESS
;
180 return new_host_list
;
182 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
187 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
191 for (x
= 0; ptr
[x
].hostname
; x
++);
196 void memcached_server_list_free(memcached_server_st
*ptr
)
200 for (x
= 0; ptr
[x
].hostname
; x
++)
201 free(ptr
[x
].hostname
);