|
|
@@ -23,6 +23,33 @@ |
|
|
|
static unsigned long |
|
|
|
net_write(struct peer *p, unsigned long wmax); |
|
|
|
|
|
|
|
void |
|
|
|
net_bw_read_cb(int sd, short type, void *arg) |
|
|
|
{ |
|
|
|
struct peer *p; |
|
|
|
struct bwlim *bw = arg; |
|
|
|
|
|
|
|
btpd.ibw_left += bw->count; |
|
|
|
|
|
|
|
unsigned long count = 0; |
|
|
|
|
|
|
|
while ((p = BTPDQ_FIRST(&btpd.readq)) != NULL && btpd.ibw_left - count > 0) { |
|
|
|
BTPDQ_REMOVE(&btpd.readq, p, rq_entry); |
|
|
|
p->flags &= ~PF_ON_READQ; |
|
|
|
count += p->reader->read(p, btpd.ibw_left - count); |
|
|
|
} |
|
|
|
btpd.ibw_left -= count; |
|
|
|
|
|
|
|
BTPDQ_REMOVE(&btpd.bwq, bw, entry); |
|
|
|
if (count == 0) |
|
|
|
free(bw); |
|
|
|
else { |
|
|
|
bw->count = count; |
|
|
|
event_add(&bw->timer, (& (struct timeval) { 1, 0 })); |
|
|
|
BTPDQ_INSERT_TAIL(&btpd.bwq, bw, entry); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
|
net_read_cb(int sd, short type, void *arg) |
|
|
|
{ |
|
|
@@ -30,13 +57,48 @@ net_read_cb(int sd, short type, void *arg) |
|
|
|
if (btpd.ibwlim == 0) { |
|
|
|
p->reader->read(p, 0); |
|
|
|
} else if (btpd.ibw_left > 0) { |
|
|
|
btpd.ibw_left -= p->reader->read(p, btpd.ibw_left); |
|
|
|
unsigned long nread = p->reader->read(p, btpd.ibw_left); |
|
|
|
if (nread > 0) { |
|
|
|
struct bwlim *bw = btpd_calloc(1, sizeof(*bw)); |
|
|
|
evtimer_set(&bw->timer, net_bw_read_cb, bw); |
|
|
|
evtimer_add(&bw->timer, (& (struct timeval) { 1, 0 })); |
|
|
|
bw->count = nread; |
|
|
|
btpd.ibw_left -= nread; |
|
|
|
BTPDQ_INSERT_TAIL(&btpd.bwq, bw, entry); |
|
|
|
} |
|
|
|
} else { |
|
|
|
p->flags |= PF_ON_READQ; |
|
|
|
BTPDQ_INSERT_TAIL(&btpd.readq, p, rq_entry); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
|
net_bw_write_cb(int sd, short type, void *arg) |
|
|
|
{ |
|
|
|
struct peer *p; |
|
|
|
struct bwlim *bw = arg; |
|
|
|
|
|
|
|
btpd.obw_left += bw->count; |
|
|
|
|
|
|
|
unsigned long count = 0; |
|
|
|
|
|
|
|
while ((p = BTPDQ_FIRST(&btpd.writeq)) != NULL && btpd.obw_left - count > 0) { |
|
|
|
BTPDQ_REMOVE(&btpd.writeq, p, wq_entry); |
|
|
|
p->flags &= ~PF_ON_WRITEQ; |
|
|
|
count += net_write(p, btpd.obw_left - count); |
|
|
|
} |
|
|
|
btpd.obw_left -= count; |
|
|
|
|
|
|
|
BTPDQ_REMOVE(&btpd.bwq, bw, entry); |
|
|
|
if (count == 0) |
|
|
|
free(bw); |
|
|
|
else { |
|
|
|
bw->count = count; |
|
|
|
event_add(&bw->timer, (& (struct timeval) { 1, 0 })); |
|
|
|
BTPDQ_INSERT_TAIL(&btpd.bwq, bw, entry); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
|
net_write_cb(int sd, short type, void *arg) |
|
|
|
{ |
|
|
@@ -44,7 +106,15 @@ net_write_cb(int sd, short type, void *arg) |
|
|
|
if (btpd.obwlim == 0) { |
|
|
|
net_write(p, 0); |
|
|
|
} else if (btpd.obw_left > 0) { |
|
|
|
btpd.obw_left -= net_write(p, btpd.obw_left); |
|
|
|
unsigned long nw = net_write(p, btpd.obw_left); |
|
|
|
if (nw > 0) { |
|
|
|
struct bwlim *bw = btpd_calloc(1, sizeof(*bw)); |
|
|
|
evtimer_set(&bw->timer, net_bw_write_cb, bw); |
|
|
|
evtimer_add(&bw->timer, (& (struct timeval) { 1, 0 })); |
|
|
|
bw->count = nw; |
|
|
|
btpd.obw_left -= nw; |
|
|
|
BTPDQ_INSERT_TAIL(&btpd.bwq, bw, entry); |
|
|
|
} |
|
|
|
} else { |
|
|
|
p->flags |= PF_ON_WRITEQ; |
|
|
|
BTPDQ_INSERT_TAIL(&btpd.writeq, p, wq_entry); |
|
|
@@ -927,6 +997,7 @@ net_by_second(void) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#if 0 |
|
|
|
btpd.obw_left = btpd.obwlim; |
|
|
|
btpd.ibw_left = btpd.ibwlim; |
|
|
|
|
|
|
@@ -957,4 +1028,5 @@ net_by_second(void) |
|
|
|
net_write(p, 0); |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
} |