refactor: package

This commit is contained in:
liwei committed 2021-03-25 19:38:23 +08:00
1 parent 359f28bb1f
commit ede6a9834d
55 files changed
+279 -237

No files matched your search

+143
View File
@@ -0,0 +1,143 @@
package message
import (
"encoding/binary"
)
// message ID
type ID byte
const (
IdChoke ID = iota
IdUnchoke
IdInterested
IdNotInterested
IdHave
IdBitfield
IdRequest
IdPiece
IdCancel
)
/*type Message struct {
Length uint32
ID MessageID
Payload []byte
}*/
type base struct {
id ID
}
func (base *base) ID() ID {
return base.id
}
func (base *base) Encode() []byte {
return encode(base, nil)
}
func (base *base) Decode(body []byte) {
}
type Message interface {
ID() ID
Encode() []byte
Decode(body []byte)
}
type TestMsg struct {
*base
}
func (base *TestMsg) Encode() []byte {
return []byte{1, 2, 3}
}
func (base *TestMsg) Decode(body []byte) {
}
func encode(base *base, body []byte) []byte {
buf := make([]byte, 5+len(body))
binary.BigEndian.PutUint32(buf, uint32(len(buf)))
buf[4] = byte(base.id)
copy(buf[4:], body)
return buf
}
/*func Decode(buf []byte) Message {
head := make([]byte, 5)
body := message.Encode()
binary.BigEndian.PutUint32(head, uint32(len(head)+len(body)))
head[4] = byte(message.ID())
return append(head, body...)
}
func (msg *Message) ToBytes() []byte {
buf := make([]byte, 5)
binary.BigEndian.PutUint32(buf, msg.Length)
buf[4] = byte(msg.ID)
if msg.Payload != nil {
return append(buf, msg.Payload.Encode()...)
} else {
return buf
}
}
func (msg *Message) FormBytes(buf []byte) {
msg.Length = binary.BigEndian.Uint32(buf)
msg.ID = ID(buf[4])
if msg.Payload != nil {
msg.Payload.Decode(buf[4:])
}
}*/
/*func NewMessage(id MessageID, Payload []byte) *Message {
message := &Message{ID: id, Payload: Payload}
if id == Keepalive {
message.Length = 0
} else {
message.Length = uint32(len(Payload) + 1)
}
return message
}
func (msg *Message) Encode() []byte {
if msg.ID == Keepalive {
return make([]byte, 4)
} else {
buf := make([]byte, 5+len(msg.Payload))
binary.BigEndian.PutUint32(buf, msg.Length)
buf[4] = byte(msg.ID)
copy(buf[5:], msg.Payload[:])
return buf
}
}
func (msg *Message) Decode(buf []byte) {
msg.Length = binary.BigEndian.Uint32(buf)
if msg.Length == 0 {
msg.ID = -1
} else {
msg.ID = MessageID(buf[4])
if msg.Length > 4 {
msg.Payload = buf[5:]
}
}
}*/
// Message protocol
// Length prefix| message ID | Payload
// 4-byte | 1-byte | remaining bytes
// 100 | 5 | 99-bytes
// https://wiki.theory.org/index.php/BitTorrentSpecification#Messages
func SplitMessage(data []byte, atEOF bool) (advance int, token []byte, err error) {
if !atEOF && len(data) > 4 {
length := int(binary.BigEndian.Uint32(data))
if len(data)-4 >= length {
return length + 4, data[:length+4], nil
}
}
return
}
@@ -0,0 +1,68 @@
package message
import (
"github.com/RoaringBitmap/roaring"
"math"
)
// bitfield: <len=0001+X><id=5><bitfield>
type Bitfield struct {
*base
pieceCount int
bitmap *roaring.Bitmap
}
func NewBitfield() *Bitfield {
return &Bitfield{
base: &base{
id: IdBitfield,
},
}
}
func BuildBitfield(pieceCount int, bitmap *roaring.Bitmap) *Bitfield {
bitfield := NewBitfield()
bitfield.pieceCount = pieceCount
bitfield.bitmap = bitmap
return bitfield
}
func (b *Bitfield) Encode() []byte {
buf := make([]byte, int(math.Ceil(float64(b.pieceCount)/8)))
for i := 0; i < len(buf); i++ {
temp := byte(0)
for j := 0; j < 8; j++ {
// 如果piece存在,位数标识为1
if b.bitmap.ContainsInt(i*8 + j) {
temp = temp | (1 << (7 - j))
}
}
buf[i] = temp
}
return encode(b.base, buf)
}
func (b *Bitfield) Decode(body []byte) {
b.bitmap = roaring.New()
// 初始化bitmap
for i := 0; i < len(body); i++ {
for j := 0; j < 8; j++ {
if body[i]&(1<<(7-j)) > 0 {
b.bitmap.AddInt(i*8 + j)
}
}
}
}
// 某个分片是否下载完成
func (b *Bitfield) IsComplete(i int) bool {
return b.bitmap.ContainsInt(i)
}
// 给定一组分片下载状态,计算出当前peer能提供下载的分片下标,如果已经拥有则不用提供
// has = 0b01111111,had = 0b10010000
// has & ~had = 0b01111111 & 0b01101111 = 0b01101111
func (b *Bitfield) Provide(had *roaring.Bitmap) []uint32 {
xor := roaring.AndNot(b.bitmap, had)
return xor.ToArray()
}
@@ -0,0 +1,111 @@
package message
import (
"github.com/RoaringBitmap/roaring"
"reflect"
"testing"
)
func TestBitfield_IsComplete(t *testing.T) {
type fields struct {
payload *roaring.Bitmap
}
type args struct {
i int
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
"case-1",
fields{
payload: roaring.BitmapOf(1, 2, 3, 4),
},
args{
1,
},
true,
},
{
"case-2",
fields{
payload: roaring.BitmapOf(1, 2, 3, 4),
},
args{
5,
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &Bitfield{
pieceCount: 100,
bitmap: tt.fields.payload,
}
if got := b.IsComplete(tt.args.i); got != tt.want {
t.Errorf("Bitfield.IsComplete() = %v, want %v", got, tt.want)
}
})
}
}
func TestBitfield_Provide(t *testing.T) {
type fields struct {
payload *roaring.Bitmap
}
type args struct {
had *roaring.Bitmap
}
tests := []struct {
name string
fields fields
args args
want []uint32
}{
{
"case-1",
fields{
payload: roaring.BitmapOf(0, 1, 2, 3, 4),
},
args{
roaring.BitmapOf(0),
},
[]uint32{1, 2, 3, 4},
},
{
"case-2",
fields{
payload: roaring.BitmapOf(0, 1, 2, 3, 4, 5, 6, 7, 8),
},
args{
roaring.BitmapOf(0, 1, 3, 5, 6, 8),
},
[]uint32{2, 4, 7},
},
{
"case-3",
fields{
payload: roaring.BitmapOf(),
},
args{
roaring.BitmapOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
},
[]uint32{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &Bitfield{
pieceCount: 100,
bitmap: tt.fields.payload,
}
if got := b.Provide(tt.args.had); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Bitfield.Provide() = %v, want %v", got, tt.want)
}
})
}
}
+14
View File
@@ -0,0 +1,14 @@
package message
// choke: <len=0001><id=0>
type Choke struct {
*base
}
func NewChoke() *Choke {
return &Choke{
base: &base{
id: IdChoke,
},
}
}
@@ -0,0 +1,14 @@
package message
// choke: <len=0001><id=2>
type Interested struct {
*base
}
func NewInterested() *Interested {
return &Interested{
base: &base{
id: IdInterested,
},
}
}
@@ -0,0 +1,16 @@
package message
// keep-alive: <len=0000>
type Keepalive struct {
*base
}
var data = make([]byte, 4)
func (k *Keepalive) Encode() []byte {
return data
}
func (k *Keepalive) Decode(body []byte) {
}
@@ -0,0 +1,14 @@
package message
// choke: <len=0001><id=3>
type NotInterested struct {
*base
}
func NewNotInterested() *NotInterested {
return &NotInterested{
base: &base{
id: IdNotInterested,
},
}
}
+42
View File
@@ -0,0 +1,42 @@
package message
import "encoding/binary"
// piece: <len=0009+X><id=7><index><begin><block>
type Piece struct {
*base
Index uint32
Begin uint32
Block []byte
}
func NewPiece() *Piece {
return &Piece{
base: &base{
id: IdPiece,
},
}
}
func BuildPiece(index uint32, begin uint32, block []byte) *Piece {
piece := NewPiece()
piece.Index = index
piece.Begin = begin
piece.Block = block
return piece
}
func (p *Piece) Encode() []byte {
buf := make([]byte, 8+len(p.Block))
binary.BigEndian.PutUint32(buf[0:4], p.Index)
binary.BigEndian.PutUint32(buf[4:8], p.Begin)
copy(buf[8:], p.Block)
return encode(p.base, buf)
}
func (p *Piece) Decode(buf []byte) {
p.Index = binary.BigEndian.Uint32(buf[0:4])
p.Begin = binary.BigEndian.Uint32(buf[4:8])
p.Block = buf[8:]
}
@@ -0,0 +1,43 @@
package message
import (
"encoding/binary"
)
// request: <len=0013><id=6><index><begin><length>
type Request struct {
*base
Index uint32
Begin uint32
Length uint32
}
func NewRequest() *Request {
return &Request{
base: &base{
id: IdRequest,
},
}
}
func BuildRequest(index uint32, begin uint32, length uint32) *Request {
request := NewRequest()
request.Index = index
request.Begin = begin
request.Length = length
return request
}
func (r *Request) Encode() []byte {
buf := make([]byte, 12)
binary.BigEndian.PutUint32(buf[0:4], r.Index)
binary.BigEndian.PutUint32(buf[4:8], r.Begin)
binary.BigEndian.PutUint32(buf[8:12], r.Length)
return encode(r.base, buf)
}
func (r *Request) Decode(buf []byte) {
r.Length = binary.BigEndian.Uint32(buf[0:4])
r.Begin = binary.BigEndian.Uint32(buf[4:8])
r.Index = binary.BigEndian.Uint32(buf[8:12])
}
@@ -0,0 +1,14 @@
package message
// unchoke: <len=0001><id=1>
type Unchoke struct {
*base
}
func NewUnchoke() *Unchoke {
return &Unchoke{
base: &base{
id: IdUnchoke,
},
}
}