support seeding
This commit is contained in:
parent
1346784b01
commit
bc82214838
|
@ -14,6 +14,11 @@ import (
|
||||||
"github.com/anacrolix/torrent/segments"
|
"github.com/anacrolix/torrent/segments"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MemoryBuf struct{
|
||||||
|
Data []byte
|
||||||
|
Length int64
|
||||||
|
}
|
||||||
|
|
||||||
// 专门用来对内存进行读写
|
// 专门用来对内存进行读写
|
||||||
// 基于指定的[]byte生成一个buffer, 可以从某个偏移量开始写入指定的数据
|
// 基于指定的[]byte生成一个buffer, 可以从某个偏移量开始写入指定的数据
|
||||||
// 返回实际写入的字节数, error
|
// 返回实际写入的字节数, error
|
||||||
|
@ -128,13 +133,25 @@ func (mci MemoryClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.H
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMemory creates a memory block to store all Torrent data
|
// NewMemory creates a memory block to store all Torrent data
|
||||||
func NewMemory(totalLength int64) (ClientImplCloser, error) {
|
func NewMemory(totalLength int64,mb *MemoryBuf) (ClientImplCloser, error) {
|
||||||
return NewMemoryWithCompletion(totalLength)
|
return NewMemoryWithCompletion(totalLength,mb)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMemoryWithCompletion 中Completion是指piece completion, 用来记录piece的完成情况(已经实现的是sqlite)
|
// NewMemoryWithCompletion 中Completion是指piece completion, 用来记录piece的完成情况(已经实现的是sqlite)
|
||||||
// 但这个并不是必须的
|
// 但这个并不是必须的
|
||||||
func NewMemoryWithCompletion(totalLength int64) (ClientImplCloser, error) {
|
func NewMemoryWithCompletion(totalLength int64,mb *MemoryBuf) (ClientImplCloser, error) {
|
||||||
|
var opts NewMemoryClientOpts
|
||||||
|
if mb!=nil{
|
||||||
|
// storage.ClientImplCloser 实际返回 memoryClientImpl
|
||||||
|
opts = NewMemoryClientOpts{
|
||||||
|
Torrent: &memoryBuf{
|
||||||
|
data: mb.Data,
|
||||||
|
length: mb.Length,
|
||||||
|
totalWrite: 0,
|
||||||
|
},
|
||||||
|
PieceCompletion: pieceCompletionForDir("./"), // 默认选择sqlite, sqlite的db文件放在./下面
|
||||||
|
}
|
||||||
|
}else{
|
||||||
// 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, fmt.Errorf("try to allocate too much memory, want %d, available %d", totalLength, avail)
|
||||||
|
@ -144,7 +161,7 @@ func NewMemoryWithCompletion(totalLength int64) (ClientImplCloser, error) {
|
||||||
log.Printf("%d %d %v", len(data), cap(data), p)
|
log.Printf("%d %d %v", len(data), cap(data), p)
|
||||||
|
|
||||||
// storage.ClientImplCloser 实际返回 memoryClientImpl
|
// storage.ClientImplCloser 实际返回 memoryClientImpl
|
||||||
opts := NewMemoryClientOpts{
|
opts = NewMemoryClientOpts{
|
||||||
Torrent: &memoryBuf{
|
Torrent: &memoryBuf{
|
||||||
data: data,
|
data: data,
|
||||||
length: totalLength,
|
length: totalLength,
|
||||||
|
@ -152,6 +169,8 @@ func NewMemoryWithCompletion(totalLength int64) (ClientImplCloser, error) {
|
||||||
},
|
},
|
||||||
PieceCompletion: pieceCompletionForDir("./"), // 默认选择sqlite, sqlite的db文件放在./下面
|
PieceCompletion: pieceCompletionForDir("./"), // 默认选择sqlite, sqlite的db文件放在./下面
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return MemoryClientImpl{opts}, nil
|
return MemoryClientImpl{opts}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue