5
5
* | (__| |_| | _ <| |___
6
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
8
* Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
10
10
* This software is licensed as described in the file COPYING, which
11
11
* you should have received as part of this distribution. The terms
18
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
* KIND, either express or implied.
21
* $Id: multi.c,v 1.172 2008-05-28 20:56:19 bagder Exp $
21
* $Id: multi.c,v 1.195 2009-02-20 08:16:04 bagder Exp $
22
22
***************************************************************************/
315
316
struct Curl_sh_entry *p = (struct Curl_sh_entry *) freethis;
320
322
static size_t fd_key_compare(void*k1, size_t k1_len, void*k2, size_t k2_len)
322
324
(void) k1_len; (void) k2_len;
324
return ((*((int* ) k1)) == (*((int* ) k2))) ? 1 : 0;
326
return (*((int* ) k1)) == (*((int* ) k2));
327
329
static size_t hash_fd(void* key, size_t key_length, size_t slots_num)
419
421
return CURLM_BAD_EASY_HANDLE;
421
423
/* Now, time to add an easy handle to the multi stack */
422
easy = (struct Curl_one_easy *)calloc(sizeof(struct Curl_one_easy), 1);
424
easy = calloc(sizeof(struct Curl_one_easy), 1);
424
426
return CURLM_OUT_OF_MEMORY;
534
536
/* increase the alive-counter */
535
537
multi->num_alive++;
539
/* A somewhat crude work-around for a little glitch in update_timer() that
540
happens if the lastcall time is set to the same time when the handle is
541
removed as when the next handle is added, as then the check in
542
update_timer() that prevents calling the application multiple times with
543
the same timer infor will not trigger and then the new handle's timeout
544
will not be notified to the app.
546
The work-around is thus simply to clear the 'lastcall' variable to force
547
update_timer() to always trigger a callback to the app when a new easy
549
memset(&multi->timer_lastcall, 0, sizeof(multi->timer_lastcall));
537
551
update_timer(multi);
575
589
bool premature = (bool)(easy->state != CURLM_STATE_COMPLETED);
590
bool easy_owns_conn = (bool)(easy->easy_conn &&
591
(easy->easy_conn->data == easy->easy_handle));
577
593
/* If the 'state' is not INIT or COMPLETED, we might need to do something
578
594
nice to put the easy_handle in a good known state when this returns. */
606
622
easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
609
/* we must call Curl_done() here (if we still "own it") so that we don't
610
leave a half-baked one around */
611
if(easy->easy_conn &&
612
(easy->easy_conn->data == easy->easy_handle)) {
614
/* Curl_done() clears the conn->data field to lose the association
615
between the easy handle and the connection
617
Note that this ignores the return code simply because there's nothing
618
really useful to do with it anyway! */
619
(void)Curl_done(&easy->easy_conn, easy->result, premature);
622
/* the connection is still alive, set back the association to enable
623
the check below to trigger TRUE */
624
easy->easy_conn->data = easy->easy_handle;
625
if(easy->easy_conn) {
627
/* we must call Curl_done() here (if we still "own it") so that we don't
628
leave a half-baked one around */
629
if (easy_owns_conn) {
631
/* Curl_done() clears the conn->data field to lose the association
632
between the easy handle and the connection
634
Note that this ignores the return code simply because there's
635
nothing really useful to do with it anyway! */
636
(void)Curl_done(&easy->easy_conn, easy->result, premature);
639
/* the connection is still alive, set back the association to enable
640
the check below to trigger TRUE */
641
easy->easy_conn->data = easy->easy_handle;
644
/* Clear connection pipelines, if Curl_done above was not called */
645
Curl_getoff_all_pipelines(easy->easy_handle, easy->easy_conn);
627
648
/* If this easy_handle was the last one in charge for one or more
628
connections a the shared connection cache, we might need to keep this
649
connections in the shared connection cache, we might need to keep this
629
650
handle around until either A) the connection is closed and killed
630
651
properly, or B) another easy_handle uses the connection.
657
678
to that since we're not part of that handle anymore */
658
679
easy->easy_handle->state.connc = NULL;
660
/* and modify the connectindex since this handle can't point to the
661
connection cache anymore */
662
if(easy->easy_conn &&
681
/* Modify the connectindex since this handle can't point to the
682
connection cache anymore.
684
TODO: consider if this is really what we want. The connection cache
685
is within the multi handle and that owns the connections so we should
686
not need to touch connections like this when we just remove an easy
689
if(easy->easy_conn && easy_owns_conn &&
663
690
(easy->easy_conn->send_pipe->size +
664
691
easy->easy_conn->recv_pipe->size == 0))
665
692
easy->easy_conn->connectindex = -1;
759
786
happen when this is called from curl_multi_remove_handle() =>
760
787
singlesocket() => multi_getsock().
763
if(easy->easy_handle->state.pipe_broke ||
789
if(easy->easy_handle->state.pipe_broke || !easy->easy_conn)
768
792
if(easy->state > CURLM_STATE_CONNECT &&
769
793
easy->state < CURLM_STATE_COMPLETED) {
965
989
easy->result = Curl_is_resolved(easy->easy_conn, &dns);
992
/* Update sockets here. Mainly because the socket(s) may have been
993
closed and the application thus needs to be told, even if it is
994
likely that the same socket(s) will again be used further down. */
995
singlesocket(multi, easy);
968
997
/* Perform the next step in the connection phase, and then move on
969
998
to the WAITCONNECT state */
970
999
easy->result = Curl_async_resolved(easy->easy_conn,
1004
1033
/* this is HTTP-specific, but sending CONNECT to a proxy is HTTP... */
1005
1034
easy->result = Curl_http_connect(easy->easy_conn, &protocol_connect);
1007
if(CURLE_OK == easy->result) {
1036
if(easy->easy_conn->bits.proxy_connect_closed) {
1037
/* reset the error buffer */
1038
if(easy->easy_handle->set.errorbuffer)
1039
easy->easy_handle->set.errorbuffer[0] = '\0';
1040
easy->easy_handle->state.errorbuf = FALSE;
1042
easy->result = CURLE_OK;
1043
result = CURLM_CALL_MULTI_PERFORM;
1044
multistate(easy, CURLM_STATE_CONNECT);
1046
else if (CURLE_OK == easy->result) {
1008
1047
if(!easy->easy_conn->bits.tunnel_connecting)
1009
1048
multistate(easy, CURLM_STATE_WAITCONNECT);
1200
1239
case CURLM_STATE_WAITPERFORM:
1202
infof(easy->easy_handle, "Conn %d recv pipe %d inuse %d athead %d\n",
1203
easy->easy_conn->connectindex,
1204
easy->easy_conn->recv_pipe->size,
1205
easy->easy_conn->readchannel_inuse,
1206
isHandleAtHead(easy->easy_handle,
1207
easy->easy_conn->recv_pipe));
1209
1240
/* Wait for our turn to PERFORM */
1210
1241
if(!easy->easy_conn->readchannel_inuse &&
1211
1242
isHandleAtHead(easy->easy_handle,
1215
1246
multistate(easy, CURLM_STATE_PERFORM);
1216
1247
result = CURLM_CALL_MULTI_PERFORM;
1251
infof(easy->easy_handle, "Conn %d recv pipe %d inuse %d athead %d\n",
1252
easy->easy_conn->connectindex,
1253
easy->easy_conn->recv_pipe->size,
1254
easy->easy_conn->readchannel_inuse,
1255
isHandleAtHead(easy->easy_handle,
1256
easy->easy_conn->recv_pipe));
1220
1261
case CURLM_STATE_TOOFAST: /* limit-rate exceeded in either direction */
1264
1305
if(easy->result) {
1265
1306
/* The transfer phase returned error, we mark the connection to get
1266
* closed to prevent being re-used. This is because we can't
1267
* possibly know if the connection is in a good shape or not now. */
1268
easy->easy_conn->bits.close = TRUE;
1269
Curl_removeHandleFromPipeline(easy->easy_handle,
1270
easy->easy_conn->recv_pipe);
1307
* closed to prevent being re-used. This is because we can't possibly
1308
* know if the connection is in a good shape or not now. Unless it is
1309
* a protocol which uses two "channels" like FTP, as then the error
1310
* happened in the data connection.
1312
if(!(easy->easy_conn->protocol & PROT_DUALCHANNEL))
1313
easy->easy_conn->bits.close = TRUE;
1272
if(CURL_SOCKET_BAD != easy->easy_conn->sock[SECONDARYSOCKET]) {
1273
/* if we failed anywhere, we must clean up the secondary socket if
1275
sclose(easy->easy_conn->sock[SECONDARYSOCKET]);
1276
easy->easy_conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
1278
1315
Curl_posttransfer(easy->easy_handle);
1279
1316
Curl_done(&easy->easy_conn, easy->result, FALSE);
1297
1334
/* Check if we can move pending requests to send pipe */
1298
1335
checkPendPipeline(easy->easy_conn);
1300
/* When we follow redirects, must to go back to the CONNECT state */
1337
/* When we follow redirects or is set to retry the connection, we must
1338
to go back to the CONNECT state */
1301
1339
if(easy->easy_handle->req.newurl || retry) {
1303
1341
/* if the URL is a follow-location and not just a retried request
1324
1362
/* after the transfer is done, go DONE */
1364
/* but first check to see if we got a location info even though we're
1365
not following redirects */
1366
if (easy->easy_handle->req.location) {
1367
newurl = easy->easy_handle->req.location;
1368
easy->easy_handle->req.location = NULL;
1369
easy->result = Curl_follow(easy->easy_handle, newurl, FOLLOW_FAKE);
1325
1374
multistate(easy, CURLM_STATE_DONE);
1326
1375
result = CURLM_CALL_MULTI_PERFORM;
1682
1736
if(s != CURL_SOCKET_BAD) {
1683
/* this socket has been removed. Remove it */
1738
/* this socket has been removed. Tell the app to remove it */
1739
remove_sock_from_hash = TRUE;
1685
1741
entry = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
1743
/* check if the socket to be removed serves a connection which has
1744
other easy-s in a pipeline. In this case the socket should not be
1746
struct connectdata *easy_conn;
1748
easy_by_hash = entry->easy->multi_pos;
1749
easy_conn = easy_by_hash->easy_conn;
1751
if (easy_conn->recv_pipe && easy_conn->recv_pipe->size > 1) {
1752
/* the handle should not be removed from the pipe yet */
1753
remove_sock_from_hash = FALSE;
1755
/* Update the sockhash entry to instead point to the next in line
1756
for the recv_pipe, or the first (in case this particular easy
1758
if (entry->easy == easy->easy_handle) {
1759
if (isHandleAtHead(easy->easy_handle, easy_conn->recv_pipe))
1760
entry->easy = easy_conn->recv_pipe->head->next->ptr;
1762
entry->easy = easy_conn->recv_pipe->head->ptr;
1765
if (easy_conn->send_pipe && easy_conn->send_pipe->size > 1) {
1766
/* the handle should not be removed from the pipe yet */
1767
remove_sock_from_hash = FALSE;
1769
/* Update the sockhash entry to instead point to the next in line
1770
for the send_pipe, or the first (in case this particular easy
1772
if (entry->easy == easy->easy_handle) {
1773
if (isHandleAtHead(easy->easy_handle, easy_conn->send_pipe))
1774
entry->easy = easy_conn->send_pipe->head->next->ptr;
1776
entry->easy = easy_conn->send_pipe->head->ptr;
1779
/* Don't worry about overwriting recv_pipe head with send_pipe_head,
1780
when action will be asked on the socket (see multi_socket()), the
1781
head of the correct pipe will be taken according to the
1687
1786
/* just a precaution, this socket really SHOULD be in the hash already
1688
1787
but in case it isn't, we don't have to tell the app to remove it
1689
1788
either since it never got to know about it */
1789
remove_sock_from_hash = FALSE;
1791
if (remove_sock_from_hash) {
1690
1792
multi->socket_cb(easy->easy_handle,
1692
1794
CURL_POLL_REMOVE,
1693
1795
multi->socket_userp,
1694
1796
entry ? entry->socketp : NULL);
1696
1797
sh_delentry(multi->sockhash, s);
1747
1849
/* bad bad bad bad bad bad bad */
1748
1850
return CURLM_INTERNAL_ERROR;
1852
/* If the pipeline is enabled, take the handle which is in the head of
1853
the pipeline. If we should write into the socket, take the send_pipe
1854
head. If we should read from the socket, take the recv_pipe head. */
1855
if(data->set.one_easy->easy_conn) {
1856
if ((ev_bitmask & CURL_POLL_OUT) &&
1857
data->set.one_easy->easy_conn->send_pipe &&
1858
data->set.one_easy->easy_conn->send_pipe->head)
1859
data = data->set.one_easy->easy_conn->send_pipe->head->ptr;
1861
if ((ev_bitmask & CURL_POLL_IN) &&
1862
data->set.one_easy->easy_conn->recv_pipe &&
1863
data->set.one_easy->easy_conn->recv_pipe->head)
1864
data = data->set.one_easy->easy_conn->recv_pipe->head->ptr;
1750
1867
if(data->set.one_easy->easy_conn) /* set socket event bitmask */
1751
1868
data->set.one_easy->easy_conn->cselect_bits = ev_bitmask;
1897
2014
/* splay the lowest to the bottom */
1898
2015
multi->timetree = Curl_splay(tv_zero, multi->timetree);
1900
if(Curl_splaycomparekeys(multi->timetree->key, now) > 0)
2017
if(Curl_splaycomparekeys(multi->timetree->key, now) > 0) {
1901
2018
/* some time left before expiration */
1902
2019
*timeout_ms = curlx_tvdiff(multi->timetree->key, now);
2022
* Since we only provide millisecond resolution on the returned value
2023
* and the diff might be less than one millisecond here, we don't
2024
* return zero as that may cause short bursts of busyloops on fast
2025
* processors while the diff is still present but less than one
2026
* millisecond! instead we return 1 until the time is ripe.
1904
2031
/* 0 means immediately */
1905
2032
*timeout_ms = 0;
1973
2100
int result = 0;
1974
2101
struct curl_llist_element *sendhead = conn->send_pipe->head;
1976
if (conn->server_supports_pipelining) {
1977
size_t pipeLen = conn->send_pipe->size + conn->recv_pipe->size;
2103
size_t pipeLen = conn->send_pipe->size + conn->recv_pipe->size;
2104
if (conn->server_supports_pipelining || pipeLen == 0) {
1978
2105
struct curl_llist_element *curr = conn->pend_pipe->head;
2106
const size_t maxPipeLen =
2107
conn->server_supports_pipelining ? MAX_PIPELINE_LENGTH : 1;
1980
while(pipeLen < MAX_PIPELINE_LENGTH && curr) {
2109
while(pipeLen < maxPipeLen && curr) {
1981
2110
Curl_llist_move(conn->pend_pipe, curr,
1982
2111
conn->send_pipe, conn->send_pipe->tail);
1983
Curl_pgrsTime(curr->ptr, TIMER_CONNECT);
2112
Curl_pgrsTime(curr->ptr, TIMER_PRETRANSFER);
1984
2113
++result; /* count how many handles we moved */
1985
2114
curr = conn->pend_pipe->head;
1989
conn->now = Curl_tvnow();
2120
conn->now = Curl_tvnow();
1993
2121
/* something moved, check for a new send pipeline leader */
1994
2122
if(sendhead != conn->send_pipe->head) {
1995
2123
/* this is a new one as head, expire it */
2052
2180
/* given a number of milliseconds from now to use to set the 'act before
2053
this'-time for the transfer, to be extracted by curl_multi_timeout() */
2181
this'-time for the transfer, to be extracted by curl_multi_timeout()
2183
Pass zero to clear the timeout value for this handle.
2054
2185
void Curl_expire(struct SessionHandle *data, long milli)
2056
2187
struct Curl_multi *multi = data->multi;