SimpleTorrent
函数 | 变量
peer.c 文件参考

单个 peer 相关操作 API 实现 更多...

#include "peer.h"
#include "util.h"
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <arpa/inet.h>
peer.c 的引用(Include)关系图:

函数

struct PeerMsgpeer_get_packet (struct Peer *peer)
 获取 BT 报文 更多...
 
struct Peerpeer_new (int fd, size_t nr_pieces)
 peer 构造 更多...
 
void peer_free (struct Peer **p)
 释放 peer 更多...
 
static unsigned in_byte_mask_of_ (size_t off)
 获取位域的字节内掩码 更多...
 
static int byte_index_of_ (unsigned off)
 获取位域的字节 更多...
 
static int in_byte_offset_of_ (unsigned bit_offset)
 获取位域的字节内偏移 更多...
 
static void set_bit (unsigned char *bytes, unsigned bit_offset)
 设置 bit 更多...
 
static unsigned char get_bit (unsigned char *bytes, unsigned bit_offset)
 获取 bit 更多...
 
static void print_bit_in_byte (unsigned char byte, size_t bit_len)
 打印一个字节的 bit 更多...
 
void print_bit (unsigned char *bytes, size_t bit_len)
 向标准输出打印 bitfield 更多...
 
void peer_set_bit (struct Peer *peer, unsigned bit_offset)
 设置 peer 的位域 更多...
 
unsigned char peer_get_bit (struct Peer *peer, unsigned bit_offset)
 获取 peer 的位域 更多...
 
void peer_send_msg (struct Peer *peer, struct PeerMsg *msg)
 向 peer 发送 BT 消息 更多...
 

变量

const char * bt_types []
 对应 BT 报文类型的字符串 更多...
 

详细描述

单个 peer 相关操作 API 实现

函数说明

static int byte_index_of_ ( unsigned  off)
inlinestatic

获取位域的字节

参数
off位偏移
返回
字节下标

这是这个函数的调用关系图:

static unsigned char get_bit ( unsigned char *  bytes,
unsigned  bit_offset 
)
static

获取 bit

参数
bytesbit field
bit_offsetbit 偏移
返回
bit 值

这是这个函数的调用关系图:

static unsigned in_byte_mask_of_ ( size_t  off)
inlinestatic

获取位域的字节内掩码

参数
off位偏移
返回
字节掩码, 独热

这是这个函数的调用关系图:

static int in_byte_offset_of_ ( unsigned  bit_offset)
inlinestatic

获取位域的字节内偏移

BitTorrent 的规定与常规思维不同, MSB 是 [0], LSB 是 [7], 所以要用 7 减去与出来的值.

参数
bit_offset位域偏移
返回
字节内偏移

这是这个函数的调用关系图:

void peer_free ( struct Peer **  peer)

释放 peer

参数
peer要释放的 peer, 会改写成 NULL
unsigned char peer_get_bit ( struct Peer peer,
unsigned  bit_offset 
)

获取 peer 的位域

参数
peer要获取的 peer
bit_offset位偏移量

这是这个函数的调用关系图:

struct PeerMsg* peer_get_packet ( struct Peer peer)

获取 BT 报文

len 不应为 0, keep-alive 由上层检查

这是这个函数的调用关系图:

struct Peer* peer_new ( int  fd,
size_t  nr_pieces 
)

peer 构造

bitfield 的大小是对 nr_pieces / 8 的上取整.

这是这个函数的调用关系图:

void peer_send_msg ( struct Peer peer,
struct PeerMsg msg 
)

向 peer 发送 BT 消息

提前构造好 msg 并将其发送给指定的 peer, 以 msg 的 len 成员为准发送缓冲区,调用者保证其正确性。

参数
peer要发送消息的 peer
msg发送的消息
void peer_set_bit ( struct Peer peer,
unsigned  bit_offset 
)

设置 peer 的位域

参数
peer要设置的 peer
bit_offset位偏移量

这是这个函数的调用关系图:

void print_bit ( unsigned char *  bytes,
size_t  bit_len 
)

向标准输出打印 bitfield

参数
bytesbitfield 缓冲区
bit_lenbit 数, 一般对应分片数量

这是这个函数的调用关系图:

static void print_bit_in_byte ( unsigned char  byte,
size_t  bit_len 
)
static

打印一个字节的 bit

参数
bytebit field
bit_len限长(小于 8)

这是这个函数的调用关系图:

static void set_bit ( unsigned char *  bytes,
unsigned  bit_offset 
)
static

设置 bit

参数
bytesbit field
bit_offsetbit 偏移

这是这个函数的调用关系图:

变量说明

const char* bt_types[]
初始值:
=
{
"CHOKE",
"UNCHOKE",
"INTERESTED",
"NOT_INTERESTED",
"HAVE",
"BITFIELD",
"REQUEST",
"PIECE",
"CANCEL",
}

对应 BT 报文类型的字符串