summaryrefslogtreecommitdiff
path: root/trackerlogic.c
diff options
context:
space:
mode:
authorerdgeist <>2008-12-06 18:46:00 +0000
committererdgeist <>2008-12-06 18:46:00 +0000
commit08c71627839a9134367db354c8255f7a720e1ff3 (patch)
treeb429abf6b3c26c7f851b522f5d4fb6797ad02d29 /trackerlogic.c
parenta4ed31d517f77a21afe12918a9cb256489daac74 (diff)
Renamed OT_FLAG to OT_PEERFLAG to make code easier to read
Introduced READ16/32 and WRITE16/32 makros to abstract loading/storing from unaligned addresses away on cpu's that can actually load/store everywhere Removed all unnecessary memmoves, especially where it only moved 6 bytes in inner loop. I replaced them with WRITE16/32(READ16/32()) makros
Diffstat (limited to 'trackerlogic.c')
-rw-r--r--trackerlogic.c52
1 files changed, 29 insertions, 23 deletions
diff --git a/trackerlogic.c b/trackerlogic.c
index 4c6b5b0..d7fca69 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -62,8 +62,8 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
62 62
63 if( !exactmatch ) { 63 if( !exactmatch ) {
64 /* Create a new torrent entry, then */ 64 /* Create a new torrent entry, then */
65 memmove( &torrent->hash, hash, sizeof( ot_hash ) ); 65 int i; for(i=0;i<20;i+=4) WRITE32(&torrent->hash,i,READ32(hash,i));
66 66
67 if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { 67 if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) {
68 vector_remove_torrent( torrents_list, torrent ); 68 vector_remove_torrent( torrents_list, torrent );
69 mutex_bucket_unlock_by_hash( hash ); 69 mutex_bucket_unlock_by_hash( hash );
@@ -86,8 +86,8 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
86 OT_PEERTIME( peer ) = 0; 86 OT_PEERTIME( peer ) = 0;
87 87
88 /* Sanitize flags: Whoever claims to have completed download, must be a seeder */ 88 /* Sanitize flags: Whoever claims to have completed download, must be a seeder */
89 if( ( OT_FLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) == PEER_FLAG_COMPLETED ) 89 if( ( OT_PEERFLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) == PEER_FLAG_COMPLETED )
90 OT_FLAG( peer ) ^= PEER_FLAG_COMPLETED; 90 OT_PEERFLAG( peer ) ^= PEER_FLAG_COMPLETED;
91 91
92 /* If we hadn't had a match create peer there */ 92 /* If we hadn't had a match create peer there */
93 if( !exactmatch ) { 93 if( !exactmatch ) {
@@ -96,13 +96,13 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
96 if( !from_sync ) 96 if( !from_sync )
97 livesync_tell( hash, peer ); 97 livesync_tell( hash, peer );
98 else 98 else
99 OT_FLAG( peer ) |= PEER_FLAG_FROM_SYNC; 99 OT_PEERFLAG( peer ) |= PEER_FLAG_FROM_SYNC;
100#endif 100#endif
101 101
102 torrent->peer_list->peer_count++; 102 torrent->peer_list->peer_count++;
103 if( OT_FLAG(peer) & PEER_FLAG_COMPLETED ) 103 if( OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED )
104 torrent->peer_list->down_count++; 104 torrent->peer_list->down_count++;
105 if( OT_FLAG(peer) & PEER_FLAG_SEEDING ) 105 if( OT_PEERFLAG(peer) & PEER_FLAG_SEEDING )
106 torrent->peer_list->seed_count++; 106 torrent->peer_list->seed_count++;
107 107
108 } else { 108 } else {
@@ -114,7 +114,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
114 int i; 114 int i;
115 for( i=0;i<20;++i)printf("%02X",(*hash)[i]); 115 for( i=0;i<20;++i)printf("%02X",(*hash)[i]);
116 if( g_this_peerid_data ) g_this_peerid_data[g_this_peerid_len] = 0; 116 if( g_this_peerid_data ) g_this_peerid_data[g_this_peerid_len] = 0;
117 printf( " %d.%d.%d.%d:%d\t%d %02X %s\n", _ip[0], _ip[1], _ip[2], _ip[3], OT_PEERTIME( peer_dest ), *(uint16_t*)( ((char*)peer_dest)+4 ), OT_FLAG(peer_dest), g_this_peerid_data ? g_this_peerid_data : "-" ); 117 printf( " %d.%d.%d.%d:%d\t%d %02X %s\n", _ip[0], _ip[1], _ip[2], _ip[3], OT_PEERTIME( peer_dest ), *(uint16_t*)( ((char*)peer_dest)+4 ), OT_PEERFLAG(peer_dest), g_this_peerid_data ? g_this_peerid_data : "-" );
118 } 118 }
119#endif 119#endif
120 120
@@ -123,19 +123,19 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
123 fresh "completed" reports */ 123 fresh "completed" reports */
124 if( !from_sync ) { 124 if( !from_sync ) {
125 if( OT_PEERTIME( peer_dest ) > OT_CLIENT_SYNC_RENEW_BOUNDARY || 125 if( OT_PEERTIME( peer_dest ) > OT_CLIENT_SYNC_RENEW_BOUNDARY ||
126 ( !(OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_FLAG(peer) & PEER_FLAG_COMPLETED ) ) ) 126 ( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED ) ) )
127 livesync_tell( hash, peer ); 127 livesync_tell( hash, peer );
128 } 128 }
129#endif 129#endif
130 130
131 if( (OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_FLAG(peer) & PEER_FLAG_SEEDING ) ) 131 if( (OT_PEERFLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) )
132 torrent->peer_list->seed_count--; 132 torrent->peer_list->seed_count--;
133 if( !(OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_FLAG(peer) & PEER_FLAG_SEEDING ) ) 133 if( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) )
134 torrent->peer_list->seed_count++; 134 torrent->peer_list->seed_count++;
135 if( !(OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_FLAG(peer) & PEER_FLAG_COMPLETED ) ) 135 if( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED ) )
136 torrent->peer_list->down_count++; 136 torrent->peer_list->down_count++;
137 if( OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) 137 if( OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED )
138 OT_FLAG( peer ) |= PEER_FLAG_COMPLETED; 138 OT_PEERFLAG( peer ) |= PEER_FLAG_COMPLETED;
139 } 139 }
140 140
141 *(uint64_t*)(peer_dest) = *(uint64_t*)(peer); 141 *(uint64_t*)(peer_dest) = *(uint64_t*)(peer);
@@ -163,8 +163,10 @@ static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) {
163 for( bucket = 0; bucket<num_buckets; ++bucket ) { 163 for( bucket = 0; bucket<num_buckets; ++bucket ) {
164 ot_peer * peers = (ot_peer*)bucket_list[bucket].data; 164 ot_peer * peers = (ot_peer*)bucket_list[bucket].data;
165 size_t peer_count = bucket_list[bucket].size; 165 size_t peer_count = bucket_list[bucket].size;
166 while( peer_count-- ) 166 while( peer_count-- ) {
167 memmove( r+=6, peers++, 6 ); 167 WRITE32(r+=4,0,READ32(peers,0));
168 WRITE16(r+=2,0,READ16(peers++,4));
169 }
168 } 170 }
169 171
170 return r - reply; 172 return r - reply;
@@ -194,6 +196,8 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha
194 bucket_offset = random() % peer_list->peer_count; 196 bucket_offset = random() % peer_list->peer_count;
195 197
196 while( amount-- ) { 198 while( amount-- ) {
199 ot_peer * peer;
200
197 /* This is the aliased, non shifted range, next value may fall into */ 201 /* This is the aliased, non shifted range, next value may fall into */
198 unsigned int diff = ( ( ( amount + 1 ) * shifted_step ) >> shift ) - 202 unsigned int diff = ( ( ( amount + 1 ) * shifted_step ) >> shift ) -
199 ( ( amount * shifted_step ) >> shift ); 203 ( ( amount * shifted_step ) >> shift );
@@ -203,9 +207,9 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha
203 bucket_offset -= bucket_list[bucket_index].size; 207 bucket_offset -= bucket_list[bucket_index].size;
204 bucket_index = ( bucket_index + 1 ) % num_buckets; 208 bucket_index = ( bucket_index + 1 ) % num_buckets;
205 } 209 }
206 210 peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset;
207 memmove( r, ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset, 6 ); 211 WRITE32(r+=4,0,READ32(peer,0));
208 r += 6; 212 WRITE16(r+=2,0,READ16(peer,4));
209 } 213 }
210 return r - reply; 214 return r - reply;
211} 215}
@@ -288,9 +292,11 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash_list, int amount, char *repl
288 if( clean_single_torrent( torrent ) ) { 292 if( clean_single_torrent( torrent ) ) {
289 vector_remove_torrent( torrents_list, torrent ); 293 vector_remove_torrent( torrents_list, torrent );
290 } else { 294 } else {
291 memmove( r, "20:", 3 ); memmove( r+3, hash, 20 ); 295 int j;
292 r += sprintf( r+23, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", 296 *r++='2';*r++='0';*r++=':';
293 torrent->peer_list->seed_count, torrent->peer_list->down_count, torrent->peer_list->peer_count-torrent->peer_list->seed_count ) + 23; 297 for(j=0;j<20;j+=4) WRITE32(r+=4,0,READ32(hash,i));
298 r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee",
299 torrent->peer_list->seed_count, torrent->peer_list->down_count, torrent->peer_list->peer_count-torrent->peer_list->seed_count );
294 } 300 }
295 } 301 }
296 mutex_bucket_unlock_by_hash( hash ); 302 mutex_bucket_unlock_by_hash( hash );
@@ -310,7 +316,7 @@ size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, PROT
310 316
311#ifdef WANT_SYNC_LIVE 317#ifdef WANT_SYNC_LIVE
312 if( proto != FLAG_MCA ) { 318 if( proto != FLAG_MCA ) {
313 OT_FLAG( peer ) |= PEER_FLAG_STOPPED; 319 OT_PEERFLAG( peer ) |= PEER_FLAG_STOPPED;
314 livesync_tell( hash, peer ); 320 livesync_tell( hash, peer );
315 } 321 }
316#endif 322#endif