summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/PeerWire/Block.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/Block.hs')
-rw-r--r--src/Network/BitTorrent/PeerWire/Block.hs35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/Network/BitTorrent/PeerWire/Block.hs b/src/Network/BitTorrent/PeerWire/Block.hs
index 33e3dead..a5bc4f32 100644
--- a/src/Network/BitTorrent/PeerWire/Block.hs
+++ b/src/Network/BitTorrent/PeerWire/Block.hs
@@ -1,25 +1,41 @@
1module Network.BitTorrent.PeerWire.Block 1module Network.BitTorrent.PeerWire.Block
2 ( BlockIx(..), Block(..) 2 ( BlockIx(..), Block(..)
3 , BlockLIx, PieceLIx
3 , defaultBlockSize 4 , defaultBlockSize
4 , blockRange, ixRange, pieceIx 5 , pieceIx, blockIx
5 , isPiece 6 , blockRange, ixRange, isPiece
6 ) where 7 ) where
7 8
9import Control.Applicative
8import Data.ByteString (ByteString) 10import Data.ByteString (ByteString)
9import qualified Data.ByteString as B 11import qualified Data.ByteString as B
10import Data.Int 12import Data.Int
11 13
14type BlockLIx = Int
15type PieceLIx = Int
16
12 17
13data BlockIx = BlockIx { 18data BlockIx = BlockIx {
14 ixPiece :: {-# UNPACK #-} !Int -- ^ Zero-based piece index. 19 -- ^ Zero-based piece index.
15 , ixOffset :: {-# UNPACK #-} !Int -- ^ Zero-based byte offset within the piece. 20 ixPiece :: {-# UNPACK #-} !PieceLIx
16 , ixLength :: {-# UNPACK #-} !Int -- ^ Block size starting from offset. 21
22 -- ^ Zero-based byte offset within the piece.
23 , ixOffset :: {-# UNPACK #-} !Int
24
25 -- ^ Block size starting from offset.
26 , ixLength :: {-# UNPACK #-} !Int
17 } deriving (Show, Eq) 27 } deriving (Show, Eq)
18 28
29
19data Block = Block { 30data Block = Block {
20 blkPiece :: Int -- ^ Zero-based piece index. 31 -- ^ Zero-based piece index.
21 , blkOffset :: Int -- ^ Zero-based byte offset within the piece. 32 blkPiece :: PieceLIx
22 , blkData :: ByteString -- ^ Payload. 33
34 -- ^ Zero-based byte offset within the piece.
35 , blkOffset :: Int
36
37 -- ^ Payload.
38 , blkData :: ByteString
23 } deriving (Show, Eq) 39 } deriving (Show, Eq)
24 40
25 41
@@ -37,6 +53,9 @@ pieceIx :: Int -> Int -> BlockIx
37pieceIx i = BlockIx i 0 53pieceIx i = BlockIx i 0
38{-# INLINE pieceIx #-} 54{-# INLINE pieceIx #-}
39 55
56blockIx :: Block -> BlockIx
57blockIx = BlockIx <$> blkPiece <*> blkOffset <*> B.length . blkData
58
40blockRange :: (Num a, Integral a) => Int -> Block -> (a, a) 59blockRange :: (Num a, Integral a) => Int -> Block -> (a, a)
41blockRange pieceSize blk = (offset, offset + len) 60blockRange pieceSize blk = (offset, offset + len)
42 where 61 where