p2p

package
v0.0.0-...-329f2e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2019 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HAVE 每当客户端下载了一个piece,即将该piece的下标作为have消息的负载构造have消息,并把该消息发送给所有建立连接的peer
	HAVE = iota

	// BITFIELD 交换位图
	BITFIELD

	// REQUEST 向该peer发送数据请求
	REQUEST

	// PIECE 当客户端收到某个peer的request消息后,则发送piece消息将文件数据传给该peer。
	PIECE
)

Variables

This section is empty.

Functions

func CreateListener

func CreateListener(cfg *common.Config) (listener net.Listener, err error)

CreateListener ...

func StartListen

func StartListen(cfg *common.Config) (conChan chan *PeerConn, listener net.Listener, err error)

StartListen listens on a TCP port for incoming connections and demuxes them to the appropriate active p2pSession based on the �taskId in the header.

Types

type ActivePiece

type ActivePiece struct {
	// contains filtered or unexported fields
}

ActivePiece 正在下载的Piece

func NewActivePiece

func NewActivePiece(pieceLength int) *ActivePiece

NewActivePiece ...

type Bitset

type Bitset struct {
	// contains filtered or unexported fields
}

Bitset As defined by the bittorrent protocol, this bitset is big-endian, such that the high bit of the first byte is block 0

func NewBitset

func NewBitset(n int) *Bitset

NewBitset ...

func NewBitsetFromBytes

func NewBitsetFromBytes(n int, data []byte) *Bitset

NewBitsetFromBytes Creates a new bitset from a given byte stream. Returns nil if the data is invalid in some way.

func (*Bitset) Bytes

func (b *Bitset) Bytes() []byte

Bytes ...

func (*Bitset) Clear

func (b *Bitset) Clear(index int)

Clear ...

func (*Bitset) FindNextClear

func (b *Bitset) FindNextClear(index int) int

FindNextClear ... TODO: Make this fast

func (*Bitset) FindNextSet

func (b *Bitset) FindNextSet(index int) int

FindNextSet ... TODO: Make this fast

func (*Bitset) InRange

func (b *Bitset) InRange(index int) bool

InRange ...

func (*Bitset) IsEndValid

func (b *Bitset) IsEndValid() bool

IsEndValid ...

func (*Bitset) IsSet

func (b *Bitset) IsSet(index int) bool

IsSet ...

func (*Bitset) Len

func (b *Bitset) Len() int

Len ...

func (*Bitset) Set

func (b *Bitset) Set(index int)

Set ...

type CacheProvider

type CacheProvider interface {
	NewCache(infohash string, numPieces int, pieceLength int, totalSize int64) FileCache
}

CacheProvider ...

func NewRAMCacheProvider

func NewRAMCacheProvider(capacity int) CacheProvider

NewRAMCacheProvider ...

type DispatchTask

type DispatchTask struct {
	TaskID    string     `json:"taskId"`
	MetaInfo  *MetaInfo  `json:"metaInfo"`
	LinkChain *LinkChain `json:"linkChain"`
	Speed     int64      `json:"speed"`
}

DispatchTask 下发给Agent的分发任务

type File

type File interface {
	io.ReaderAt
	io.WriterAt
	io.Closer
}

File Interface for a file. Multiple goroutines may access a File at the same time.

type FileCache

type FileCache interface {

	//Marks a piece as committed to permanent storage.
	MarkCommitted(piece int)
	//Close the cache and free all the things
	Close()
	// contains filtered or unexported methods
}

FileCache ...

type FileDict

type FileDict struct {
	Length int64  `json:"length"`
	Path   string `json:"path"`
	Name   string `json:"name"`
	Sum    string `json:"sum" `
}

FileDict 一个文件的元数据信息

type FileStore

type FileStore interface {
	io.ReaderAt
	io.WriterAt
	io.Closer
	SetCache(FileCache)
	Commit(int, []byte, int64)
}

FileStore a file store.

func NewFileStore

func NewFileStore(info *MetaInfo, fileSystem FileSystem) (f FileStore, totalSize int64, err error)

NewFileStore 根据元数据信息打开所有文件

type FileSystem

type FileSystem interface {
	Open(name []string, length int64) (file File, err error)
	io.Closer
}

FileSystem Interface for a file system. A file system contains files.

type FsProvider

type FsProvider interface {
	NewFS() (FileSystem, error)
}

FsProvider Interface for a provider of filesystems.

type LinkChain

type LinkChain struct {
	// 软件分发的路径,要求服务端的地址排在第一个
	DispatchAddrs []string `json:"dispatchAddrs"`
	// 服务端管理接口,用于上报状态
	ServerAddr string `json:"serverAddr"`
}

LinkChain 分发路径

type MetaInfo

type MetaInfo struct {
	Length   int64       `json:"length"`
	PieceLen int64       `json:"PieceLen"`
	Pieces   []byte      `json:"pieces"`
	Files    []*FileDict `json:"files"`
}

MetaInfo 一个任务内所有文件的元数据信息

func CreateFileMeta

func CreateFileMeta(roots []string, pieceLen int64) (mi *MetaInfo, err error)

CreateFileMeta ...

type OsFsProvider

type OsFsProvider struct{}

OsFsProvider ...

func (OsFsProvider) NewFS

func (o OsFsProvider) NewFS() (fs FileSystem, err error)

NewFS ...

type PHeader

type PHeader struct {
	Len      int32
	TaskID   string
	Username string
	Password string
	Salt     string
}

PHeader 连接认证消息头

type PeerConn

type PeerConn struct {
	// contains filtered or unexported fields
}

PeerConn wraps an incoming network connection and contains metadata that helps identify which active p2pSession it's relevant for.

type RAMCache

type RAMCache struct {
	// contains filtered or unexported fields
}

RAMCache ... 'pieceSize' is the size of the average piece 'capacity' is how many pieces the cache can hold 'actualUsage' is how many pieces the cache has at the moment 'atime' is an array of access times for each stored box 'store' is an array of "boxes" ([]byte of 1 piece each) 'isBoxFull' indicates if a box entirely contains written data 'isBoxCommit' indicates if a box has been committed to storage 'isByteSet' for [i] indicates for box 'i' if a byte has been written to 'torrentLength' is the number of bytes in the torrent 'cacheProvider' is a pointer to the cacheProvider that created this cache 'infohash' is the infohash of the torrent

func (*RAMCache) Close

func (r *RAMCache) Close()

Close ...

func (*RAMCache) MarkCommitted

func (r *RAMCache) MarkCommitted(piece int)

MarkCommitted ...

type RAMCacheProvider

type RAMCacheProvider struct {
	// contains filtered or unexported fields
}

RAMCacheProvider provider creates a ram cache for each torrent. Each time a cache is created or closed, all cache are recalculated so they total <= capacity (in MiB).

func (*RAMCacheProvider) NewCache

func (r *RAMCacheProvider) NewCache(infohash string, numPieces int, pieceSize int, torrentLength int64) FileCache

NewCache ...

type StartTask

type StartTask struct {
	TaskID    string     `json:"taskId"`
	LinkChain *LinkChain `json:"linkChain"`
}

StartTask 下发给Agent的分发任务

type StatusReport

type StatusReport struct {
	TaskID          string  `json:"taskId"`
	IP              string  `json:"�ip"`
	PercentComplete float32 `json:"percentComplete"`
}

StatusReport Agent分发状态上报

type TaskSession

type TaskSession struct {
	// contains filtered or unexported fields
}

TaskSession ...

func NewTaskSession

func NewTaskSession(g *global, dt *DispatchTask, stopSessChan chan string) (s *TaskSession, err error)

NewTaskSession ...

func (*TaskSession) AcceptNewPeer

func (s *TaskSession) AcceptNewPeer(c *PeerConn)

AcceptNewPeer 接入其它的Peer连接

func (*TaskSession) ClosePeer

func (s *TaskSession) ClosePeer(peer *peer)

ClosePeer 关闭Peer

func (*TaskSession) Init

func (s *TaskSession) Init()

Init 初始化

func (*TaskSession) Quit

func (s *TaskSession) Quit() (err error)

Quit ...

func (*TaskSession) Start

func (s *TaskSession) Start(st *StartTask)

Start ...

type TaskSessionMgnt

type TaskSessionMgnt struct {
	// contains filtered or unexported fields
}

TaskSessionMgnt ...

func NewSessionMgnt

func NewSessionMgnt(cfg *common.Config) *TaskSessionMgnt

NewSessionMgnt ...

func (*TaskSessionMgnt) CreateTask

func (sm *TaskSessionMgnt) CreateTask(dt *DispatchTask)

CreateTask 创建一个任务

func (*TaskSessionMgnt) Start

func (sm *TaskSessionMgnt) Start() error

Start 启动监控

func (*TaskSessionMgnt) StartTask

func (sm *TaskSessionMgnt) StartTask(st *StartTask)

StartTask 启动一个任务

func (*TaskSessionMgnt) Stop

func (sm *TaskSessionMgnt) Stop()

Stop 停止所有的任务,并退出监控

func (*TaskSessionMgnt) StopTask

func (sm *TaskSessionMgnt) StopTask(taskID string)

StopTask 停止一下任务

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL