refactor: message entity

This commit is contained in:
liwei committed 2020-04-23 09:56:17 +08:00
1 parent 1d2f38d7ae
commit d6d411f48c
12 files changed
+167 -151

No files matched your search

+1
View File
@@ -16,6 +16,7 @@ func TestParseFromFile(t *testing.T) {
fmt.Printf("%+v\n", metaInfo.AnnounceList) fmt.Printf("%+v\n", metaInfo.AnnounceList)
fmt.Printf("%+v\n", metaInfo.Info.Files) fmt.Printf("%+v\n", metaInfo.Info.Files)
fmt.Printf("%+v\n", metaInfo.Info.PieceLength) fmt.Printf("%+v\n", metaInfo.Info.PieceLength)
fmt.Printf("%+v\n", len(metaInfo.Info.Pieces))
fmt.Printf("%+v\n", hex.EncodeToString(metaInfo.infoHash[:])) fmt.Printf("%+v\n", hex.EncodeToString(metaInfo.infoHash[:]))
} }
+65 -18
View File
@@ -11,7 +11,7 @@ const (
IdChoke ID = iota IdChoke ID = iota
IdUnchoke IdUnchoke
IdInterested IdInterested
IdNotinterested IdNotInterested
IdHave IdHave
IdBitfield IdBitfield
IdRequest IdRequest
@@ -24,34 +24,81 @@ const (
ID MessageID ID MessageID
Payload []byte Payload []byte
}*/ }*/
type Message struct {
Length uint32 type base struct {
ID ID id ID
} }
func (msg *Message) Encode() []byte { func (base *base) ID() ID {
buf := make([]byte, 5) return base.id
binary.BigEndian.PutUint32(buf, msg.Length) }
buf[4] = byte(msg.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 return buf
} }
func (msg *Message) Decode(buf []byte) { /*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.Length = binary.BigEndian.Uint32(buf)
msg.ID = ID(buf[4]) msg.ID = ID(buf[4])
} if msg.Payload != nil {
msg.Payload.Decode(buf[4:])
}
}*/
type Serialize interface { /*func NewMessage(id MessageID, Payload []byte) *Message {
Encode() []byte message := &Message{ID: id, Payload: Payload}
Decode(buf []byte) Serialize
}
/*func NewMessage(id MessageID, payload []byte) *Message {
message := &Message{ID: id, Payload: payload}
if id == Keepalive { if id == Keepalive {
message.Length = 0 message.Length = 0
} else { } else {
message.Length = uint32(len(payload) + 1) message.Length = uint32(len(Payload) + 1)
} }
return message return message
} }
+29 -25
View File
@@ -1,58 +1,62 @@
package message package message
import "github.com/RoaringBitmap/roaring" import (
"github.com/RoaringBitmap/roaring"
"math"
)
// bitfield: <len=0001+X><id=5><bitfield> // bitfield: <len=0001+X><id=5><bitfield>
type Bitfield struct { type Bitfield struct {
*Message *base
payload *roaring.Bitmap pieceCount int
bitmap *roaring.Bitmap
} }
func NewBitfield(payload *roaring.Bitmap) *Bitfield { func NewBitfield() *Bitfield {
return &Bitfield{ return &Bitfield{
Message: &Message{ base: &base{
// TODO 长度处理 id: IdBitfield,
Length: 1,
ID: IdBitfield,
}, },
payload: payload,
} }
} }
func BuildBitfield(pieceCount int, bitmap *roaring.Bitmap) *Bitfield {
bitfield := NewBitfield()
bitfield.pieceCount = pieceCount
bitfield.bitmap = bitmap
return bitfield
}
func (b *Bitfield) Encode() []byte { func (b *Bitfield) Encode() []byte {
buf := b.Message.Encode() buf := make([]byte, int(math.Ceil(float64(b.pieceCount)/8)))
for i := 0; i < int(b.Length)-len(buf); i++ { for i := 0; i < len(buf); i++ {
temp := byte(0) temp := byte(0)
for j := 0; j < 8; j++ { for j := 0; j < 8; j++ {
// 如果piece存在,位数标识为1 // 如果piece存在,位数标识为1
if b.payload.ContainsInt(i*8 + j) { if b.bitmap.ContainsInt(i*8 + j) {
temp = temp | (1 << (7 - j)) temp = temp | (1 << (7 - j))
} }
} }
buf = append(buf, temp) buf[i] = temp
} }
return buf return encode(b.base, buf)
} }
func (b *Bitfield) Decode(buf []byte) Serialize { func (b *Bitfield) Decode(body []byte) {
b.Message = &Message{} b.bitmap = roaring.New()
b.Message.Decode(buf)
temp := buf[5:]
b.payload = roaring.New()
// 初始化bitmap // 初始化bitmap
for i := 0; i < len(temp); i++ { for i := 0; i < len(body); i++ {
for j := 0; j < 8; j++ { for j := 0; j < 8; j++ {
if temp[i]&(1<<(7-j)) > 0 { if body[i]&(1<<(7-j)) > 0 {
b.payload.AddInt(i*8 + j) b.bitmap.AddInt(i*8 + j)
} }
} }
} }
return b
} }
// 某个分片是否下载完成 // 某个分片是否下载完成
func (b *Bitfield) IsComplete(i int) bool { func (b *Bitfield) IsComplete(i int) bool {
return b.payload.ContainsInt(i) return b.bitmap.ContainsInt(i)
} }
// 给定一组分片下载状态,计算出当前peer能提供下载的分片下标,如果已经拥有则不用提供 // 给定一组分片下载状态,计算出当前peer能提供下载的分片下标,如果已经拥有则不用提供
@@ -60,6 +64,6 @@ func (b *Bitfield) IsComplete(i int) bool {
// had & has = 0b10010000 & 0b01111111 = 0b00010000 // had & has = 0b10010000 & 0b01111111 = 0b00010000
// had & has ^ has = 0b00010000 ^ 0b01111111 = 0b01101111 // had & has ^ has = 0b00010000 ^ 0b01111111 = 0b01101111
func (b *Bitfield) Provide(had *roaring.Bitmap) []uint32 { func (b *Bitfield) Provide(had *roaring.Bitmap) []uint32 {
xor := roaring.Xor(roaring.And(had, b.payload), b.payload) xor := roaring.Xor(roaring.And(had, b.bitmap), b.bitmap)
return xor.ToArray() return xor.ToArray()
} }
@@ -8,7 +8,6 @@ import (
func TestBitfield_IsComplete(t *testing.T) { func TestBitfield_IsComplete(t *testing.T) {
type fields struct { type fields struct {
Message Message
payload *roaring.Bitmap payload *roaring.Bitmap
} }
type args struct { type args struct {
@@ -44,8 +43,8 @@ func TestBitfield_IsComplete(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
b := &Bitfield{ b := &Bitfield{
Message: &tt.fields.Message, pieceCount: 100,
payload: tt.fields.payload, bitmap: tt.fields.payload,
} }
if got := b.IsComplete(tt.args.i); got != tt.want { if got := b.IsComplete(tt.args.i); got != tt.want {
t.Errorf("Bitfield.IsComplete() = %v, want %v", got, tt.want) t.Errorf("Bitfield.IsComplete() = %v, want %v", got, tt.want)
@@ -54,9 +53,8 @@ func TestBitfield_IsComplete(t *testing.T) {
} }
} }
func TestBitfield_Have(t *testing.T) { func TestBitfield_Provide(t *testing.T) {
type fields struct { type fields struct {
Message Message
payload *roaring.Bitmap payload *roaring.Bitmap
} }
type args struct { type args struct {
@@ -102,8 +100,8 @@ func TestBitfield_Have(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
b := &Bitfield{ b := &Bitfield{
Message: &tt.fields.Message, pieceCount: 100,
payload: tt.fields.payload, bitmap: tt.fields.payload,
} }
if got := b.Provide(tt.args.had); !reflect.DeepEqual(got, tt.want) { if got := b.Provide(tt.args.had); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Bitfield.Provide() = %v, want %v", got, tt.want) t.Errorf("Bitfield.Provide() = %v, want %v", got, tt.want)
+3 -13
View File
@@ -2,23 +2,13 @@ package message
// choke: <len=0001><id=0> // choke: <len=0001><id=0>
type Choke struct { type Choke struct {
Message *base
} }
func NewChoke() *Choke { func NewChoke() *Choke {
return &Choke{ return &Choke{
Message{ base: &base{
Length: 1, id: IdChoke,
ID: IdChoke,
}, },
} }
} }
func (c *Choke) Encode() []byte {
return c.Message.Encode()
}
func (c *Choke) Decode(buf []byte) Serialize {
c.Message.Decode(buf)
return c
}
+4 -14
View File
@@ -1,24 +1,14 @@
package message package message
// choke: <len=0001><id=0> // choke: <len=0001><id=2>
type Interested struct { type Interested struct {
Message *base
} }
func NewInterested() *Interested { func NewInterested() *Interested {
return &Interested{ return &Interested{
Message{ base: &base{
Length: 1, id: IdInterested,
ID: IdInterested,
}, },
} }
} }
func (c *Interested) Encode() []byte {
return c.Message.Encode()
}
func (c *Interested) Decode(buf []byte) Serialize {
c.Message.Decode(buf)
return c
}
+8 -4
View File
@@ -1,12 +1,16 @@
package message package message
// keep-alive: <len=0000> // keep-alive: <len=0000>
type Keepalive struct{} type Keepalive struct {
*base
}
var data = make([]byte, 4)
func (k *Keepalive) Encode() []byte { func (k *Keepalive) Encode() []byte {
return []byte{0, 0, 0, 0} return data
} }
func (k *Keepalive) Decode(buf []byte) Serialize { func (k *Keepalive) Decode(body []byte) {
return &Keepalive{}
} }
+4 -14
View File
@@ -1,24 +1,14 @@
package message package message
// choke: <len=0001><id=0> // choke: <len=0001><id=3>
type NotInterested struct { type NotInterested struct {
Message *base
} }
func NewNotInterested() *NotInterested { func NewNotInterested() *NotInterested {
return &NotInterested{ return &NotInterested{
Message{ base: &base{
Length: 1, id: IdNotInterested,
ID: IdNotinterested,
}, },
} }
} }
func (ni *NotInterested) Encode() []byte {
return ni.Message.Encode()
}
func (ni *NotInterested) Decode(buf []byte) Serialize {
ni.Message.Decode(buf)
return ni
}
+23 -22
View File
@@ -4,38 +4,39 @@ import "encoding/binary"
// piece: <len=0009+X><id=7><index><begin><block> // piece: <len=0009+X><id=7><index><begin><block>
type Piece struct { type Piece struct {
*Message *base
Index uint32 Index uint32
Begin uint32 Begin uint32
Block []byte Block []byte
} }
func NewPiece(index uint32, begin uint32, block []byte) *Piece { func NewPiece() *Piece {
return &Piece{ return &Piece{
Message: &Message{ base: &base{
Length: 9 + uint32(len(block)), id: IdPiece,
ID: IdPiece,
}, },
Index: index,
Begin: begin,
// Block: io.LimitReader(reader, int64(length)),
Block: block,
} }
} }
func (p *Piece) Encode() []byte { func BuildPiece(index uint32, begin uint32, block []byte) *Piece {
buf := make([]byte, 8) piece := NewPiece()
binary.BigEndian.PutUint32(buf[:4], p.Index) piece.Index = index
binary.BigEndian.PutUint32(buf[4:8], p.Begin) piece.Begin = begin
buf = append(buf, p.Block...) piece.Block = block
return append(p.Message.Encode(), buf...) return piece
} }
func (p *Piece) Decode(buf []byte) Serialize { func (p *Piece) Encode() []byte {
p.Message = &Message{} buf := make([]byte, 8+len(p.Block))
p.Message.Decode(buf) binary.BigEndian.PutUint32(buf[0:4], p.Index)
p.Index = binary.BigEndian.Uint32(buf[5:9]) binary.BigEndian.PutUint32(buf[4:8], p.Begin)
p.Begin = binary.BigEndian.Uint32(buf[9:13]) copy(buf[8:], p.Block)
p.Block = buf[13:] return encode(p.base, buf)
return p }
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:]
} }
+17 -15
View File
@@ -6,36 +6,38 @@ import (
// request: <len=0013><id=6><index><begin><length> // request: <len=0013><id=6><index><begin><length>
type Request struct { type Request struct {
Message *base
Index uint32 Index uint32
Begin uint32 Begin uint32
Length uint32 Length uint32
} }
func NewRequest(index uint32, begin uint32, length uint32) *Request { func NewRequest() *Request {
return &Request{ return &Request{
Message: Message{ base: &base{
Length: 13, id: IdRequest,
ID: IdRequest,
}, },
Index: index,
Begin: begin,
Length: length,
} }
} }
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 { func (r *Request) Encode() []byte {
buf := make([]byte, 12) buf := make([]byte, 12)
binary.BigEndian.PutUint32(buf[0:4], r.Index) binary.BigEndian.PutUint32(buf[0:4], r.Index)
binary.BigEndian.PutUint32(buf[4:8], r.Begin) binary.BigEndian.PutUint32(buf[4:8], r.Begin)
binary.BigEndian.PutUint32(buf[8:12], r.Length) binary.BigEndian.PutUint32(buf[8:12], r.Length)
return append(r.Message.Encode(), buf...) return encode(r.base, buf)
} }
func (r *Request) Decode(buf []byte) Serialize { func (r *Request) Decode(buf []byte) {
r.Message.Decode(buf) r.Length = binary.BigEndian.Uint32(buf[0:4])
r.Length = binary.BigEndian.Uint32(buf[5:9]) r.Begin = binary.BigEndian.Uint32(buf[4:8])
r.Begin = binary.BigEndian.Uint32(buf[9:13]) r.Index = binary.BigEndian.Uint32(buf[8:12])
r.Index = binary.BigEndian.Uint32(buf[13:17])
return r
} }
+3 -13
View File
@@ -2,23 +2,13 @@ package message
// unchoke: <len=0001><id=1> // unchoke: <len=0001><id=1>
type Unchoke struct { type Unchoke struct {
Message *base
} }
func NewUnchoke() *Unchoke { func NewUnchoke() *Unchoke {
return &Unchoke{ return &Unchoke{
Message{ base: &base{
Length: 1, id: IdUnchoke,
ID: IdUnchoke,
}, },
} }
} }
func (u *Unchoke) Encode() []byte {
return u.Message.Encode()
}
func (u *Unchoke) Decode(buf []byte) Serialize {
u.Message.Decode(buf)
return u
}
+5 -6
View File
@@ -155,8 +155,7 @@ func (pc *peerConn) ready() error {
if length == 0 { if length == 0 {
// keepalive // keepalive
} else { } else {
messageID := message.ID(buf[4]) switch message.ID(buf[4]) {
switch messageID {
case message.IdChoke: case message.IdChoke:
break break
case message.IdUnchoke: case message.IdUnchoke:
@@ -164,7 +163,7 @@ func (pc *peerConn) ready() error {
break break
case message.IdInterested: case message.IdInterested:
break break
case message.IdNotinterested: case message.IdNotInterested:
break break
case message.IdHave: case message.IdHave:
break break
@@ -245,7 +244,7 @@ func (pc *peerConn) downloadPiece(index int) (err error) {
return return
} }
// 发起request,对方会响应piece // 发起request,对方会响应piece
_, err = pc.conn.Write(message.NewRequest(uint32(index), uint32(offset), blockLength).Encode()) _, err = pc.conn.Write(message.BuildRequest(uint32(index), uint32(offset), blockLength).Encode())
if err != nil { if err != nil {
break break
} }
@@ -279,7 +278,7 @@ func (pc *peerConn) handleUnchoke(readyCh chan<- bool) {
} }
func (pc *peerConn) handleBitfield(buf []byte) { func (pc *peerConn) handleBitfield(buf []byte) {
pc.bitfield = &message.Bitfield{} pc.bitfield = message.NewBitfield()
pc.bitfield.Decode(buf) pc.bitfield.Decode(buf)
have := pc.getHavePieces(pc.bitfield) have := pc.getHavePieces(pc.bitfield)
if len(have) > 0 { if len(have) > 0 {
@@ -296,7 +295,7 @@ func (pc *peerConn) handleBitfield(buf []byte) {
// 处理下载响应,每次接收到响应直接将block写入到对应文件中 // 处理下载响应,每次接收到响应直接将block写入到对应文件中
func (pc *peerConn) handlePiece(buf []byte) { func (pc *peerConn) handlePiece(buf []byte) {
piece := &message.Piece{} piece := message.NewPiece()
piece.Decode(buf) piece.Decode(buf)
info := pc.torrent.MetaInfo.Info info := pc.torrent.MetaInfo.Info
fds := pc.torrent.MetaInfo.GetFileDetails() fds := pc.torrent.MetaInfo.GetFileDetails()