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 host
->stack_responses
= 0;
13 host
->cursor_active
= 0;
14 host
->hostname
= new_hostname
;
18 host
->write_buffer_offset
= 0;
19 host
->read_buffer_length
= 0;
20 host
->read_ptr
= host
->read_buffer
;
23 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
27 memcached_server_st
*new_host_list
;
30 return MEMCACHED_SUCCESS
;
32 for (count
= 0; list
[count
].hostname
; count
++);
35 (memcached_server_st
*)realloc(ptr
->hosts
,
36 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
+ 1));
39 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
41 ptr
->hosts
= new_host_list
;
43 for (x
= 0; list
[x
].hostname
; x
++)
45 ptr
->hosts
[ptr
->number_of_hosts
].hostname
= strdup(list
[x
].hostname
);
46 ptr
->hosts
[ptr
->number_of_hosts
].port
= list
[x
].port
;
47 ptr
->hosts
[ptr
->number_of_hosts
].fd
= list
[x
].fd
;
48 ptr
->hosts
[ptr
->number_of_hosts
].stack_responses
= list
[x
].stack_responses
;
49 ptr
->hosts
[ptr
->number_of_hosts
].cursor_active
= list
[x
].cursor_active
;
50 ptr
->hosts
[ptr
->number_of_hosts
].type
= list
[x
].type
;
51 ptr
->number_of_hosts
++;
53 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], NULL
, 0,
54 MEMCACHED_CONNECTION_UNKNOWN
);
56 return MEMCACHED_SUCCESS
;
59 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
62 return MEMCACHED_FAILURE
;
64 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
67 memcached_return
memcached_server_add(memcached_st
*ptr
, char *hostname
, unsigned int port
)
70 port
= MEMCACHED_DEFAULT_PORT
;
73 hostname
= "localhost";
75 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
78 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
80 memcached_connection type
)
82 memcached_server_st
*new_host_list
;
84 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
87 if (ptr
->number_of_hosts
)
89 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
90 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
92 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
93 host_reset(&new_host_list
[ptr
->number_of_hosts
], NULL
, 0,
94 MEMCACHED_CONNECTION_UNKNOWN
);
99 (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * 2);
101 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
102 host_reset(&new_host_list
[0], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
103 host_reset(&new_host_list
[1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
106 ptr
->hosts
= new_host_list
;
109 (char *)malloc(sizeof(char) * (strlen(hostname
)+1));
112 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
114 memset(new_hostname
, 0, strlen(hostname
)+1);
115 memcpy(new_hostname
, hostname
, strlen(hostname
));
116 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], new_hostname
, port
, type
);
117 ptr
->number_of_hosts
++;
119 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
121 return MEMCACHED_SUCCESS
;
124 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
125 char *hostname
, unsigned int port
,
126 memcached_return
*error
)
129 memcached_server_st
*new_host_list
;
136 port
= MEMCACHED_DEFAULT_PORT
;
138 /* Always count so that we keep a free host at the end */
141 for (count
= 0; ptr
[count
].hostname
; count
++);
143 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
146 host_reset(&new_host_list
[count
-1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
151 new_host_list
= (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * count
);
154 host_reset(&new_host_list
[0], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
155 host_reset(&new_host_list
[1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
158 new_hostname
= strdup(hostname
);
163 host_reset(&new_host_list
[count
-2], new_hostname
, port
, MEMCACHED_CONNECTION_TCP
);
165 *error
= MEMCACHED_SUCCESS
;
166 return new_host_list
;
168 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
173 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
177 for (x
= 0; ptr
[x
].hostname
; x
++);
182 void memcached_server_list_free(memcached_server_st
*ptr
)
186 for (x
= 0; ptr
[x
].hostname
; x
++)
187 free(ptr
[x
].hostname
);