forked from GopeedLab/gopeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
415 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.