浏览代码

Rename p->net to p->in and net_state to input_state. Move code to where it

belongs.
master
Richard Nyberg 19 年前
父节点
当前提交
2e63eaf8ea
共有 4 个文件被更改,包括 60 次插入60 次删除
  1. +36
    -43
      btpd/net.c
  2. +0
    -12
      btpd/net.h
  3. +10
    -3
      btpd/peer.c
  4. +14
    -2
      btpd/peer.h

+ 36
- 43
btpd/net.c 查看文件

@@ -166,20 +166,13 @@ net_write(struct peer *p, unsigned long wmax)
return nwritten; return nwritten;
} }


void
net_set_state(struct peer *p, enum net_state state, size_t size)
{
p->net.state = state;
p->net.st_bytes = size;
}

static int static int
net_dispatch_msg(struct peer *p, const char *buf) net_dispatch_msg(struct peer *p, const char *buf)
{ {
uint32_t index, begin, length; uint32_t index, begin, length;
int res = 0; int res = 0;


switch (p->net.msg_num) { switch (p->in.msg_num) {
case MSG_CHOKE: case MSG_CHOKE:
peer_on_choke(p); peer_on_choke(p);
break; break;
@@ -225,8 +218,8 @@ net_dispatch_msg(struct peer *p, const char *buf)
peer_on_cancel(p, index, begin, length); peer_on_cancel(p, index, begin, length);
break; break;
case MSG_PIECE: case MSG_PIECE:
length = p->net.msg_len - 9; length = p->in.msg_len - 9;
peer_on_piece(p, p->net.pc_index, p->net.pc_begin, length, buf); peer_on_piece(p, p->in.pc_index, p->in.pc_begin, length, buf);
break; break;
default: default:
abort(); abort();
@@ -237,8 +230,8 @@ net_dispatch_msg(struct peer *p, const char *buf)
static int static int
net_mh_ok(struct peer *p) net_mh_ok(struct peer *p)
{ {
uint32_t mlen = p->net.msg_len; uint32_t mlen = p->in.msg_len;
switch (p->net.msg_num) { switch (p->in.msg_num) {
case MSG_CHOKE: case MSG_CHOKE:
case MSG_UNCHOKE: case MSG_UNCHOKE:
case MSG_INTEREST: case MSG_INTEREST:
@@ -261,7 +254,7 @@ net_mh_ok(struct peer *p)
static void static void
net_progress(struct peer *p, size_t length) net_progress(struct peer *p, size_t length)
{ {
if (p->net.state == BTP_MSGBODY && p->net.msg_num == MSG_PIECE) { if (p->in.state == BTP_MSGBODY && p->in.msg_num == MSG_PIECE) {
p->tp->downloaded += length; p->tp->downloaded += length;
p->count_dwn += length; p->count_dwn += length;
} }
@@ -270,11 +263,11 @@ net_progress(struct peer *p, size_t length)
static int static int
net_state(struct peer *p, const char *buf) net_state(struct peer *p, const char *buf)
{ {
switch (p->net.state) { switch (p->in.state) {
case SHAKE_PSTR: case SHAKE_PSTR:
if (bcmp(buf, "\x13""BitTorrent protocol", 20) != 0) if (bcmp(buf, "\x13""BitTorrent protocol", 20) != 0)
goto bad; goto bad;
net_set_state(p, SHAKE_INFO, 20); peer_set_in_state(p, SHAKE_INFO, 20);
break; break;
case SHAKE_INFO: case SHAKE_INFO:
if (p->flags & PF_INCOMING) { if (p->flags & PF_INCOMING) {
@@ -288,7 +281,7 @@ net_state(struct peer *p, const char *buf)
peer_send(p, nb_create_shake(p->tp)); peer_send(p, nb_create_shake(p->tp));
} else if (bcmp(buf, p->tp->meta.info_hash, 20) != 0) } else if (bcmp(buf, p->tp->meta.info_hash, 20) != 0)
goto bad; goto bad;
net_set_state(p, SHAKE_ID, 20); peer_set_in_state(p, SHAKE_ID, 20);
break; break;
case SHAKE_ID: case SHAKE_ID:
if ((torrent_has_peer(p->tp, buf) if ((torrent_has_peer(p->tp, buf)
@@ -296,37 +289,37 @@ net_state(struct peer *p, const char *buf)
goto bad; goto bad;
bcopy(buf, p->id, 20); bcopy(buf, p->id, 20);
peer_on_shake(p); peer_on_shake(p);
net_set_state(p, BTP_MSGSIZE, 4); peer_set_in_state(p, BTP_MSGSIZE, 4);
break; break;
case BTP_MSGSIZE: case BTP_MSGSIZE:
p->net.msg_len = net_read32(buf); p->in.msg_len = net_read32(buf);
if (p->net.msg_len == 0) if (p->in.msg_len == 0)
peer_on_keepalive(p); peer_on_keepalive(p);
else else
net_set_state(p, BTP_MSGHEAD, 1); peer_set_in_state(p, BTP_MSGHEAD, 1);
break; break;
case BTP_MSGHEAD: case BTP_MSGHEAD:
p->net.msg_num = buf[0]; p->in.msg_num = buf[0];
if (!net_mh_ok(p)) if (!net_mh_ok(p))
goto bad; goto bad;
else if (p->net.msg_len == 1) { else if (p->in.msg_len == 1) {
if (net_dispatch_msg(p, buf) != 0) if (net_dispatch_msg(p, buf) != 0)
goto bad; goto bad;
net_set_state(p, BTP_MSGSIZE, 4); peer_set_in_state(p, BTP_MSGSIZE, 4);
} else if (p->net.msg_num == MSG_PIECE) } else if (p->in.msg_num == MSG_PIECE)
net_set_state(p, BTP_PIECEMETA, 8); peer_set_in_state(p, BTP_PIECEMETA, 8);
else else
net_set_state(p, BTP_MSGBODY, p->net.msg_len - 1); peer_set_in_state(p, BTP_MSGBODY, p->in.msg_len - 1);
break; break;
case BTP_PIECEMETA: case BTP_PIECEMETA:
p->net.pc_index = net_read32(buf); p->in.pc_index = net_read32(buf);
p->net.pc_begin = net_read32(buf + 4); p->in.pc_begin = net_read32(buf + 4);
net_set_state(p, BTP_MSGBODY, p->net.msg_len - 9); peer_set_in_state(p, BTP_MSGBODY, p->in.msg_len - 9);
break; break;
case BTP_MSGBODY: case BTP_MSGBODY:
if (net_dispatch_msg(p, buf) != 0) if (net_dispatch_msg(p, buf) != 0)
goto bad; goto bad;
net_set_state(p, BTP_MSGSIZE, 4); peer_set_in_state(p, BTP_MSGSIZE, 4);
break; break;
default: default:
abort(); abort();
@@ -335,7 +328,7 @@ net_state(struct peer *p, const char *buf)
return 0; return 0;
bad: bad:
btpd_log(BTPD_L_CONN, "bad data from %p (%u, %u, %u).\n", btpd_log(BTPD_L_CONN, "bad data from %p (%u, %u, %u).\n",
p, p->net.state, p->net.msg_len, p->net.msg_num); p, p->in.state, p->in.msg_len, p->in.msg_num);
peer_kill(p); peer_kill(p);
return -1; return -1;
} }
@@ -345,11 +338,11 @@ bad:
static unsigned long static unsigned long
net_read(struct peer *p, unsigned long rmax) net_read(struct peer *p, unsigned long rmax)
{ {
size_t rest = p->net.buf != NULL ? p->net.st_bytes - p->net.off : 0; size_t rest = p->in.buf != NULL ? p->in.st_bytes - p->in.off : 0;
char buf[GRBUFLEN]; char buf[GRBUFLEN];
struct iovec iov[2] = { struct iovec iov[2] = {
{ {
p->net.buf + p->net.off, p->in.buf + p->in.off,
rest rest
}, { }, {
buf, buf,
@@ -378,21 +371,21 @@ net_read(struct peer *p, unsigned long rmax)


if (rest > 0) { if (rest > 0) {
if (nread < rest) { if (nread < rest) {
p->net.off += nread; p->in.off += nread;
net_progress(p, nread); net_progress(p, nread);
goto out; goto out;
} }
net_progress(p, rest); net_progress(p, rest);
if (net_state(p, p->net.buf) != 0) if (net_state(p, p->in.buf) != 0)
return nread; return nread;
free(p->net.buf); free(p->in.buf);
p->net.buf = NULL; p->in.buf = NULL;
p->net.off = 0; p->in.off = 0;
} }


iov[1].iov_len = nread - rest; iov[1].iov_len = nread - rest;
while (p->net.st_bytes <= iov[1].iov_len) { while (p->in.st_bytes <= iov[1].iov_len) {
size_t consumed = p->net.st_bytes; size_t consumed = p->in.st_bytes;
net_progress(p, consumed); net_progress(p, consumed);
if (net_state(p, iov[1].iov_base) != 0) if (net_state(p, iov[1].iov_base) != 0)
return nread; return nread;
@@ -402,9 +395,9 @@ net_read(struct peer *p, unsigned long rmax)


if (iov[1].iov_len > 0) { if (iov[1].iov_len > 0) {
net_progress(p, iov[1].iov_len); net_progress(p, iov[1].iov_len);
p->net.off = iov[1].iov_len; p->in.off = iov[1].iov_len;
p->net.buf = btpd_malloc(p->net.st_bytes); p->in.buf = btpd_malloc(p->in.st_bytes);
bcopy(iov[1].iov_base, p->net.buf, iov[1].iov_len); bcopy(iov[1].iov_base, p->in.buf, iov[1].iov_len);
} }


out: out:


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

@@ -18,18 +18,6 @@ extern struct peer_tq net_bw_readq;
extern struct peer_tq net_bw_writeq; extern struct peer_tq net_bw_writeq;
extern unsigned net_npeers; extern unsigned net_npeers;


enum net_state {
SHAKE_PSTR,
SHAKE_INFO,
SHAKE_ID,
BTP_MSGSIZE,
BTP_MSGHEAD,
BTP_PIECEMETA,
BTP_MSGBODY
};

void net_set_state(struct peer *p, enum net_state state, size_t size);

void net_init(void); void net_init(void);


void net_add_torrent(struct torrent *tp); void net_add_torrent(struct torrent *tp);


+ 10
- 3
btpd/peer.c 查看文件

@@ -42,14 +42,21 @@ peer_kill(struct peer *p)
nl = next; nl = next;
} }


if (p->net.buf != NULL) if (p->in.buf != NULL)
free(p->net.buf); free(p->in.buf);
if (p->piece_field != NULL) if (p->piece_field != NULL)
free(p->piece_field); free(p->piece_field);
free(p); free(p);
net_npeers--; net_npeers--;
} }


void
peer_set_in_state(struct peer *p, enum input_state state, size_t size)
{
p->in.state = state;
p->in.st_bytes = size;
}

void void
peer_send(struct peer *p, struct net_buf *nb) peer_send(struct peer *p, struct net_buf *nb)
{ {
@@ -258,7 +265,7 @@ peer_create_common(int sd)
BTPDQ_INIT(&p->my_reqs); BTPDQ_INIT(&p->my_reqs);
BTPDQ_INIT(&p->outq); BTPDQ_INIT(&p->outq);


net_set_state(p, SHAKE_PSTR, 28); peer_set_in_state(p, SHAKE_PSTR, 28);


event_set(&p->out_ev, p->sd, EV_WRITE, net_write_cb, p); event_set(&p->out_ev, p->sd, EV_WRITE, net_write_cb, p);
event_set(&p->in_ev, p->sd, EV_READ, net_read_cb, p); event_set(&p->in_ev, p->sd, EV_READ, net_read_cb, p);


+ 14
- 2
btpd/peer.h 查看文件

@@ -24,6 +24,16 @@ struct block_request {


BTPDQ_HEAD(block_request_tq, block_request); BTPDQ_HEAD(block_request_tq, block_request);


enum input_state {
SHAKE_PSTR,
SHAKE_INFO,
SHAKE_ID,
BTP_MSGSIZE,
BTP_MSGHEAD,
BTP_PIECEMETA,
BTP_MSGBODY
};

struct peer { struct peer {
int sd; int sd;
uint16_t flags; uint16_t flags;
@@ -54,11 +64,11 @@ struct peer {
uint8_t msg_num; uint8_t msg_num;
uint32_t pc_index; uint32_t pc_index;
uint32_t pc_begin; uint32_t pc_begin;
enum net_state state; enum input_state state;
size_t st_bytes; size_t st_bytes;
char *buf; char *buf;
size_t off; size_t off;
} net; } in;


BTPDQ_ENTRY(peer) p_entry; BTPDQ_ENTRY(peer) p_entry;
BTPDQ_ENTRY(peer) ul_entry; BTPDQ_ENTRY(peer) ul_entry;
@@ -68,6 +78,8 @@ struct peer {


BTPDQ_HEAD(peer_tq, peer); BTPDQ_HEAD(peer_tq, peer);


void peer_set_in_state(struct peer *p, enum input_state state, size_t size);

void peer_send(struct peer *p, struct net_buf *nb); void peer_send(struct peer *p, struct net_buf *nb);
int peer_unsend(struct peer *p, struct nb_link *nl); int peer_unsend(struct peer *p, struct nb_link *nl);
void peer_sent(struct peer *p, struct net_buf *nb); void peer_sent(struct peer *p, struct net_buf *nb);


||||||
x
 
000:0
正在加载...
取消
保存