diff options
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/Message.hs')
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Message.hs | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/Network/BitTorrent/PeerWire/Message.hs b/src/Network/BitTorrent/PeerWire/Message.hs index 6515fdf2..3895ed5f 100644 --- a/src/Network/BitTorrent/PeerWire/Message.hs +++ b/src/Network/BitTorrent/PeerWire/Message.hs | |||
@@ -7,6 +7,7 @@ module Network.BitTorrent.PeerWire.Message | |||
7 | 7 | ||
8 | import Control.Applicative | 8 | import Control.Applicative |
9 | import qualified Data.ByteString as B | 9 | import qualified Data.ByteString as B |
10 | import qualified Data.ByteString.Lazy as Lazy | ||
10 | import Data.Serialize | 11 | import Data.Serialize |
11 | import Text.PrettyPrint | 12 | import Text.PrettyPrint |
12 | import Network | 13 | import Network |
@@ -91,7 +92,7 @@ instance Serialize Message where | |||
91 | 0x02 -> return Interested | 92 | 0x02 -> return Interested |
92 | 0x03 -> return NotInterested | 93 | 0x03 -> return NotInterested |
93 | 0x04 -> Have <$> getInt | 94 | 0x04 -> Have <$> getInt |
94 | 0x05 -> Bitfield <$> getBitfield (pred len) | 95 | 0x05 -> (Bitfield . fromBitmap) <$> getByteString (pred len) |
95 | 0x06 -> Request <$> get | 96 | 0x06 -> Request <$> get |
96 | 0x07 -> Piece <$> getBlock (len - 9) | 97 | 0x07 -> Piece <$> getBlock (len - 9) |
97 | 0x08 -> Cancel <$> get | 98 | 0x08 -> Cancel <$> get |
@@ -118,8 +119,9 @@ instance Serialize Message where | |||
118 | put Interested = putInt 1 >> putWord8 0x02 | 119 | put Interested = putInt 1 >> putWord8 0x02 |
119 | put NotInterested = putInt 1 >> putWord8 0x03 | 120 | put NotInterested = putInt 1 >> putWord8 0x03 |
120 | put (Have i) = putInt 5 >> putWord8 0x04 >> putInt i | 121 | put (Have i) = putInt 5 >> putWord8 0x04 >> putInt i |
121 | put (Bitfield b) = putInt l >> putWord8 0x05 >> putBitfield b | 122 | put (Bitfield bf) = putInt l >> putWord8 0x05 >> putLazyByteString b |
122 | where l = succ (bitfieldByteCount b) | 123 | where b = toBitmap bf |
124 | l = succ (fromIntegral (Lazy.length b)) | ||
123 | {-# INLINE l #-} | 125 | {-# INLINE l #-} |
124 | put (Request blk) = putInt 13 >> putWord8 0x06 >> put blk | 126 | put (Request blk) = putInt 13 >> putWord8 0x06 >> put blk |
125 | put (Piece blk) = putInt l >> putWord8 0x07 >> putBlock | 127 | put (Piece blk) = putInt l >> putWord8 0x07 >> putBlock |