ref: 3997c57b572f10784bbc4d811d997d3b28ab0a12
parent: ab022ff67d0e3f2e479d4722303e26ea38172cf2
author: cinap_lenrek <[email protected]>
date: Sun May 6 13:38:20 EDT 2012
ip/torrent: keep peers in queue
--- a/sys/src/cmd/ip/torrent.c
+++ b/sys/src/cmd/ip/torrent.c
@@ -630,9 +630,9 @@
static Dict *peers, *peerqh, *peerqt;
static QLock peerslk;
static int nprocs;
- int try, fd;
char *addr;
Dict *d;
+ int fd;
if(ip == nil || port == nil)
return;
@@ -648,12 +648,10 @@
d->len = strlen(addr);
d->typ = 'd';
d->val = d;
- d->next = nil;
- if(peerqt == nil)
- peerqh = d;
- else
- peerqt->next = d;
- peerqt = d;
+ /* enqueue to front */
+ if((d->next = peerqh) == nil)
+ peerqt = d;
+ peerqh = d;
if(nprocs >= CLIPROCS){
qunlock(&peerslk);
return;
@@ -660,11 +658,29 @@
}
nprocs++;
qunlock(&peerslk);
-
if(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT))
return;
+
+ d = nil;
for(;;){
qlock(&peerslk);
+ if(d){
+ Dict **dd;
+ /* remove from peers list */
+ for(dd = &peers; *dd; dd = &((*dd)->next))
+ if(*dd == d){
+ *dd = d->next;
+ break;
+ }
+ /* enqueue to back */
+ d->next = nil;
+ if(peerqt == nil)
+ peerqh = d;
+ else
+ peerqt->next = d;
+ peerqt = d;
+ }
+ /* dequeue and put in peer list */
if(d = peerqh){
if((peerqh = d->next) == nil)
peerqt = nil;
@@ -677,14 +693,12 @@
exits(0);
addr = d->str;
if(debug) fprint(2, "client %s\n", addr);
- for(try = 0; try < 5; try++){
- if((fd = dial(addr, nil, nil, nil)) >= 0){
- if(!peer(fd, 0, addr))
- break;
- close(fd);
- }
- sleep((1000<<try)+nrand(5000));
+ if((fd = dial(addr, nil, nil, nil)) >= 0){
+ if(!peer(fd, 0, addr))
+ d = nil;
+ close(fd);
}
+ if(d) sleep(1000+nrand(5000));
}
}