summaryrefslogtreecommitdiff
path: root/src/Data/Torrent/Block.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Torrent/Block.hs')
-rw-r--r--src/Data/Torrent/Block.hs33
1 files changed, 16 insertions, 17 deletions
diff --git a/src/Data/Torrent/Block.hs b/src/Data/Torrent/Block.hs
index ca3bef45..17907a39 100644
--- a/src/Data/Torrent/Block.hs
+++ b/src/Data/Torrent/Block.hs
@@ -7,12 +7,14 @@
7-- 7--
8-- TODO 8-- TODO
9-- 9--
10{-# LANGUAGE TemplateHaskell #-} 10{-# LANGUAGE GeneralizedNewtypeDeriving #-}
11{-# LANGUAGE TemplateHaskell #-}
11module Data.Torrent.Block 12module Data.Torrent.Block
12 ( -- * Block attributes 13 ( -- * Block attributes
13 BlockLIx 14 BlockLIx
14 , PieceLIx 15 , PieceLIx
15 , BlockSize (..) 16 , BlockSize
17 , defaultTransferSize
16 18
17 -- * Block index 19 -- * Block index
18 , BlockIx(..) 20 , BlockIx(..)
@@ -50,17 +52,14 @@ import Text.PrettyPrint
50-- Block attributes 52-- Block attributes
51-----------------------------------------------------------------------} 53-----------------------------------------------------------------------}
52 54
53newtype BlockSize = BlockSize { unBlockSize :: Int } 55type BlockSize = Int
54 deriving (Show, Eq, Num, ToJSON, FromJSON)
55
56-- | Widely used semi-official block size.
57instance Default BlockSize where
58 def = 16 * 1024
59 {-# INLINE def #-}
60
61type BlockLIx = Int 56type BlockLIx = Int
62type PieceLIx = Int 57type PieceLIx = Int
63 58
59-- | Widely used semi-official block size.
60defaultTransferSize :: BlockSize
61defaultTransferSize = 16 * 1024
62
64{----------------------------------------------------------------------- 63{-----------------------------------------------------------------------
65 Block Index 64 Block Index
66-----------------------------------------------------------------------} 65-----------------------------------------------------------------------}
@@ -98,33 +97,33 @@ instance Serialize BlockIx where
98 {-# SPECIALIZE instance Serialize BlockIx #-} 97 {-# SPECIALIZE instance Serialize BlockIx #-}
99 get = BlockIx <$> getInt 98 get = BlockIx <$> getInt
100 <*> getInt 99 <*> getInt
101 <*> (BlockSize <$> getInt) 100 <*> getInt
102 {-# INLINE get #-} 101 {-# INLINE get #-}
103 102
104 put BlockIx {..} = do 103 put BlockIx {..} = do
105 putInt ixPiece 104 putInt ixPiece
106 putInt ixOffset 105 putInt ixOffset
107 putInt (unBlockSize ixLength) 106 putInt ixLength
108 {-# INLINE put #-} 107 {-# INLINE put #-}
109 108
110instance Binary BlockIx where 109instance Binary BlockIx where
111 {-# SPECIALIZE instance Binary BlockIx #-} 110 {-# SPECIALIZE instance Binary BlockIx #-}
112 get = BlockIx <$> getIntB 111 get = BlockIx <$> getIntB
113 <*> getIntB 112 <*> getIntB
114 <*> (BlockSize <$> getIntB) 113 <*> getIntB
115 {-# INLINE get #-} 114 {-# INLINE get #-}
116 115
117 put BlockIx {..} = do 116 put BlockIx {..} = do
118 putIntB ixPiece 117 putIntB ixPiece
119 putIntB ixOffset 118 putIntB ixOffset
120 putIntB (unBlockSize ixLength) 119 putIntB ixLength
121 120
122-- | Format block index in human readable form. 121-- | Format block index in human readable form.
123ppBlockIx :: BlockIx -> Doc 122ppBlockIx :: BlockIx -> Doc
124ppBlockIx BlockIx {..} = 123ppBlockIx BlockIx {..} =
125 "piece = " <> int ixPiece <> "," <+> 124 "piece = " <> int ixPiece <> "," <+>
126 "offset = " <> int ixOffset <> "," <+> 125 "offset = " <> int ixOffset <> "," <+>
127 "length = " <> int (unBlockSize ixLength) 126 "length = " <> int ixLength
128 127
129{----------------------------------------------------------------------- 128{-----------------------------------------------------------------------
130 Block 129 Block
@@ -158,7 +157,7 @@ isPiece pieceSize (Block i offset bs) =
158{-# INLINE isPiece #-} 157{-# INLINE isPiece #-}
159 158
160pieceIx :: Int -> Int -> BlockIx 159pieceIx :: Int -> Int -> BlockIx
161pieceIx i = BlockIx i 0 . BlockSize 160pieceIx i = BlockIx i 0
162{-# INLINE pieceIx #-} 161{-# INLINE pieceIx #-}
163 162
164blockIx :: Block Lazy.ByteString -> BlockIx 163blockIx :: Block Lazy.ByteString -> BlockIx
@@ -177,5 +176,5 @@ ixRange pieceSize i = (offset, offset + len)
177 where 176 where
178 offset = fromIntegral pieceSize * fromIntegral (ixPiece i) 177 offset = fromIntegral pieceSize * fromIntegral (ixPiece i)
179 + fromIntegral (ixOffset i) 178 + fromIntegral (ixOffset i)
180 len = fromIntegral (unBlockSize (ixLength i)) 179 len = fromIntegral (ixLength i)
181{-# INLINE ixRange #-} 180{-# INLINE ixRange #-}