瀏覽代碼

New algorithm for bandwidth limitation.

master
Richard Nyberg 19 年之前
父節點
當前提交
b84c77f1f7
共有 4 個文件被更改,包括 84 次插入2 次删除
  1. +1
    -0
      btpd/btpd.c
  2. +1
    -0
      btpd/btpd.h
  3. +74
    -2
      btpd/net.c
  4. +8
    -0
      btpd/net.h

+ 1
- 0
btpd/btpd.c 查看文件

@@ -116,6 +116,7 @@ btpd_init(void)
btpd.ntorrents = 0;
BTPDQ_INIT(&btpd.cm_list);

BTPDQ_INIT(&btpd.bwq);
BTPDQ_INIT(&btpd.readq);
BTPDQ_INIT(&btpd.writeq);



+ 1
- 0
btpd/btpd.h 查看文件

@@ -45,6 +45,7 @@ struct btpd {
unsigned ntorrents;
struct torrent_tq cm_list;

struct bwlim_tq bwq;
struct peer_tq readq;
struct peer_tq writeq;



+ 74
- 2
btpd/net.c 查看文件

@@ -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
}

+ 8
- 0
btpd/net.h 查看文件

@@ -11,6 +11,14 @@
#define MSG_PIECE 7
#define MSG_CANCEL 8

struct bwlim {
unsigned long count;
struct event timer;
BTPDQ_ENTRY(bwlim) entry;
};

BTPDQ_HEAD(bwlim_tq, bwlim);

struct iob_link {
BTPDQ_ENTRY(iob_link) entry;
void (*kill_buf)(struct io_buffer *);


Loading…
取消
儲存