ref: 326c8d9c4261d8e58e2469adb66f5c8438a8838c
parent: 3997c57b572f10784bbc4d811d997d3b28ab0a12
author: cinap_lenrek <[email protected]>
date: Sun May 6 14:00:15 EDT 2012
ip/torrent: simplify
--- a/sys/src/cmd/ip/torrent.c
+++ b/sys/src/cmd/ip/torrent.c
@@ -627,7 +627,7 @@
void
client(char *ip, char *port)
{
- static Dict *peers, *peerqh, *peerqt;
+ static Dict *peerqh, *peerqt;
static QLock peerslk;
static int nprocs;
char *addr;
@@ -640,7 +640,7 @@
d = mallocz(sizeof(*d) + 64, 1);
snprint(addr = d->str, 64, "tcp!%s!%s", ip, port);
qlock(&peerslk);
- if(dlook(peers, addr) || dlook(peerqh, addr)){
+ if(dlook(peerqh, addr)){
qunlock(&peerslk);
free(d);
return;
@@ -661,31 +661,18 @@
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 */
+ /* dequeue and put to tail */
if(d = peerqh){
if((peerqh = d->next) == nil)
peerqt = nil;
- d->next = peers;
- peers = d;
+ d->next = nil;
+ if(peerqt)
+ peerqt->next = d;
+ else
+ peerqh = d;
+ peerqt = d;
} else
nprocs--;
qunlock(&peerslk);
@@ -694,11 +681,10 @@
addr = d->str;
if(debug) fprint(2, "client %s\n", addr);
if((fd = dial(addr, nil, nil, nil)) >= 0){
- if(!peer(fd, 0, addr))
- d = nil;
+ peer(fd, 0, addr);
close(fd);
}
- if(d) sleep(1000+nrand(5000));
+ sleep(1000+nrand(5000));
}
}