summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Exchange/Message.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-05 23:46:58 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-05 23:46:58 +0400
commitabec3de25b40d23627396d3eb98d1af547cf546c (patch)
treea8db4d54f8f4a33b213288ecadd2e0fcbc9f481a /src/Network/BitTorrent/Exchange/Message.hs
parent0362306da8401c8fc4d60fbb537c73afb42a250e (diff)
Lift the requires function to message class
Diffstat (limited to 'src/Network/BitTorrent/Exchange/Message.hs')
-rw-r--r--src/Network/BitTorrent/Exchange/Message.hs62
1 files changed, 46 insertions, 16 deletions
diff --git a/src/Network/BitTorrent/Exchange/Message.hs b/src/Network/BitTorrent/Exchange/Message.hs
index 8ef9f3da..eccffc77 100644
--- a/src/Network/BitTorrent/Exchange/Message.hs
+++ b/src/Network/BitTorrent/Exchange/Message.hs
@@ -48,7 +48,6 @@ module Network.BitTorrent.Exchange.Message
48 -- * Messages 48 -- * Messages
49 , Message (..) 49 , Message (..)
50 , PeerMessage (..) 50 , PeerMessage (..)
51 , requires
52 51
53 -- ** Core messages 52 -- ** Core messages
54 , StatusUpdate (..) 53 , StatusUpdate (..)
@@ -238,12 +237,20 @@ defaultHandshake = Handshake defaultBTProtocol def
238-- Regular messages 237-- Regular messages
239-----------------------------------------------------------------------} 238-----------------------------------------------------------------------}
240 239
240-- | Messages which can be sent after handshaking. Minimal complete
241-- definition: 'envelop'.
241class PeerMessage a where 242class PeerMessage a where
242 -- | Construct a message to be /sent/. 243 -- | Construct a message to be /sent/.
243 envelop :: ExtendedCaps -- ^ The /receiver/ extended capabilities; 244 envelop :: ExtendedCaps -- ^ The /receiver/ extended capabilities;
244 -> a -- ^ An regular message; 245 -> a -- ^ An regular message;
245 -> Message -- ^ Enveloped message to sent. 246 -> Message -- ^ Enveloped message to sent.
246 247
248 -- | Find out the extension this message belong to. Can be used to
249 -- check if this message is allowed to send\/recv in current
250 -- session.
251 requires :: a -> Maybe Extension
252 requires _ = Nothing
253
247{----------------------------------------------------------------------- 254{-----------------------------------------------------------------------
248-- Status messages 255-- Status messages
249-----------------------------------------------------------------------} 256-----------------------------------------------------------------------}
@@ -268,6 +275,7 @@ instance Pretty StatusUpdate where
268 275
269instance PeerMessage StatusUpdate where 276instance PeerMessage StatusUpdate where
270 envelop _ = Status 277 envelop _ = Status
278 {-# INLINE envelop #-}
271 279
272{----------------------------------------------------------------------- 280{-----------------------------------------------------------------------
273-- Available and transfer messages 281-- Available and transfer messages
@@ -314,15 +322,19 @@ instance Pretty RegularMessage where
314 322
315instance PeerMessage RegularMessage where 323instance PeerMessage RegularMessage where
316 envelop _ = Regular 324 envelop _ = Regular
325 {-# INLINE envelop #-}
317 326
318instance PeerMessage Bitfield where 327instance PeerMessage Bitfield where
319 envelop c = envelop c . Bitfield 328 envelop c = envelop c . Bitfield
329 {-# INLINE envelop #-}
320 330
321instance PeerMessage BlockIx where 331instance PeerMessage BlockIx where
322 envelop c = envelop c . Request 332 envelop c = envelop c . Request
333 {-# INLINE envelop #-}
323 334
324instance PeerMessage (Block BL.ByteString) where 335instance PeerMessage (Block BL.ByteString) where
325 envelop c = envelop c . Piece 336 envelop c = envelop c . Piece
337 {-# INLINE envelop #-}
326 338
327{----------------------------------------------------------------------- 339{-----------------------------------------------------------------------
328-- Fast messages 340-- Fast messages
@@ -360,7 +372,11 @@ instance Pretty FastMessage where
360 pretty (AllowedFast pix) = "Allowed fast" <+> int pix 372 pretty (AllowedFast pix) = "Allowed fast" <+> int pix
361 373
362instance PeerMessage FastMessage where 374instance PeerMessage FastMessage where
363 envelop _ = Fast 375 envelop _ = Fast
376 {-# INLINE envelop #-}
377
378 requires _ = Just ExtFast
379 {-# INLINE requires #-}
364 380
365{----------------------------------------------------------------------- 381{-----------------------------------------------------------------------
366-- Extended messages 382-- Extended messages
@@ -474,7 +490,11 @@ instance Pretty ExtendedHandshake where
474 pretty = PP.text . show 490 pretty = PP.text . show
475 491
476instance PeerMessage ExtendedHandshake where 492instance PeerMessage ExtendedHandshake where
477 envelop c = envelop c . EHandshake 493 envelop c = envelop c . EHandshake
494 {-# INLINE envelop #-}
495
496 requires _ = Just ExtExtended
497 {-# INLINE requires #-}
478 498
479nullExtendedHandshake :: ExtendedCaps -> ExtendedHandshake 499nullExtendedHandshake :: ExtendedCaps -> ExtendedHandshake
480nullExtendedHandshake caps 500nullExtendedHandshake caps
@@ -521,7 +541,11 @@ instance Pretty ExtendedMetadata where
521 pretty (MetadataUnknown bval ) = ppBEncode bval 541 pretty (MetadataUnknown bval ) = ppBEncode bval
522 542
523instance PeerMessage ExtendedMetadata where 543instance PeerMessage ExtendedMetadata where
524 envelop c = envelop c . EMetadata 544 envelop c = envelop c . EMetadata
545 {-# INLINE envelop #-}
546
547 requires _ = Just ExtExtended
548 {-# INLINE requires #-}
525 549
526-- | For more info see <http://www.bittorrent.org/beps/bep_0010.html> 550-- | For more info see <http://www.bittorrent.org/beps/bep_0010.html>
527data ExtendedMessage 551data ExtendedMessage
@@ -536,7 +560,11 @@ instance Pretty ExtendedMessage where
536 pretty (EUnknown mid _) = "Unknown" <+> PP.text (show mid) 560 pretty (EUnknown mid _) = "Unknown" <+> PP.text (show mid)
537 561
538instance PeerMessage ExtendedMessage where 562instance PeerMessage ExtendedMessage where
539 envelop _ = Extended 563 envelop _ = Extended
564 {-# INLINE envelop #-}
565
566 requires _ = Just ExtExtended
567 {-# INLINE requires #-}
540 568
541{----------------------------------------------------------------------- 569{-----------------------------------------------------------------------
542-- The message datatype 570-- The message datatype
@@ -579,19 +607,21 @@ instance Pretty Message where
579 607
580instance PeerMessage Message where 608instance PeerMessage Message where
581 envelop _ = id 609 envelop _ = id
610 {-# INLINE envelop #-}
611
612 requires KeepAlive = Nothing
613 requires (Status _) = Nothing
614 requires (Regular _) = Nothing
615 requires (Port _) = Just ExtDHT
616 requires (Fast _) = Just ExtFast
617 requires (Extended _) = Just ExtExtended
582 618
583instance PeerMessage PortNumber where 619instance PeerMessage PortNumber where
584 envelop _ = Port 620 envelop _ = Port
585 621 {-# INLINE envelop #-}
586-- | Can be used to check if this message is allowed to send\/recv in 622
587-- current session. 623 requires _ = Just ExtDHT
588requires :: Message -> Maybe Extension 624 {-# INLINE requires #-}
589requires KeepAlive = Nothing
590requires (Status _) = Nothing
591requires (Regular _) = Nothing
592requires (Port _) = Just ExtDHT
593requires (Fast _) = Just ExtFast
594requires (Extended _) = Just ExtExtended
595 625
596getInt :: S.Get Int 626getInt :: S.Get Int
597getInt = fromIntegral <$> S.getWord32be 627getInt = fromIntegral <$> S.getWord32be