bittorrent-0.0.0.3: Bittorrent protocol implementation.

Portabilityportable
Stabilityexperimental
Maintainerpxqr.sta@gmail.com
Safe HaskellNone

Data.Torrent.Piece

Contents

Description

Pieces are used to validate torrent content.

Synopsis

Piece attributes

type PieceIx = IntSource

Zero-based index of piece in torrent content.

type PieceCount = IntSource

Number of pieces in torrent or a part of torrent.

type PieceSize = IntSource

Size of piece in bytes. Should be a power of 2.

NOTE: Have max and min size constrained to wide used semi-standard values. This bounds should be used to make decision about piece size for new torrents.

minPieceSize :: IntSource

Piece size should not be less than this value.

maxPieceSize :: IntSource

To prevent transfer degradation piece size should not exceed this value.

defaultPieceSize :: Int64 -> IntSource

Find the optimal piece size for a given torrent size.

Piece data

data Piece a Source

Piece payload should be strict or lazy bytestring.

Constructors

Piece 

Fields

pieceIndex :: !PieceIx

Zero-based piece index in torrent.

pieceData :: !a

Payload.

Instances

Typeable1 Piece 
Eq a => Eq (Piece a) 
Read a => Read (Piece a) 
Show a => Show (Piece a) 
ToJSON a0 => ToJSON (Piece a0) 
FromJSON a0 => FromJSON (Piece a0) 
NFData (Piece a) 
Pretty (Piece a)

Payload bytes are omitted.

pieceSize :: Piece BL.ByteString -> PieceSizeSource

Get size of piece in bytes.

isPiece :: PieceSize -> Block BL.ByteString -> BoolSource

Test if a block can be safely turned into a piece.

Piece control

newtype HashArray Source

A flat array of SHA1 sums of each piece.

Constructors

HashArray 

Instances

Eq HashArray 
Read HashArray 
Show HashArray 
ToJSON HashArray

Represented as base64 encoded JSON string.

FromJSON HashArray 
BEncode HashArray 

data PieceInfo Source

Part of torrent file used for torrent content validation.

Constructors

PieceInfo 

Fields

piPieceLength :: !PieceSize

Number of bytes in each piece.

piPieceHashes :: !HashArray

Concatenation of all 20-byte SHA1 hash values.

pieceCount :: PieceInfo -> PieceCountSource

Find count of pieces in the torrent. If torrent size is not a multiple of piece size then the count is rounded up.

Lens

pieceLength :: Lens' PieceInfo PieceSizeSource

Number of bytes in each piece.

pieceHashes :: Lens' PieceInfo HashArraySource

Concatenation of all 20-byte SHA1 hash values.

Validation

pieceHash :: PieceInfo -> PieceIx -> BS.ByteStringSource

Extract validation hash by specified piece index.

checkPieceLazy :: PieceInfo -> Piece BL.ByteString -> BoolSource

Validate piece with metainfo hash.

Internal

putPieceInfo :: PieceInfo -> BDict -> BDictSource