本文整理汇总了Golang中github.com/anacrolix/torrent/metainfo.InfoEx.Hash方法的典型用法代码示例。如果您正苦于以下问题:Golang InfoEx.Hash方法的具体用法?Golang InfoEx.Hash怎么用?Golang InfoEx.Hash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/anacrolix/torrent/metainfo.InfoEx
的用法示例。
在下文中一共展示了InfoEx.Hash方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestPeerInvalidHave
// Ensure that it's an error for a peer to send an invalid have message.
func TestPeerInvalidHave(t *testing.T) {
cl, err := NewClient(&TestingConfig)
require.NoError(t, err)
defer cl.Close()
ie := metainfo.InfoEx{
Info: metainfo.Info{
PieceLength: 1,
Pieces: make([]byte, 20),
Files: []metainfo.FileInfo{{Length: 1}},
},
}
ie.UpdateBytes()
tt, _new, err := cl.AddTorrentSpec(&TorrentSpec{
Info: &ie,
InfoHash: ie.Hash(),
})
require.NoError(t, err)
assert.True(t, _new)
defer tt.Drop()
cn := &connection{
t: tt,
}
assert.NoError(t, cn.peerSentHave(0))
assert.Error(t, cn.peerSentHave(1))
}
示例2: TestCompletedPieceWrongSize
// We read from a piece which is marked completed, but is missing data.
func TestCompletedPieceWrongSize(t *testing.T) {
cfg := TestingConfig
cfg.DefaultStorage = badStorage{}
cl, err := NewClient(&cfg)
require.NoError(t, err)
defer cl.Close()
ie := metainfo.InfoEx{
Info: metainfo.Info{
PieceLength: 15,
Pieces: make([]byte, 20),
Files: []metainfo.FileInfo{
metainfo.FileInfo{Path: []string{"greeting"}, Length: 13},
},
},
}
ie.UpdateBytes()
tt, new, err := cl.AddTorrentSpec(&TorrentSpec{
Info: &ie,
InfoHash: ie.Hash(),
})
require.NoError(t, err)
defer tt.Drop()
assert.True(t, new)
r := tt.NewReader()
defer r.Close()
b, err := ioutil.ReadAll(r)
assert.Len(t, b, 13)
assert.NoError(t, err)
}
示例3: setInfoBytes
// Called when metadata for a torrent becomes available.
func (t *Torrent) setInfoBytes(b []byte) error {
if t.haveInfo() {
return nil
}
var ie *metainfo.InfoEx
err := bencode.Unmarshal(b, &ie)
if err != nil {
return fmt.Errorf("error unmarshalling info bytes: %s", err)
}
if ie.Hash() != t.infoHash {
return errors.New("info bytes have wrong hash")
}
err = validateInfo(&ie.Info)
if err != nil {
return fmt.Errorf("bad info: %s", err)
}
defer t.updateWantPeersEvent()
t.info = ie
t.cl.event.Broadcast()
t.gotMetainfo.Set()
t.storage, err = t.storageOpener.OpenTorrent(t.info)
if err != nil {
return fmt.Errorf("error opening torrent storage: %s", err)
}
t.length = 0
for _, f := range t.info.UpvertedFiles() {
t.length += f.Length
}
t.metadataBytes = b
t.metadataCompletedChunks = nil
hashes := infoPieceHashes(&t.info.Info)
t.pieces = make([]piece, len(hashes))
for i, hash := range hashes {
piece := &t.pieces[i]
piece.t = t
piece.index = i
piece.noPendingWrites.L = &piece.pendingWritesMutex
missinggo.CopyExact(piece.Hash[:], hash)
}
for _, conn := range t.conns {
if err := conn.setNumPieces(t.numPieces()); err != nil {
log.Printf("closing connection: %s", err)
conn.Close()
}
}
for i := range t.pieces {
t.updatePieceCompletion(i)
t.pieces[i].QueuedForHash = true
}
go func() {
for i := range t.pieces {
t.verifyPiece(i)
}
}()
return nil
}
示例4: setInfoBytes
// Called when metadata for a torrent becomes available.
func (t *Torrent) setInfoBytes(b []byte) error {
if t.haveInfo() {
return nil
}
var ie *metainfo.InfoEx
err := bencode.Unmarshal(b, &ie)
if err != nil {
return fmt.Errorf("error unmarshalling info bytes: %s", err)
}
if ie.Hash() != t.infoHash {
return errors.New("info bytes have wrong hash")
}
err = validateInfo(&ie.Info)
if err != nil {
return fmt.Errorf("bad info: %s", err)
}
defer t.updateWantPeersEvent()
t.info = ie
t.displayName = "" // Save a few bytes lol.
t.cl.event.Broadcast()
t.gotMetainfo.Set()
t.storage, err = t.storageOpener.OpenTorrent(t.info)
if err != nil {
return fmt.Errorf("error opening torrent storage: %s", err)
}
t.length = 0
for _, f := range t.info.UpvertedFiles() {
t.length += f.Length
}
t.metadataBytes = b
t.metadataCompletedChunks = nil
t.makePieces()
for _, conn := range t.conns {
if err := conn.setNumPieces(t.numPieces()); err != nil {
log.Printf("closing connection: %s", err)
conn.Close()
}
}
for i := range t.pieces {
t.updatePieceCompletion(i)
t.pieces[i].QueuedForHash = true
}
go func() {
for i := range t.pieces {
t.verifyPiece(i)
}
}()
return nil
}