summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/PeerWire/Block.hs
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-24 07:04:10 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-24 07:04:10 +0400
commitea7947d3db0217f31dc507f930f3d9e6c6f437c0 (patch)
tree3f3a11f59db5e9d299e32f6825bd66048986d47b /src/Network/BitTorrent/PeerWire/Block.hs
parent71806baa0d07f2ea0a92698e26ce79c7c6647a50 (diff)
~ One less orphan instance.
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/Block.hs')
-rw-r--r--src/Network/BitTorrent/PeerWire/Block.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/PeerWire/Block.hs b/src/Network/BitTorrent/PeerWire/Block.hs
index a5bc4f32..8e4a1f24 100644
--- a/src/Network/BitTorrent/PeerWire/Block.hs
+++ b/src/Network/BitTorrent/PeerWire/Block.hs
@@ -4,12 +4,16 @@ module Network.BitTorrent.PeerWire.Block
4 , defaultBlockSize 4 , defaultBlockSize
5 , pieceIx, blockIx 5 , pieceIx, blockIx
6 , blockRange, ixRange, isPiece 6 , blockRange, ixRange, isPiece
7
8 , putInt, getInt
7 ) where 9 ) where
8 10
9import Control.Applicative 11import Control.Applicative
10import Data.ByteString (ByteString) 12import Data.ByteString (ByteString)
11import qualified Data.ByteString as B 13import qualified Data.ByteString as B
12import Data.Int 14import Data.Int
15import Data.Serialize
16
13 17
14type BlockLIx = Int 18type BlockLIx = Int
15type PieceLIx = Int 19type PieceLIx = Int
@@ -26,6 +30,24 @@ data BlockIx = BlockIx {
26 , ixLength :: {-# UNPACK #-} !Int 30 , ixLength :: {-# UNPACK #-} !Int
27 } deriving (Show, Eq) 31 } deriving (Show, Eq)
28 32
33getInt :: Get Int
34getInt = fromIntegral <$> getWord32be
35{-# INLINE getInt #-}
36
37putInt :: Putter Int
38putInt = putWord32be . fromIntegral
39{-# INLINE putInt #-}
40
41instance Serialize BlockIx where
42 {-# SPECIALIZE instance Serialize BlockIx #-}
43 get = BlockIx <$> getInt <*> getInt <*> getInt
44 {-# INLINE get #-}
45
46 put ix = do putInt (ixPiece ix)
47 putInt (ixOffset ix)
48 putInt (ixLength ix)
49 {-# INLINE put #-}
50
29 51
30data Block = Block { 52data Block = Block {
31 -- ^ Zero-based piece index. 53 -- ^ Zero-based piece index.