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
;
21 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
25 memcached_server_st
*new_host_list
;
28 return MEMCACHED_SUCCESS
;
30 for (count
= 0; list
[count
].hostname
; count
++);
33 (memcached_server_st
*)realloc(ptr
->hosts
,
34 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
+ 1));
37 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
39 ptr
->hosts
= new_host_list
;
41 for (x
= 0; list
[x
].hostname
; x
++)
43 ptr
->hosts
[ptr
->number_of_hosts
].hostname
= strdup(list
[x
].hostname
);
44 ptr
->hosts
[ptr
->number_of_hosts
].port
= list
[x
].port
;
45 ptr
->hosts
[ptr
->number_of_hosts
].fd
= list
[x
].fd
;
46 ptr
->hosts
[ptr
->number_of_hosts
].stack_responses
= list
[x
].stack_responses
;
47 ptr
->hosts
[ptr
->number_of_hosts
].cursor_active
= list
[x
].cursor_active
;
48 ptr
->hosts
[ptr
->number_of_hosts
].type
= list
[x
].type
;
49 ptr
->number_of_hosts
++;
51 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], NULL
, 0,
52 MEMCACHED_CONNECTION_UNKNOWN
);
54 return MEMCACHED_SUCCESS
;
57 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
60 return MEMCACHED_FAILURE
;
62 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
65 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
70 port
= MEMCACHED_DEFAULT_PORT
;
73 hostname
= "localhost";
75 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
78 memcached_return
memcached_server_add(memcached_st
*ptr
,
83 port
= MEMCACHED_DEFAULT_PORT
;
86 hostname
= "localhost";
88 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
91 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
93 memcached_connection type
)
95 memcached_server_st
*new_host_list
;
97 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
100 if (ptr
->number_of_hosts
)
102 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
103 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
105 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
106 host_reset(&new_host_list
[ptr
->number_of_hosts
], NULL
, 0,
107 MEMCACHED_CONNECTION_UNKNOWN
);
112 (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * 2);
114 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
115 host_reset(&new_host_list
[0], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
116 host_reset(&new_host_list
[1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
119 ptr
->hosts
= new_host_list
;
122 (char *)malloc(sizeof(char) * (strlen(hostname
)+1));
125 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
127 memcpy(new_hostname
, hostname
, strlen(hostname
));
128 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], new_hostname
, port
, type
);
129 ptr
->number_of_hosts
++;
131 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
133 return MEMCACHED_SUCCESS
;
136 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
137 char *hostname
, unsigned int port
,
138 memcached_return
*error
)
141 memcached_server_st
*new_host_list
;
148 port
= MEMCACHED_DEFAULT_PORT
;
150 /* Always count so that we keep a free host at the end */
153 for (count
= 0; ptr
[count
].hostname
; count
++);
155 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
158 host_reset(&new_host_list
[count
-1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
163 new_host_list
= (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * count
);
166 host_reset(&new_host_list
[0], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
167 host_reset(&new_host_list
[1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
170 new_hostname
= strdup(hostname
);
175 host_reset(&new_host_list
[count
-2], new_hostname
, port
, MEMCACHED_CONNECTION_TCP
);
177 *error
= MEMCACHED_SUCCESS
;
178 return new_host_list
;
180 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
185 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
189 for (x
= 0; ptr
[x
].hostname
; x
++);
194 void memcached_server_list_free(memcached_server_st
*ptr
)
198 for (x
= 0; ptr
[x
].hostname
; x
++)
199 free(ptr
[x
].hostname
);