diff options
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/Block.hs')
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Block.hs | 35 |
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 @@ | |||
1 | module Network.BitTorrent.PeerWire.Block | 1 | module 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 | ||
9 | import Control.Applicative | ||
8 | import Data.ByteString (ByteString) | 10 | import Data.ByteString (ByteString) |
9 | import qualified Data.ByteString as B | 11 | import qualified Data.ByteString as B |
10 | import Data.Int | 12 | import Data.Int |
11 | 13 | ||
14 | type BlockLIx = Int | ||
15 | type PieceLIx = Int | ||
16 | |||
12 | 17 | ||
13 | data BlockIx = BlockIx { | 18 | data 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 | |||
19 | data Block = Block { | 30 | data 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 | |||
37 | pieceIx i = BlockIx i 0 | 53 | pieceIx i = BlockIx i 0 |
38 | {-# INLINE pieceIx #-} | 54 | {-# INLINE pieceIx #-} |
39 | 55 | ||
56 | blockIx :: Block -> BlockIx | ||
57 | blockIx = BlockIx <$> blkPiece <*> blkOffset <*> B.length . blkData | ||
58 | |||
40 | blockRange :: (Num a, Integral a) => Int -> Block -> (a, a) | 59 | blockRange :: (Num a, Integral a) => Int -> Block -> (a, a) |
41 | blockRange pieceSize blk = (offset, offset + len) | 60 | blockRange pieceSize blk = (offset, offset + len) |
42 | where | 61 | where |