Generate default peerExtensionBytes using helpers

This commit is contained in:
Matt Joiner 2018-02-03 13:36:17 +11:00
parent 738a75bc1c
commit 085e676ef0
4 changed files with 13 additions and 15 deletions

View File

@ -278,7 +278,7 @@ func NewClient(cfg *Config) (cl *Client, err error) {
} else {
cl.downloadLimit = cfg.DownloadRateLimiter
}
missinggo.CopyExact(&cl.extensionBytes, defaultExtensionBytes)
cl.extensionBytes = defaultPeerExtensionBytes()
cl.event.L = &cl.mu
storageImpl := cfg.DefaultStorage
if storageImpl == nil {

View File

@ -10,25 +10,16 @@ const (
maxRequests = 250 // Maximum pending requests we allow peers to send us.
defaultChunkSize = 0x4000 // 16KiB
// Justification for set bits follows.
//
// Extension protocol ([5]|=0x10):
// http://www.bittorrent.org/beps/bep_0010.html
//
// Fast Extension ([7]|=0x04):
// http://bittorrent.org/beps/bep_0006.html.
// Disabled until AllowedFast is implemented. TODO
//
// DHT ([7]|=1):
// http://www.bittorrent.org/beps/bep_0005.html
defaultExtensionBytes = "\x00\x00\x00\x00\x00\x10\x00\x01"
// These are our extended message IDs. Peers will use these values to
// select which extension a message is intended for.
metadataExtendedId = iota + 1 // 0 is reserved for deleting keys
pexExtendedId
)
func defaultPeerExtensionBytes() peerExtensionBytes {
return newPeerExtensionBytes(ExtensionBitDHT, ExtensionBitExtended)
}
// I could move a lot of these counters to their own file, but I suspect they
// may be attached to a Client someday.
var (

View File

@ -38,6 +38,13 @@ type (
peerExtensionBytes [8]byte
)
func newPeerExtensionBytes(bits ...ExtensionBit) (ret peerExtensionBytes) {
for _, b := range bits {
ret.SetBit(b)
}
return
}
func (pex peerExtensionBytes) SupportsExtended() bool {
return pex.GetBit(ExtensionBitExtended)
}

View File

@ -9,7 +9,7 @@ import (
func TestDefaultExtensionBytes(t *testing.T) {
var pex peerExtensionBytes
missinggo.CopyExact(&pex, defaultExtensionBytes)
missinggo.CopyExact(&pex, defaultPeerExtensionBytes())
assert.True(t, pex.SupportsDHT())
assert.True(t, pex.SupportsExtended())
assert.False(t, pex.SupportsFast())