diff options
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/Bitfield.hs')
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Bitfield.hs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/PeerWire/Bitfield.hs b/src/Network/BitTorrent/PeerWire/Bitfield.hs new file mode 100644 index 00000000..c9357a25 --- /dev/null +++ b/src/Network/BitTorrent/PeerWire/Bitfield.hs | |||
@@ -0,0 +1,39 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam T. 2013 | ||
3 | -- License : MIT | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : experimental | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | -- | ||
9 | -- This module provides Bitfield datatype used to represent sets of | ||
10 | -- piece indexes any peer have. All associated operations should be | ||
11 | -- defined here as well. | ||
12 | -- | ||
13 | module Network.BitTorrent.PeerWire.Bitfield | ||
14 | ( Bitfield(..) | ||
15 | , getBitfield, putBitfield, bitfieldByteCount | ||
16 | ) where | ||
17 | |||
18 | import Control.Applicative | ||
19 | import Data.ByteString (ByteString) | ||
20 | import qualified Data.ByteString as B | ||
21 | |||
22 | import Data.Serialize | ||
23 | |||
24 | |||
25 | newtype Bitfield = MkBitfield { | ||
26 | bfBits :: ByteString | ||
27 | } deriving (Show, Eq, Ord) | ||
28 | |||
29 | bitfieldByteCount :: Bitfield -> Int | ||
30 | bitfieldByteCount = B.length . bfBits | ||
31 | {-# INLINE bitfieldByteCount #-} | ||
32 | |||
33 | getBitfield :: Int -> Get Bitfield | ||
34 | getBitfield n = MkBitfield <$> getBytes n | ||
35 | {-# INLINE getBitfield #-} | ||
36 | |||
37 | putBitfield :: Bitfield -> Put | ||
38 | putBitfield = putByteString . bfBits | ||
39 | {-# INLINE putBitfield #-} \ No newline at end of file | ||