diff options
| author | erdgeist <> | 2009-07-14 20:51:14 +0000 |
|---|---|---|
| committer | erdgeist <> | 2009-07-14 20:51:14 +0000 |
| commit | bb9650f55ecf1c1522f76cdbf009e25bfcdd7a6b (patch) | |
| tree | 96f303cd1c4c005d532a573145290215b091515e | |
| parent | d729c88d8843de6d78639b1fbfb85512e3049f27 (diff) | |
Order peers by whether they seed. This way clients can chose if they prefer leechers (at the beginning) or leechers (at the end of the list).
| -rw-r--r-- | trackerlogic.c | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/trackerlogic.c b/trackerlogic.c index 37cca03..18895ba 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
| @@ -183,8 +183,9 @@ size_t add_peer_to_torrent_and_return_peers( ot_hash hash, ot_peer *peer, PROTO_ | |||
| 183 | 183 | ||
| 184 | static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) { | 184 | static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) { |
| 185 | unsigned int bucket, num_buckets = 1; | 185 | unsigned int bucket, num_buckets = 1; |
| 186 | ot_vector * bucket_list = &peer_list->peers; | 186 | ot_vector * bucket_list = &peer_list->peers; |
| 187 | char * r = reply; | 187 | size_t result = OT_PEER_COMPARE_SIZE * peer_list->peer_count; |
| 188 | char * r_end = reply + result; | ||
| 188 | 189 | ||
| 189 | if( OT_PEERLIST_HASBUCKETS(peer_list) ) { | 190 | if( OT_PEERLIST_HASBUCKETS(peer_list) ) { |
| 190 | num_buckets = bucket_list->size; | 191 | num_buckets = bucket_list->size; |
| @@ -195,12 +196,16 @@ static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) { | |||
| 195 | ot_peer * peers = (ot_peer*)bucket_list[bucket].data; | 196 | ot_peer * peers = (ot_peer*)bucket_list[bucket].data; |
| 196 | size_t peer_count = bucket_list[bucket].size; | 197 | size_t peer_count = bucket_list[bucket].size; |
| 197 | while( peer_count-- ) { | 198 | while( peer_count-- ) { |
| 198 | memcpy(r,peers++,OT_PEER_COMPARE_SIZE); | 199 | if( OT_PEERFLAG(peers) & PEER_FLAG_SEEDING ) { |
| 199 | r+=OT_PEER_COMPARE_SIZE; | 200 | r_end-=OT_PEER_COMPARE_SIZE; |
| 201 | memcpy(r_end,peers++,OT_PEER_COMPARE_SIZE); | ||
| 202 | } else { | ||
| 203 | memcpy(reply,peers++,OT_PEER_COMPARE_SIZE); | ||
| 204 | reply+=OT_PEER_COMPARE_SIZE; | ||
| 205 | } | ||
| 200 | } | 206 | } |
| 201 | } | 207 | } |
| 202 | 208 | return result; | |
| 203 | return r - reply; | ||
| 204 | } | 209 | } |
| 205 | 210 | ||
| 206 | static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, char *reply ) { | 211 | static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, char *reply ) { |
| @@ -209,8 +214,9 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha | |||
| 209 | unsigned int shifted_pc = peer_list->peer_count; | 214 | unsigned int shifted_pc = peer_list->peer_count; |
| 210 | unsigned int shifted_step = 0; | 215 | unsigned int shifted_step = 0; |
| 211 | unsigned int shift = 0; | 216 | unsigned int shift = 0; |
| 212 | char * r = reply; | 217 | size_t result = OT_PEER_COMPARE_SIZE * amount; |
| 213 | 218 | char * r_end = reply + result; | |
| 219 | |||
| 214 | if( OT_PEERLIST_HASBUCKETS(peer_list) ) { | 220 | if( OT_PEERLIST_HASBUCKETS(peer_list) ) { |
| 215 | num_buckets = bucket_list->size; | 221 | num_buckets = bucket_list->size; |
| 216 | bucket_list = (ot_vector *)bucket_list->data; | 222 | bucket_list = (ot_vector *)bucket_list->data; |
| @@ -239,10 +245,15 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha | |||
| 239 | bucket_index = ( bucket_index + 1 ) % num_buckets; | 245 | bucket_index = ( bucket_index + 1 ) % num_buckets; |
| 240 | } | 246 | } |
| 241 | peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset; | 247 | peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset; |
| 242 | memcpy(r,peer,OT_PEER_COMPARE_SIZE); | 248 | if( OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) { |
| 243 | r+=OT_PEER_COMPARE_SIZE; | 249 | r_end-=OT_PEER_COMPARE_SIZE; |
| 250 | memcpy(r_end,peer,OT_PEER_COMPARE_SIZE); | ||
| 251 | } else { | ||
| 252 | memcpy(reply,peer,OT_PEER_COMPARE_SIZE); | ||
| 253 | reply+=OT_PEER_COMPARE_SIZE; | ||
| 254 | } | ||
| 244 | } | 255 | } |
| 245 | return r - reply; | 256 | return result; |
| 246 | } | 257 | } |
| 247 | 258 | ||
| 248 | /* Compiles a list of random peers for a torrent | 259 | /* Compiles a list of random peers for a torrent |
