NewMemory supports both seeder and leecher

This commit is contained in:
whr819987540 2023-05-08 15:48:53 +08:00
parent a68c49defc
commit f58ec9e188
1 changed files with 30 additions and 16 deletions

View File

@ -14,7 +14,7 @@ import (
"github.com/anacrolix/torrent/segments" "github.com/anacrolix/torrent/segments"
) )
type MemoryBuf struct{ type MemoryBuf struct {
Data []byte Data []byte
Length int64 Length int64
} }
@ -87,6 +87,14 @@ type MemoryClientImpl struct {
opts NewMemoryClientOpts opts NewMemoryClientOpts
} }
// GetData return a torrent's data in memory
func (mci MemoryClientImpl) GetData() *MemoryBuf {
return &MemoryBuf{
Data: mci.opts.Torrent.data,
Length: mci.opts.Torrent.length,
}
}
// Close 关闭操作piece completion的sqlite连接 // Close 关闭操作piece completion的sqlite连接
func (mci MemoryClientImpl) Close() error { func (mci MemoryClientImpl) Close() error {
return mci.opts.PieceCompletion.Close() return mci.opts.PieceCompletion.Close()
@ -132,16 +140,18 @@ func (mci MemoryClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.H
}, nil }, nil
} }
// NewMemory creates a memory block to store all Torrent data // NewMemory acts differently for seeders and leechers
func NewMemory(totalLength int64,mb *MemoryBuf) (ClientImplCloser, error) { // for seeders, mb should have already contained the data. mb is not nill
return NewMemoryWithCompletion(totalLength,mb) // for leechers, mb should be nil. but we can access the downloaded date through the returned mb
func NewMemory(totalLength int64, mb *MemoryBuf) (ClientImplCloser, *MemoryBuf, error) {
return NewMemoryWithCompletion(totalLength, mb)
} }
// NewMemoryWithCompletion 中Completion是指piece completion, 用来记录piece的完成情况(已经实现的是sqlite) // NewMemoryWithCompletion 中Completion是指piece completion, 用来记录piece的完成情况(已经实现的是sqlite)
// 但这个并不是必须的 // 但这个并不是必须的
func NewMemoryWithCompletion(totalLength int64,mb *MemoryBuf) (ClientImplCloser, error) { func NewMemoryWithCompletion(totalLength int64, mb *MemoryBuf) (ClientImplCloser, *MemoryBuf, error) {
var opts NewMemoryClientOpts var opts NewMemoryClientOpts
if mb!=nil{ if mb != nil { // seeders
// storage.ClientImplCloser 实际返回 memoryClientImpl // storage.ClientImplCloser 实际返回 memoryClientImpl
opts = NewMemoryClientOpts{ opts = NewMemoryClientOpts{
Torrent: &memoryBuf{ Torrent: &memoryBuf{
@ -151,10 +161,10 @@ func NewMemoryWithCompletion(totalLength int64,mb *MemoryBuf) (ClientImplCloser,
}, },
PieceCompletion: pieceCompletionForDir("./"), // 默认选择sqlite, sqlite的db文件放在./下面 PieceCompletion: pieceCompletionForDir("./"), // 默认选择sqlite, sqlite的db文件放在./下面
} }
}else{ } else { // leechers
// allocate memory // allocate memory
if flag, avail := isAllocatedOutOfHeapMemory(totalLength); !flag { if flag, avail := isAllocatedOutOfHeapMemory(totalLength); !flag {
return nil, fmt.Errorf("try to allocate too much memory, want %d, available %d", totalLength, avail) return nil, nil, fmt.Errorf("try to allocate too much memory, want %d, available %d", totalLength, avail)
} }
data := make([]byte, totalLength) data := make([]byte, totalLength)
var p *byte = &data[0] var p *byte = &data[0]
@ -169,9 +179,13 @@ func NewMemoryWithCompletion(totalLength int64,mb *MemoryBuf) (ClientImplCloser,
}, },
PieceCompletion: pieceCompletionForDir("./"), // 默认选择sqlite, sqlite的db文件放在./下面 PieceCompletion: pieceCompletionForDir("./"), // 默认选择sqlite, sqlite的db文件放在./下面
} }
mb = &MemoryBuf{
Data: data,
Length: totalLength,
}
} }
return MemoryClientImpl{opts}, nil return MemoryClientImpl{opts}, mb, nil
} }
// NewMemoryOpts creates a new MemoryImplCloser that stores files using memory // NewMemoryOpts creates a new MemoryImplCloser that stores files using memory