summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Exchange.hs12
-rw-r--r--src/Network/BitTorrent/Exchange/Protocol.hs2
-rw-r--r--src/Network/BitTorrent/Internal.hs9
3 files changed, 15 insertions, 8 deletions
diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs
index 505360a4..66112f14 100644
--- a/src/Network/BitTorrent/Exchange.hs
+++ b/src/Network/BitTorrent/Exchange.hs
@@ -53,6 +53,7 @@ module Network.BitTorrent.Exchange
53 , getHaveCount 53 , getHaveCount
54 , getWantCount 54 , getWantCount
55 , getPieceCount 55 , getPieceCount
56 , peerOffer
56 57
57 -- * Events 58 -- * Events
58 , Event(..) 59 , Event(..)
@@ -295,6 +296,10 @@ data Event
295 | Fragment Block 296 | Fragment Block
296 deriving Show 297 deriving Show
297 298
299-- INVARIANT:
300--
301-- * Available Bitfield is never empty
302--
298 303
299-- | You could think of 'awaitEvent' as wait until something interesting occur. 304-- | You could think of 'awaitEvent' as wait until something interesting occur.
300-- 305--
@@ -316,9 +321,7 @@ data Event
316-- forall (Fragment block). isPiece block == True 321-- forall (Fragment block). isPiece block == True
317-- 322--
318awaitEvent :: P2P Event 323awaitEvent :: P2P Event
319awaitEvent = do 324awaitEvent = awaitMessage >>= go
320
321 awaitMessage >>= go
322 where 325 where
323 go KeepAlive = awaitEvent 326 go KeepAlive = awaitEvent
324 go Choke = do 327 go Choke = do
@@ -341,8 +344,7 @@ awaitEvent = do
341 awaitEvent 344 awaitEvent
342 345
343 go (Have idx) = do 346 go (Have idx) = do
344 new <- singletonBF idx 347 bitfield %= have idx
345 bitfield %= BF.union new
346 _ <- revise 348 _ <- revise
347 349
348 offer <- peerOffer 350 offer <- peerOffer
diff --git a/src/Network/BitTorrent/Exchange/Protocol.hs b/src/Network/BitTorrent/Exchange/Protocol.hs
index 8d42e3a8..4cf4685d 100644
--- a/src/Network/BitTorrent/Exchange/Protocol.hs
+++ b/src/Network/BitTorrent/Exchange/Protocol.hs
@@ -237,7 +237,7 @@ data Block = Block {
237 , blkOffset :: {-# UNPACK #-} !Int 237 , blkOffset :: {-# UNPACK #-} !Int
238 238
239 -- | Payload. 239 -- | Payload.
240 , blkData :: !ByteString 240 , blkData :: !ByteString -- TODO make lazy bytestring
241 } deriving (Show, Eq) 241 } deriving (Show, Eq)
242 242
243-- | Format block in human readable form. Payload is ommitted. 243-- | Format block in human readable form. Payload is ommitted.
diff --git a/src/Network/BitTorrent/Internal.hs b/src/Network/BitTorrent/Internal.hs
index 38388b9a..bf47b87b 100644
--- a/src/Network/BitTorrent/Internal.hs
+++ b/src/Network/BitTorrent/Internal.hs
@@ -48,7 +48,7 @@ module Network.BitTorrent.Internal
48 , leaveSwarm 48 , leaveSwarm
49 , waitVacancy 49 , waitVacancy
50 50
51 , available 51 , pieceLength
52 52
53 -- * Peer 53 -- * Peer
54 , PeerSession( PeerSession, connectedPeerAddr 54 , PeerSession( PeerSession, connectedPeerAddr
@@ -57,6 +57,9 @@ module Network.BitTorrent.Internal
57 ) 57 )
58 , SessionState 58 , SessionState
59 , withPeerSession 59 , withPeerSession
60
61 -- ** Broadcasting
62 , available
60 , getPending 63 , getPending
61 64
62 -- ** Exceptions 65 -- ** Exceptions
@@ -388,6 +391,7 @@ waitVacancy se =
388 391
389pieceLength :: SwarmSession -> Int 392pieceLength :: SwarmSession -> Int
390pieceLength = ciPieceLength . tInfo . torrentMeta 393pieceLength = ciPieceLength . tInfo . torrentMeta
394{-# INLINE pieceLength #-}
391 395
392{----------------------------------------------------------------------- 396{-----------------------------------------------------------------------
393 Peer session 397 Peer session
@@ -532,7 +536,8 @@ findPieceCount = pieceCount . tInfo . torrentMeta . swarmSession
532-- 3. Signal to the all other peer about this. 536-- 3. Signal to the all other peer about this.
533 537
534available :: Bitfield -> SwarmSession -> IO () 538available :: Bitfield -> SwarmSession -> IO ()
535available bf se @ SwarmSession {..} = mark >> atomically broadcast 539available bf se @ SwarmSession {..} = do
540 mark >> atomically broadcast
536 where 541 where
537 mark = do 542 mark = do
538 let bytes = pieceLength se * BF.haveCount bf 543 let bytes = pieceLength se * BF.haveCount bf