Changeset 7609 for trunk/libtransmission/peer-msgs.c
- Timestamp:
- Jan 4, 2009, 4:29:44 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-msgs.c
r7606 r7609 35 35 #include "platform.h" /* MAX_STACK_ARRAY_SIZE */ 36 36 #include "ratecontrol.h" 37 #include "request-list.h" 37 38 #include "stats.h" 38 39 #include "torrent.h" … … 103 104 }; 104 105 105 /**106 *** REQUEST MANAGEMENT107 **/108 109 106 enum 110 107 { … … 114 111 AWAITING_BT_PIECE 115 112 }; 116 117 struct peer_request118 {119 uint32_t index;120 uint32_t offset;121 uint32_t length;122 time_t time_requested;123 };124 125 static inline tr_bool126 requestsMatch( const struct peer_request * a, const struct peer_request * b )127 {128 return (a->index==b->index) && (a->offset==b->offset) && (a->length==b->length);129 }130 131 struct request_list132 {133 uint16_t count;134 uint16_t max;135 struct peer_request * requests;136 };137 138 static const struct request_list REQUEST_LIST_INIT = { 0, 0, NULL };139 140 static void141 reqListReserve( struct request_list * list,142 uint16_t max )143 {144 if( list->max < max )145 {146 list->max = max;147 list->requests = tr_renew( struct peer_request,148 list->requests,149 list->max );150 }151 }152 153 static void154 reqListClear( struct request_list * list )155 {156 tr_free( list->requests );157 *list = REQUEST_LIST_INIT;158 }159 160 static void161 reqListCopy( struct request_list * dest, const struct request_list * src )162 {163 dest->count = dest->max = src->count;164 dest->requests = tr_memdup( src->requests, dest->count * sizeof( struct peer_request ) );165 }166 167 static void168 reqListRemoveOne( struct request_list * list,169 int i )170 {171 assert( 0 <= i && i < list->count );172 173 memmove( &list->requests[i],174 &list->requests[i + 1],175 sizeof( struct peer_request ) * ( --list->count - i ) );176 }177 178 static void179 reqListAppend( struct request_list * list,180 const struct peer_request * req )181 {182 if( ++list->count >= list->max )183 reqListReserve( list, list->max + 8 );184 185 list->requests[list->count - 1] = *req;186 }187 188 static int189 reqListPop( struct request_list * list,190 struct peer_request * setme )191 {192 int success;193 194 if( !list->count )195 success = FALSE;196 else {197 *setme = list->requests[0];198 reqListRemoveOne( list, 0 );199 success = TRUE;200 }201 202 return success;203 }204 205 static int206 reqListFind( struct request_list * list,207 const struct peer_request * key )208 {209 uint16_t i;210 211 for( i = 0; i < list->count; ++i )212 if( requestsMatch( key, list->requests + i ) )213 return i;214 215 return -1;216 }217 218 static int219 reqListRemove( struct request_list * list,220 const struct peer_request * key )221 {222 int success;223 const int i = reqListFind( list, key );224 225 if( i < 0 )226 success = FALSE;227 else {228 reqListRemoveOne( list, i );229 success = TRUE;230 }231 232 return success;233 }234 113 235 114 /** … … 861 740 const time_t oldestAllowed ) 862 741 { 863 int i; 864 865 /* walk through the list, looking for the first req that's too old */ 866 for( i=0; i<list->count; ++i ) { 867 const struct peer_request * req = &list->requests[i]; 868 if( req->time_requested < oldestAllowed ) 742 size_t i; 743 struct request_list tmp = REQUEST_LIST_INIT; 744 745 /* since the fifo list is sorted by time, the oldest will be first */ 746 if( list->fifo[0].time_requested >= oldestAllowed ) 747 return; 748 749 /* if we found one too old, start pruning them */ 750 reqListCopy( &tmp, list ); 751 for( i=0; i<tmp.len; ++i ) { 752 const struct peer_request * req = &tmp.fifo[i]; 753 if( req->time_requested >= oldestAllowed ) 869 754 break; 870 } 871 872 /* if we found one too old, start pruning them */ 873 if( i < list->count ) { 874 struct request_list tmp = REQUEST_LIST_INIT; 875 reqListCopy( &tmp, list ); 876 for( ; i<tmp.count; ++i ) { 877 const struct peer_request * req = &tmp.requests[i]; 878 if( req->time_requested < oldestAllowed ) 879 tr_peerMsgsCancel( msgs, req->index, req->offset, req->length ); 880 } 881 reqListClear( &tmp ); 882 } 755 tr_peerMsgsCancel( msgs, req->index, req->offset, req->length ); 756 } 757 reqListClear( &tmp ); 883 758 } 884 759 … … 909 784 const int max = msgs->maxActiveRequests; 910 785 int sent = 0; 911 int count = msgs->clientAskedFor.count;786 int len = msgs->clientAskedFor.len; 912 787 struct peer_request req; 913 788 … … 917 792 return; 918 793 919 while( ( count< max ) && reqListPop( &msgs->clientWillAskFor, &req ) )794 while( ( len < max ) && reqListPop( &msgs->clientWillAskFor, &req ) ) 920 795 { 921 796 const tr_block_index_t block = _tr_block( msgs->torrent, req.index, req.offset ); … … 932 807 reqListAppend( &msgs->clientAskedFor, &req ); 933 808 934 ++ count;809 ++len; 935 810 ++sent; 936 811 } … … 939 814 if( sent ) 940 815 dbgmsg( msgs, "pump sent %d requests, now have %d active and %d queued", 941 sent, msgs->clientAskedFor. count, msgs->clientWillAskFor.count);942 943 if( count< max )816 sent, msgs->clientAskedFor.len, msgs->clientWillAskFor.len ); 817 818 if( len < max ) 944 819 fireNeedReq( msgs ); 945 820 } … … 949 824 { 950 825 const int req_max = msgs->maxActiveRequests; 951 return msgs->clientWillAskFor. count >=req_max;826 return msgs->clientWillAskFor.len >= (size_t)req_max; 952 827 } 953 828 … … 988 863 req.offset = offset; 989 864 req.length = length; 990 if( reqList Find( &msgs->clientAskedFor, &req ) != -1) {865 if( reqListHas( &msgs->clientAskedFor, &req ) ) { 991 866 dbgmsg( msgs, "declining because it's a duplicate" ); 992 867 return TR_ADDREQ_DUPLICATE; 993 868 } 994 if( reqList Find( &msgs->clientWillAskFor, &req ) != -1) {869 if( reqListHas( &msgs->clientWillAskFor, &req ) ) { 995 870 dbgmsg( msgs, "declining because it's a duplicate" ); 996 871 return TR_ADDREQ_DUPLICATE; … … 1011 886 cancelAllRequestsToPeer( tr_peermsgs * msgs, tr_bool sendCancel ) 1012 887 { 1013 int i;888 size_t i; 1014 889 struct request_list a = msgs->clientWillAskFor; 1015 890 struct request_list b = msgs->clientAskedFor; … … 1019 894 msgs->clientWillAskFor = REQUEST_LIST_INIT; 1020 895 1021 for( i=0; i<a. count; ++i )1022 fireCancelledReq( msgs, &a. requests[i] );1023 1024 for( i = 0; i < b. count; ++i ) {1025 fireCancelledReq( msgs, &b. requests[i] );896 for( i=0; i<a.len; ++i ) 897 fireCancelledReq( msgs, &a.fifo[i] ); 898 899 for( i = 0; i < b.len; ++i ) { 900 fireCancelledReq( msgs, &b.fifo[i] ); 1026 901 if( sendCancel ) 1027 protocolSendCancel( msgs, &b. requests[i] );902 protocolSendCancel( msgs, &b.fifo[i] ); 1028 903 } 1029 904 … … 1687 1562 1688 1563 dbgmsg( msgs, "peer has %d more blocks we've asked for", 1689 msgs->clientAskedFor. count);1564 msgs->clientAskedFor.len ); 1690 1565 1691 1566 /**
Note: See TracChangeset
for help on using the changeset viewer.