multi_init: moved init code here from add_handle

The closure_handle is "owned" by the multi handle and it is
unconditional so the setting up of it should be in the Curl_multi_handle
function rather than curl_multi_add_handle.
This commit is contained in:
Daniel Stenberg 2013-08-20 14:48:08 +02:00
parent bc7d806e3a
commit c346c4c8f9
1 changed files with 18 additions and 39 deletions

View File

@ -307,6 +307,14 @@ struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
if(!multi->msglist) if(!multi->msglist)
goto error; goto error;
/* allocate a new easy handle to use when closing cached connections */
multi->closure_handle = curl_easy_init();
if(!multi->closure_handle)
goto error;
multi->closure_handle->multi = multi;
multi->closure_handle->state.conn_cache = multi->conn_cache;
multi->max_pipeline_length = 5; multi->max_pipeline_length = 5;
return (CURLM *) multi; return (CURLM *) multi;
@ -318,6 +326,8 @@ struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
multi->hostcache = NULL; multi->hostcache = NULL;
Curl_conncache_destroy(multi->conn_cache); Curl_conncache_destroy(multi->conn_cache);
multi->conn_cache = NULL; multi->conn_cache = NULL;
Curl_close(multi->closure_handle);
multi->closure_handle = NULL;
free(multi); free(multi);
return NULL; return NULL;
@ -329,22 +339,12 @@ CURLM *curl_multi_init(void)
CURL_CONNECTION_HASH_SIZE); CURL_CONNECTION_HASH_SIZE);
} }
/*
* Store a pointed to the multi handle within the easy handle's data struct.
*/
static void easy_addmulti(struct SessionHandle *data,
void *multi)
{
data->multi = multi;
}
CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURLMcode curl_multi_add_handle(CURLM *multi_handle,
CURL *easy_handle) CURL *easy_handle)
{ {
struct curl_llist *timeoutlist; struct curl_llist *timeoutlist;
struct Curl_multi *multi = (struct Curl_multi *)multi_handle; struct Curl_multi *multi = (struct Curl_multi *)multi_handle;
struct SessionHandle *data = (struct SessionHandle *)easy_handle; struct SessionHandle *data = (struct SessionHandle *)easy_handle;
struct SessionHandle *new_closure = NULL;
/* First, make some basic checks that the CURLM handle is a good handle */ /* First, make some basic checks that the CURLM handle is a good handle */
if(!GOOD_MULTI_HANDLE(multi)) if(!GOOD_MULTI_HANDLE(multi))
@ -365,31 +365,12 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
if(!timeoutlist) if(!timeoutlist)
return CURLM_OUT_OF_MEMORY; return CURLM_OUT_OF_MEMORY;
/* In case multi handle has no closure_handle yet, allocate
a new easy handle to use when closing cached connections */
if(!multi->closure_handle) {
new_closure = (struct SessionHandle *)curl_easy_init();
if(!new_closure) {
free(data);
Curl_llist_destroy(timeoutlist, NULL);
return CURLM_OUT_OF_MEMORY;
}
}
/* /*
** No failure allowed in this function beyond this point. And * No failure allowed in this function beyond this point. And no
** no modification of easy nor multi handle allowed before this * modification of easy nor multi handle allowed before this except for
** except for potential multi's connection cache growing which * potential multi's connection cache growing which won't be undone in this
** won't be undone in this function no matter what. * function no matter what.
*/ */
/* In case a new closure handle has been initialized above, it
is associated now with the multi handle which lacked one. */
if(new_closure) {
multi->closure_handle = new_closure;
easy_addmulti(multi->closure_handle, multi_handle);
multi->closure_handle->state.conn_cache = multi->conn_cache;
}
/* Make easy handle use timeout list initialized above */ /* Make easy handle use timeout list initialized above */
data->state.timeoutlist = timeoutlist; data->state.timeoutlist = timeoutlist;
@ -440,7 +421,7 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
} }
/* make the SessionHandle refer back to this multi handle */ /* make the SessionHandle refer back to this multi handle */
easy_addmulti(easy_handle, multi_handle); data->multi = multi_handle;
/* Set the timeout for this handle to expire really soon so that it will /* Set the timeout for this handle to expire really soon so that it will
be taken care of even when this handle is added in the midst of operation be taken care of even when this handle is added in the midst of operation
@ -581,8 +562,7 @@ CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
data->easy_conn = NULL; data->easy_conn = NULL;
} }
easy_addmulti(data, NULL); /* clear the association to this multi data->multi = NULL; /* clear the association to this multi handle */
handle */
{ {
/* make sure there's no pending message in the queue sent from this easy /* make sure there's no pending message in the queue sent from this easy
@ -1836,8 +1816,7 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)
/* Clear the pointer to the connection cache */ /* Clear the pointer to the connection cache */
data->state.conn_cache = NULL; data->state.conn_cache = NULL;
data->multi = NULL; /* clear the association */
easy_addmulti(data, NULL); /* clear the association */
data = nextdata; data = nextdata;
} }