1,
curl_easy_init内部调用Curl_open创建一个结构体SessionHandle(里面包含了所以curl使用的数据和指针)并初始化一些数据,然后返回将其作为给外侧使用的句柄CURL*。
2,
curl_easy_setopt调用Curl_setopt,其内部一个switch,case所有支持的参数来设置上面init的SessionHandle中相应数据。
3,curl_easy_perform内部会创建一个multi结构并让其与相互指向
multi = data->multi_easy;
data->multi = multi;
static
CURLcode easy_perform(struct
SessionHandle *data, bool
events)
{
CURLM *multi;
CURLMcode mcode;
if(data->multi) {
failf(data, "easy handle already used in multi handle");
return CURLE_FAILED_INIT;
}
if(data->multi_easy)
multi = data->multi_easy;
else {
/* this multi handle will only ever have a single easy handled attached
to it, so make it use minimal hashes */
multi = Curl_multi_handle(1, 3);
if(!multi)
return CURLE_OUT_OF_MEMORY;
data->multi_easy = multi;
}
mcode = curl_multi_add_handle(multi, data);
data->multi = multi;
/* run the transfer */
result = events ? easy_events(multi) : easy_transfer(multi);
(void)curl_multi_remove_handle(multi, data);
return result;
}
4,curl_multi_perform内部由multi_runsingle执行真正的一些列http操作。
multi_runsingle由一个while循环内不断的switch-case 20种 CURLMstate状态执行相应操作并指定下一个状态传递操作直到结束。
static
CURLMcode multi_runsingle(struct
Curl_multi *multi,
struct
timeval
now,
struct
SessionHandle *data)
{
do {
switch(data->mstate) {
case CURLM_STATE_INIT:
result=Curl_pretransfer(data);
multistate(data, CURLM_STATE_CONNECT);
break;
case CURLM_STATE_CONNECT:
result = Curl_connect(data, &data->easy_conn,
&async, &protocol_connect);
multistate(data, CURLM_STATE_CONNECT_PEND);
break;
case CURLM_STATE_WAITCONNECT:
result = Curl_is_connected(data->easy_conn, FIRSTSOCKET, &connected);
multistate(data, data->easy_conn->bits.tunnel_proxy?
CURLM_STATE_WAITPROXYCONNECT:
CURLM_STATE_SENDPROTOCONNECT);
break;
case CURLM_STATE_PERFORM:
result = Curl_readwrite(data->easy_conn, data, &done);
multistate(data, CURLM_STATE_DONE);
break;
}while((rc == CURLM_CALL_MULTI_PERFORM) || multi_ischanged(multi, FALSE))
}
5,
case CURLM_STATE_INIT:
创建连接数据并初始化之
case CURLM_STATE_CONNECT:
为上面创建的连接结构绑定socket
case CURLM_STATE_WAITCONNECT:
使用select模型开始socket操作
case CURLM_STATE_PERFORM:
收到数据了,使用之前easy_setopt设置在SessionHandle结构中的回调函数写数据。
6,curl_easy_cleanup清空SessionHandle结构,
当然会先调用curl_multi_cleanup清空关联的multi结构。