diff options
author | Joe Crayne <joe@jerkface.net> | 2019-12-20 23:11:48 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 23:27:24 -0500 |
commit | f5c9b738e489bced6189b85124952918414d8c8b (patch) | |
tree | 0da3c160657b532369b033e07bc19d80a2490516 | |
parent | 6863cc64e606dd753d57aa1a124584bcb4416de4 (diff) |
Ressurect Truzjan's Tracker modules.
-rw-r--r-- | dht/src/Network/BitTorrent/Internal/Progress.hs (renamed from dht/bittorrent/src/Network/BitTorrent/Internal/Progress.hs) | 4 | ||||
-rw-r--r-- | dht/src/Network/BitTorrent/Tracker/Message.hs (renamed from dht/bittorrent/src/Network/BitTorrent/Tracker/Message.hs) | 52 |
2 files changed, 32 insertions, 24 deletions
diff --git a/dht/bittorrent/src/Network/BitTorrent/Internal/Progress.hs b/dht/src/Network/BitTorrent/Internal/Progress.hs index 6ac889e2..251bbca4 100644 --- a/dht/bittorrent/src/Network/BitTorrent/Internal/Progress.hs +++ b/dht/src/Network/BitTorrent/Internal/Progress.hs | |||
@@ -77,6 +77,10 @@ instance Default Progress where | |||
77 | def = Progress 0 0 0 | 77 | def = Progress 0 0 0 |
78 | {-# INLINE def #-} | 78 | {-# INLINE def #-} |
79 | 79 | ||
80 | instance Semigroup Progress where | ||
81 | (<>) = mappend | ||
82 | {-# INLINE (<>) #-} | ||
83 | |||
80 | -- | Can be used to aggregate total progress. | 84 | -- | Can be used to aggregate total progress. |
81 | instance Monoid Progress where | 85 | instance Monoid Progress where |
82 | mempty = def | 86 | mempty = def |
diff --git a/dht/bittorrent/src/Network/BitTorrent/Tracker/Message.hs b/dht/src/Network/BitTorrent/Tracker/Message.hs index ab492275..e9d12006 100644 --- a/dht/bittorrent/src/Network/BitTorrent/Tracker/Message.hs +++ b/dht/src/Network/BitTorrent/Tracker/Message.hs | |||
@@ -28,6 +28,7 @@ | |||
28 | {-# LANGUAGE ScopedTypeVariables #-} | 28 | {-# LANGUAGE ScopedTypeVariables #-} |
29 | {-# LANGUAGE TypeFamilies #-} | 29 | {-# LANGUAGE TypeFamilies #-} |
30 | {-# LANGUAGE CPP #-} | 30 | {-# LANGUAGE CPP #-} |
31 | {-# LANGUAGE PatternSynonyms #-} | ||
31 | {-# OPTIONS -fno-warn-orphans #-} | 32 | {-# OPTIONS -fno-warn-orphans #-} |
32 | module Network.BitTorrent.Tracker.Message | 33 | module Network.BitTorrent.Tracker.Message |
33 | ( -- * Announce | 34 | ( -- * Announce |
@@ -94,6 +95,8 @@ module Network.BitTorrent.Tracker.Message | |||
94 | , genTransactionId | 95 | , genTransactionId |
95 | , TransactionId | 96 | , TransactionId |
96 | , Transaction (..) | 97 | , Transaction (..) |
98 | |||
99 | , MessageId(ConnectId,AnnounceId,ScrapeId,ErrorId) | ||
97 | ) | 100 | ) |
98 | where | 101 | where |
99 | 102 | ||
@@ -257,7 +260,7 @@ instance Serialize AnnounceQuery where | |||
257 | , reqPort = port | 260 | , reqPort = port |
258 | , reqProgress = progress | 261 | , reqProgress = progress |
259 | , reqIP = if ip == 0 then Nothing else Just ip | 262 | , reqIP = if ip == 0 then Nothing else Just ip |
260 | , reqNumWant = if want == -1 then Nothing | 263 | , reqNumWant = if want == maxBound then Nothing |
261 | else Just (fromIntegral want) | 264 | else Just (fromIntegral want) |
262 | , reqEvent = ev | 265 | , reqEvent = ev |
263 | } | 266 | } |
@@ -515,15 +518,13 @@ instance BEncode AnnounceInfo where | |||
515 | merge :: PeerList IPv4 -> Maybe (PeerList IPv6) -> BE.Get (PeerList IP) | 518 | merge :: PeerList IPv4 -> Maybe (PeerList IPv6) -> BE.Get (PeerList IP) |
516 | merge (PeerList ips) Nothing = pure (PeerList ips) | 519 | merge (PeerList ips) Nothing = pure (PeerList ips) |
517 | merge (PeerList _ ) (Just _) | 520 | merge (PeerList _ ) (Just _) |
518 | = fail "PeerList: non-compact peer list provided, \ | 521 | = fail "PeerList: non-compact peer list provided, but the `peers6' field present" |
519 | \but the `peers6' field present" | ||
520 | 522 | ||
521 | merge (CompactPeerList ipv4s) Nothing | 523 | merge (CompactPeerList ipv4s) Nothing |
522 | = pure $ CompactPeerList ipv4s | 524 | = pure $ CompactPeerList ipv4s |
523 | 525 | ||
524 | merge (CompactPeerList _ ) (Just (PeerList _)) | 526 | merge (CompactPeerList _ ) (Just (PeerList _)) |
525 | = fail "PeerList: the `peers6' field value \ | 527 | = fail "PeerList: the `peers6' field value should contain *compact* peer list" |
526 | \should contain *compact* peer list" | ||
527 | 528 | ||
528 | merge (CompactPeerList ipv4s) (Just (CompactPeerList ipv6s)) | 529 | merge (CompactPeerList ipv4s) (Just (CompactPeerList ipv6s)) |
529 | = pure $ CompactPeerList $ | 530 | = pure $ CompactPeerList $ |
@@ -846,29 +847,32 @@ data instance Transaction Response = TransactionR | |||
846 | newtype MessageId = MessageId Word32 | 847 | newtype MessageId = MessageId Word32 |
847 | deriving (Show, Eq, Num, Serialize) | 848 | deriving (Show, Eq, Num, Serialize) |
848 | 849 | ||
849 | connectId, announceId, scrapeId, errorId :: MessageId | 850 | pattern ConnectId :: MessageId |
850 | connectId = 0 | 851 | pattern ConnectId = MessageId 0 |
851 | announceId = 1 | 852 | pattern AnnounceId :: MessageId |
852 | scrapeId = 2 | 853 | pattern AnnounceId = MessageId 1 |
853 | errorId = 3 | 854 | pattern ScrapeId :: MessageId |
855 | pattern ScrapeId = MessageId 2 | ||
856 | pattern ErrorId :: MessageId | ||
857 | pattern ErrorId = MessageId 3 | ||
854 | 858 | ||
855 | instance Serialize (Transaction Request) where | 859 | instance Serialize (Transaction Request) where |
856 | put TransactionQ {..} = do | 860 | put TransactionQ {..} = do |
857 | case request of | 861 | case request of |
858 | Connect -> do | 862 | Connect -> do |
859 | put initialConnectionId | 863 | put initialConnectionId |
860 | put connectId | 864 | put ConnectId |
861 | put transIdQ | 865 | put transIdQ |
862 | 866 | ||
863 | Announce ann -> do | 867 | Announce ann -> do |
864 | put connIdQ | 868 | put connIdQ |
865 | put announceId | 869 | put AnnounceId |
866 | put transIdQ | 870 | put transIdQ |
867 | put ann | 871 | put ann |
868 | 872 | ||
869 | Scrape hashes -> do | 873 | Scrape hashes -> do |
870 | put connIdQ | 874 | put connIdQ |
871 | put scrapeId | 875 | put ScrapeId |
872 | put transIdQ | 876 | put transIdQ |
873 | forM_ hashes put | 877 | forM_ hashes put |
874 | 878 | ||
@@ -879,9 +883,9 @@ instance Serialize (Transaction Request) where | |||
879 | where | 883 | where |
880 | getBody :: MessageId -> S.Get Request | 884 | getBody :: MessageId -> S.Get Request |
881 | getBody msgId | 885 | getBody msgId |
882 | | msgId == connectId = pure Connect | 886 | | msgId == ConnectId = pure Connect |
883 | | msgId == announceId = Announce <$> get | 887 | | msgId == AnnounceId = Announce <$> get |
884 | | msgId == scrapeId = Scrape <$> many get | 888 | | msgId == ScrapeId = Scrape <$> many get |
885 | | otherwise = fail errMsg | 889 | | otherwise = fail errMsg |
886 | where | 890 | where |
887 | errMsg = "unknown request: " ++ show msgId | 891 | errMsg = "unknown request: " ++ show msgId |
@@ -890,22 +894,22 @@ instance Serialize (Transaction Response) where | |||
890 | put TransactionR {..} = do | 894 | put TransactionR {..} = do |
891 | case response of | 895 | case response of |
892 | Connected conn -> do | 896 | Connected conn -> do |
893 | put connectId | 897 | put ConnectId |
894 | put transIdR | 898 | put transIdR |
895 | put conn | 899 | put conn |
896 | 900 | ||
897 | Announced info -> do | 901 | Announced info -> do |
898 | put announceId | 902 | put AnnounceId |
899 | put transIdR | 903 | put transIdR |
900 | put info | 904 | put info |
901 | 905 | ||
902 | Scraped infos -> do | 906 | Scraped infos -> do |
903 | put scrapeId | 907 | put ScrapeId |
904 | put transIdR | 908 | put transIdR |
905 | forM_ infos put | 909 | forM_ infos put |
906 | 910 | ||
907 | Failed info -> do | 911 | Failed info -> do |
908 | put errorId | 912 | put ErrorId |
909 | put transIdR | 913 | put transIdR |
910 | put (encodeUtf8 info) | 914 | put (encodeUtf8 info) |
911 | 915 | ||
@@ -916,10 +920,10 @@ instance Serialize (Transaction Response) where | |||
916 | where | 920 | where |
917 | getBody :: MessageId -> S.Get Response | 921 | getBody :: MessageId -> S.Get Response |
918 | getBody msgId | 922 | getBody msgId |
919 | | msgId == connectId = Connected <$> get | 923 | | msgId == ConnectId = Connected <$> get |
920 | | msgId == announceId = Announced <$> get | 924 | | msgId == AnnounceId = Announced <$> get |
921 | | msgId == scrapeId = Scraped <$> many get | 925 | | msgId == ScrapeId = Scraped <$> many get |
922 | | msgId == errorId = (Failed . decodeUtf8) <$> get | 926 | | msgId == ErrorId = (Failed . decodeUtf8) <$> get |
923 | | otherwise = fail msg | 927 | | otherwise = fail msg |
924 | where | 928 | where |
925 | msg = "unknown response: " ++ show msgId | 929 | msg = "unknown response: " ++ show msgId |