Plan 9 from Bell Labs’s /usr/web/sources/contrib/mjl/wip/deluge/piece.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "deluge.h"


void
pieceinit(Piece *p, int n, uchar *hash, ulong length)
{
	p->n = n;
	memmove(p->hash, hash, Piecehashlen);
	p->ids = nil;
	p->nids = 0;
	p->length = length;
	p->bites = bitnew((length+Bitelength-1) / Bitelength, nil);
	p->reqbites = bitnew((length+Bitelength-1) / Bitelength, nil);
}

int
piecehaspeer(Piece *p, int id)
{
	int i;

	for(i = 0; i < p->nids; i++)
		if(p->ids[i] == id)
			return 1;
	return 0;
}

void
pieceaddpeer(Piece *p, int id)
{
	if(piecehaspeer(p, id))
		return;
	p->ids = erealloc(p->ids, sizeof p->ids[0] * (p->nids+1));
	p->ids[p->nids] = id;
	p->nids++;
}

void
pieceremovepeer(Piece *p, int id)
{
	int i;

	for(i = 0; i < p->nids; i++){
		if(p->ids[i] == id){
			p->ids[i] = p->ids[p->nids-1];
			p->ids[p->nids-1] = -1;
			p->nids--;
			break;
		}
	}
}

void
pieceaddpeerhaves(Torrent *t, Peer *p)
{
	int i;

	DEBUG(2, "pieceaddpeerhaves: p->pieces=%B\n", p->pieces);

	for(i = 0; i < t->npieces; i++)
		if(bitget(p->pieces, i))
			pieceaddpeer(&t->pieces[i], p->n);
}

void
pieceremoveid(Piece *pc, int peern)
{
	int i;
	for(i = 0; i < pc->nids; i++)
		if(pc->ids[i] == peern){
			pc->ids[i] = pc->ids[pc->nids-1];
			pc->nids--;
			return;
		}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.