mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 06:13:42 +00:00
Refactor
This commit is contained in:
1 parent
075852b847
commit
adb7d89018
19 files changed
+415
-258
No files matched your search
@@ -1,31 +1,52 @@
|
||||
package peer
|
||||
package message
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
type MessageID int
|
||||
// message ID
|
||||
type ID byte
|
||||
|
||||
const (
|
||||
Keepalive MessageID = -1
|
||||
Choke MessageID = 0
|
||||
Unchoke MessageID = 1
|
||||
Interested MessageID = 2
|
||||
NotInterested MessageID = 3
|
||||
Have MessageID = 4
|
||||
Bitfield MessageID = 5
|
||||
Request MessageID = 6
|
||||
Piece MessageID = 7
|
||||
Cancel MessageID = 8
|
||||
IdChoke ID = 0
|
||||
IdUnchoke ID = 1
|
||||
IdInterested ID = 2
|
||||
IdNotinterested ID = 3
|
||||
IdHave ID = 4
|
||||
IdBitfield ID = 5
|
||||
IdRequest ID = 6
|
||||
IdPiece ID = 7
|
||||
IdCancel ID = 8
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
/*type Message struct {
|
||||
Length uint32
|
||||
ID MessageID
|
||||
Payload []byte
|
||||
}*/
|
||||
type Message struct {
|
||||
Length uint32
|
||||
ID ID
|
||||
}
|
||||
|
||||
func NewMessage(id MessageID, payload []byte) *Message {
|
||||
func (msg Message) Encode() []byte {
|
||||
buf := make([]byte, 5)
|
||||
binary.BigEndian.PutUint32(buf, msg.Length)
|
||||
buf[4] = byte(msg.ID)
|
||||
return buf
|
||||
}
|
||||
|
||||
func (msg Message) Decode(buf []byte) {
|
||||
msg.Length = binary.BigEndian.Uint32(buf)
|
||||
msg.ID = ID(buf[4])
|
||||
}
|
||||
|
||||
type Serialize interface {
|
||||
Encode() []byte
|
||||
Decode(buf []byte) Serialize
|
||||
}
|
||||
|
||||
/*func NewMessage(id MessageID, payload []byte) *Message {
|
||||
message := &Message{ID: id, Payload: payload}
|
||||
if id == Keepalive {
|
||||
message.Length = 0
|
||||
@@ -57,7 +78,7 @@ func (msg *Message) Decode(buf []byte) {
|
||||
msg.Payload = buf[5:]
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
// Message protocol
|
||||
// Length prefix| message ID | Payload
|
||||
@@ -73,39 +94,3 @@ func SplitMessage(data []byte, atEOF bool) (advance int, token []byte, err error
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type MsgBitfield []byte
|
||||
|
||||
// 某个分片是否下载完成
|
||||
func (mb MsgBitfield) IsComplete(i int) bool {
|
||||
bts := []byte(mb)
|
||||
index := i / 8
|
||||
if index >= len(bts) {
|
||||
return false
|
||||
}
|
||||
return bts[index]&(1<<uint(7-i%8)) > 0
|
||||
}
|
||||
|
||||
// 给定一组分片下载状态,计算出当前peer能提供下载的分片下标
|
||||
func (mb MsgBitfield) Have(pieces []bool) []int {
|
||||
arr := make([]int, 0)
|
||||
bts := []byte(mb)
|
||||
length := len(pieces) / 8
|
||||
if len(pieces)%8 != 0 {
|
||||
length++
|
||||
}
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
for j := 0; j < 8; j++ {
|
||||
index := i*8 + j
|
||||
// 如果此分片在本地还未下载,检查peer是否能提供该分片下载
|
||||
if index < len(pieces) &&
|
||||
!pieces[index] &&
|
||||
i < len(bts) &&
|
||||
uint(bts[i])&(1<<uint(7-j)) > 0 {
|
||||
arr = append(arr, index)
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package message
|
||||
|
||||
// bitfield: <len=0001+X><id=5><bitfield>
|
||||
type Bitfield struct {
|
||||
Message
|
||||
payload []byte
|
||||
}
|
||||
|
||||
func NewBitfield(payload []byte) *Bitfield {
|
||||
return &Bitfield{
|
||||
Message: Message{
|
||||
Length: uint32(len(payload) + 1),
|
||||
ID: IdBitfield,
|
||||
},
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bitfield) Encode() []byte {
|
||||
return append(b.Message.Encode(), b.payload...)
|
||||
}
|
||||
|
||||
func (b *Bitfield) Decode(buf []byte) Serialize {
|
||||
b.Message.Decode(buf)
|
||||
copy(b.payload, buf[5:])
|
||||
return b
|
||||
}
|
||||
|
||||
// 某个分片是否下载完成
|
||||
func (b *Bitfield) IsComplete(i int) bool {
|
||||
index := i / 8
|
||||
if index >= len(b.payload) {
|
||||
return false
|
||||
}
|
||||
return b.payload[index]&(1<<(7-i%8)) > 0
|
||||
}
|
||||
|
||||
// 给定一组分片下载状态,计算出当前peer能提供下载的分片下标
|
||||
func (b *Bitfield) Have(pieces []bool) []int {
|
||||
arr := make([]int, 0)
|
||||
length := len(pieces) / 8
|
||||
if len(pieces)%8 != 0 {
|
||||
length++
|
||||
}
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
for j := 0; j < 8; j++ {
|
||||
index := i*8 + j
|
||||
// 如果此分片在本地还未下载,检查peer是否能提供该分片下载
|
||||
if index < len(pieces) &&
|
||||
!pieces[index] &&
|
||||
i < len(b.payload) &&
|
||||
b.payload[i]&(1<<(7-j)) > 0 {
|
||||
arr = append(arr, index)
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package message
|
||||
|
||||
// choke: <len=0001><id=0>
|
||||
type Choke struct {
|
||||
Message
|
||||
}
|
||||
|
||||
func NewChoke() *Choke {
|
||||
return &Choke{
|
||||
Message{
|
||||
Length: 1,
|
||||
ID: IdChoke,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Choke) Encode() []byte {
|
||||
return c.Message.Encode()
|
||||
}
|
||||
|
||||
func (c *Choke) Decode(buf []byte) Serialize {
|
||||
c.Message.Decode(buf)
|
||||
return c
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package message
|
||||
|
||||
// choke: <len=0001><id=0>
|
||||
type Interested struct {
|
||||
Message
|
||||
}
|
||||
|
||||
func NewInterested() *Interested {
|
||||
return &Interested{
|
||||
Message{
|
||||
Length: 1,
|
||||
ID: IdInterested,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Interested) Encode() []byte {
|
||||
return c.Message.Encode()
|
||||
}
|
||||
|
||||
func (c *Interested) Decode(buf []byte) Serialize {
|
||||
c.Message.Decode(buf)
|
||||
return c
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package message
|
||||
|
||||
// keep-alive: <len=0000>
|
||||
type Keepalive struct{}
|
||||
|
||||
func (k *Keepalive) Encode() []byte {
|
||||
return []byte{0, 0, 0, 0}
|
||||
}
|
||||
|
||||
func (k *Keepalive) Decode(buf []byte) Serialize {
|
||||
return &Keepalive{}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package message
|
||||
|
||||
// choke: <len=0001><id=0>
|
||||
type NotInterested struct {
|
||||
Message
|
||||
}
|
||||
|
||||
func NewNotInterested() *NotInterested {
|
||||
return &NotInterested{
|
||||
Message{
|
||||
Length: 1,
|
||||
ID: IdNotinterested,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (ni *NotInterested) Encode() []byte {
|
||||
return ni.Message.Encode()
|
||||
}
|
||||
|
||||
func (ni *NotInterested) Decode(buf []byte) Serialize {
|
||||
ni.Message.Decode(buf)
|
||||
return ni
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package message
|
||||
|
||||
import "io"
|
||||
|
||||
// piece: <len=0009+X><id=7><index><begin><block>
|
||||
type Piece struct {
|
||||
Message
|
||||
Index uint32
|
||||
Begin uint32
|
||||
Block io.Reader
|
||||
}
|
||||
|
||||
func newPiece(index uint32, begin uint32, reader io.Reader, length uint32) *Piece {
|
||||
return &Piece{
|
||||
Message: Message{
|
||||
Length: 9 + length,
|
||||
ID: IdPiece,
|
||||
},
|
||||
Index: index,
|
||||
Begin: begin,
|
||||
Block: io.LimitReader(reader, int64(length)),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// request: <len=0013><id=6><index><begin><length>
|
||||
type Request struct {
|
||||
Message
|
||||
Index uint32
|
||||
Begin uint32
|
||||
Length uint32
|
||||
}
|
||||
|
||||
func NewRequest(index uint32, begin uint32, length uint32) *Request {
|
||||
return &Request{
|
||||
Message: Message{
|
||||
Length: 13,
|
||||
ID: IdRequest,
|
||||
},
|
||||
Index: index,
|
||||
Begin: begin,
|
||||
Length: length,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Request) Encode() []byte {
|
||||
buf := make([]byte, 12)
|
||||
binary.BigEndian.PutUint32(buf[:4], r.Index)
|
||||
binary.BigEndian.PutUint32(buf[4:8], r.Begin)
|
||||
binary.BigEndian.PutUint32(buf[8:], r.Length)
|
||||
return append(r.Message.Encode(), buf...)
|
||||
}
|
||||
|
||||
func (r *Request) Decode(buf []byte) Serialize {
|
||||
r.Message.Decode(buf)
|
||||
r.Length = binary.BigEndian.Uint32(buf[5:9])
|
||||
r.Begin = binary.BigEndian.Uint32(buf[9:13])
|
||||
r.Index = binary.BigEndian.Uint32(buf[13:17])
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package message
|
||||
|
||||
// unchoke: <len=0001><id=1>
|
||||
type Unchoke struct {
|
||||
Message
|
||||
}
|
||||
|
||||
func NewUnchoke() *Unchoke {
|
||||
return &Unchoke{
|
||||
Message{
|
||||
Length: 1,
|
||||
ID: IdUnchoke,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Unchoke) Encode() []byte {
|
||||
return u.Message.Encode()
|
||||
}
|
||||
|
||||
func (u *Unchoke) Decode(buf []byte) Serialize {
|
||||
u.Message.Decode(buf)
|
||||
return u
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
package peer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMsgBitfield_IsComplete(t *testing.T) {
|
||||
type args struct {
|
||||
i int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
mb MsgBitfield
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"case-0",
|
||||
MsgBitfield([]byte{convertByte("10000000")}),
|
||||
args{
|
||||
0,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"case-1",
|
||||
MsgBitfield([]byte{convertByte("01000000")}),
|
||||
args{
|
||||
1,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"case-2",
|
||||
MsgBitfield([]byte{convertByte("00000000")}),
|
||||
args{
|
||||
2,
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"case-10",
|
||||
MsgBitfield([]byte{convertByte("00000000"), convertByte("00100000")}),
|
||||
args{
|
||||
10,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"case-11",
|
||||
MsgBitfield([]byte{convertByte("00000000"), convertByte("00100000")}),
|
||||
args{
|
||||
11,
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"case-20",
|
||||
MsgBitfield([]byte{convertByte("00000000"), convertByte("00100000")}),
|
||||
args{
|
||||
20,
|
||||
},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.mb.IsComplete(tt.args.i); got != tt.want {
|
||||
t.Errorf("MsgBitfield.get() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMsgBitfield_Have(t *testing.T) {
|
||||
type args struct {
|
||||
pieces []bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
mb MsgBitfield
|
||||
args args
|
||||
want []int
|
||||
}{
|
||||
{
|
||||
"case-1",
|
||||
MsgBitfield([]byte{convertByte("11111111"), convertByte("00000000")}),
|
||||
args{
|
||||
[]bool{false, false, false, false, false, false, false, false},
|
||||
},
|
||||
[]int{0, 1, 2, 3, 4, 5, 6, 7},
|
||||
},
|
||||
{
|
||||
"case-2",
|
||||
MsgBitfield([]byte{convertByte("01010101"), convertByte("01000000")}),
|
||||
args{
|
||||
[]bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
||||
},
|
||||
[]int{1, 3, 5, 7, 9},
|
||||
},
|
||||
{
|
||||
"case-3",
|
||||
MsgBitfield([]byte{convertByte("11010101"), convertByte("01000000")}),
|
||||
args{
|
||||
[]bool{false, true, false},
|
||||
},
|
||||
[]int{0},
|
||||
},
|
||||
{
|
||||
"case-4",
|
||||
MsgBitfield([]byte{convertByte("11010101")}),
|
||||
args{
|
||||
[]bool{false, true, false, false, true, false, false, true, false},
|
||||
},
|
||||
[]int{0, 3, 5},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.mb.Have(tt.args.pieces); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("MsgBitfield.Have() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMsg(t *testing.T) {
|
||||
fmt.Println(Choke)
|
||||
fmt.Println(Bitfield)
|
||||
}
|
||||
|
||||
func convertByte(bit string) byte {
|
||||
i, _ := strconv.ParseUint(bit, 2, 8)
|
||||
return byte(i)
|
||||
}
|
||||
Reference in new issue
Block a user