summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/PeerWire
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-07 23:27:31 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-07 23:27:31 +0400
commita30bb766e8f2bea19e5a8f1739354d5f7894df1d (patch)
tree5209629a266b6cc007f1e3d24469b70a0a4c6960 /src/Network/BitTorrent/PeerWire
parentec063c9e50aa6f19e82e836a33c10d596f766290 (diff)
~ Fix bitfield encoding.
Diffstat (limited to 'src/Network/BitTorrent/PeerWire')
-rw-r--r--src/Network/BitTorrent/PeerWire/Message.hs8
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
8import Control.Applicative 8import Control.Applicative
9import qualified Data.ByteString as B 9import qualified Data.ByteString as B
10import qualified Data.ByteString.Lazy as Lazy
10import Data.Serialize 11import Data.Serialize
11import Text.PrettyPrint 12import Text.PrettyPrint
12import Network 13import 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