summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent.hs4
-rw-r--r--src/Network/BitTorrent/DHT/Protocol.hs2
-rw-r--r--src/Network/BitTorrent/Exchange.hs3
-rw-r--r--src/Network/BitTorrent/Exchange/Protocol.hs152
-rw-r--r--src/Network/BitTorrent/Sessions.hs4
-rw-r--r--src/Network/BitTorrent/Sessions/Types.lhs6
-rw-r--r--src/Network/BitTorrent/Tracker.hs2
-rw-r--r--src/Network/BitTorrent/Tracker/HTTP.hs2
-rw-r--r--src/Network/BitTorrent/Tracker/Protocol.hs2
-rw-r--r--src/Network/BitTorrent/Tracker/UDP.hs2
10 files changed, 32 insertions, 147 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs
index e68d1597..7ff85b39 100644
--- a/src/Network/BitTorrent.hs
+++ b/src/Network/BitTorrent.hs
@@ -7,7 +7,7 @@
7-- 7--
8{-# LANGUAGE RecordWildCards #-} 8{-# LANGUAGE RecordWildCards #-}
9module Network.BitTorrent 9module Network.BitTorrent
10 ( module Data.Torrent 10 ( module Data.Torrent.Metainfo
11 11
12 , TorrentLoc(..), TorrentMap, Progress(..) 12 , TorrentLoc(..), TorrentMap, Progress(..)
13 , ThreadCount, SessionCount 13 , ThreadCount, SessionCount
@@ -49,7 +49,7 @@ import Text.PrettyPrint
49import System.Directory 49import System.Directory
50import System.FilePath 50import System.FilePath
51 51
52import Data.Torrent 52import Data.Torrent.Metainfo
53import Network.BitTorrent.Sessions.Types 53import Network.BitTorrent.Sessions.Types
54import Network.BitTorrent.Sessions 54import Network.BitTorrent.Sessions
55import Network.BitTorrent.Extension 55import Network.BitTorrent.Extension
diff --git a/src/Network/BitTorrent/DHT/Protocol.hs b/src/Network/BitTorrent/DHT/Protocol.hs
index b0100b70..73e5fa68 100644
--- a/src/Network/BitTorrent/DHT/Protocol.hs
+++ b/src/Network/BitTorrent/DHT/Protocol.hs
@@ -32,7 +32,7 @@ import System.Entropy
32import Remote.KRPC 32import Remote.KRPC
33import Remote.KRPC.Protocol 33import Remote.KRPC.Protocol
34import Data.BEncode 34import Data.BEncode
35import Data.Torrent 35import Data.Torrent.Metainfo
36import Network.BitTorrent.Peer 36import Network.BitTorrent.Peer
37import Network.BitTorrent.Exchange.Protocol () 37import Network.BitTorrent.Exchange.Protocol ()
38 38
diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs
index 0f1d2833..e81880b4 100644
--- a/src/Network/BitTorrent/Exchange.hs
+++ b/src/Network/BitTorrent/Exchange.hs
@@ -91,7 +91,8 @@ import Text.PrettyPrint as PP hiding (($$))
91 91
92import Network 92import Network
93 93
94import Data.Bitfield as BF 94import Data.Torrent.Block
95import Data.Torrent.Bitfield as BF
95import Network.BitTorrent.Extension 96import Network.BitTorrent.Extension
96import Network.BitTorrent.Exchange.Protocol 97import Network.BitTorrent.Exchange.Protocol
97import Network.BitTorrent.Sessions.Types 98import Network.BitTorrent.Sessions.Types
diff --git a/src/Network/BitTorrent/Exchange/Protocol.hs b/src/Network/BitTorrent/Exchange/Protocol.hs
index 00b6795b..3b2472da 100644
--- a/src/Network/BitTorrent/Exchange/Protocol.hs
+++ b/src/Network/BitTorrent/Exchange/Protocol.hs
@@ -37,16 +37,6 @@ module Network.BitTorrent.Exchange.Protocol
37 , defaultHandshake, defaultBTProtocol, defaultReserved 37 , defaultHandshake, defaultBTProtocol, defaultReserved
38 , handshakeMaxSize 38 , handshakeMaxSize
39 39
40 -- * Block
41 , PieceIx, BlockLIx, PieceLIx
42 , BlockIx(..), ppBlockIx
43 , Block(..), ppBlock ,blockSize
44 , pieceIx, blockIx
45 , blockRange, ixRange, isPiece
46
47 -- ** Defaults
48 , defaultBlockSize
49
50 -- * Regular messages 40 -- * Regular messages
51 , Message(..) 41 , Message(..)
52 , ppMessage 42 , ppMessage
@@ -89,12 +79,28 @@ import Text.PrettyPrint
89import Network 79import Network
90import Network.Socket.ByteString 80import Network.Socket.ByteString
91 81
92import Data.Bitfield 82import Data.Torrent.Bitfield
93import Data.Torrent 83import Data.Torrent.Block
84import Data.Torrent.Metainfo
94import Network.BitTorrent.Extension 85import Network.BitTorrent.Extension
95import Network.BitTorrent.Peer 86import Network.BitTorrent.Peer
96 87
97 88
89getInt :: S.Get Int
90getInt = fromIntegral <$> S.getWord32be
91{-# INLINE getInt #-}
92
93putInt :: S.Putter Int
94putInt = S.putWord32be . fromIntegral
95{-# INLINE putInt #-}
96
97getIntB :: B.Get Int
98getIntB = fromIntegral <$> B.getWord32be
99{-# INLINE getIntB #-}
100
101putIntB :: Int -> B.Put
102putIntB = B.putWord32be . fromIntegral
103{-# INLINE putIntB #-}
98 104
99{----------------------------------------------------------------------- 105{-----------------------------------------------------------------------
100 Handshake 106 Handshake
@@ -196,128 +202,6 @@ handshake sock hs = do
196 return hs' 202 return hs'
197 203
198{----------------------------------------------------------------------- 204{-----------------------------------------------------------------------
199 Block Index
200-----------------------------------------------------------------------}
201
202type BlockLIx = Int
203type PieceLIx = Int
204
205
206data BlockIx = BlockIx {
207 -- | Zero-based piece index.
208 ixPiece :: {-# UNPACK #-} !PieceLIx
209
210 -- | Zero-based byte offset within the piece.
211 , ixOffset :: {-# UNPACK #-} !Int
212
213 -- | Block size starting from offset.
214 , ixLength :: {-# UNPACK #-} !Int
215 } deriving (Show, Eq)
216
217$(deriveJSON (L.map toLower . L.dropWhile isLower) ''BlockIx)
218
219getInt :: S.Get Int
220getInt = fromIntegral <$> S.getWord32be
221{-# INLINE getInt #-}
222
223putInt :: S.Putter Int
224putInt = S.putWord32be . fromIntegral
225{-# INLINE putInt #-}
226
227getIntB :: B.Get Int
228getIntB = fromIntegral <$> B.getWord32be
229{-# INLINE getIntB #-}
230
231putIntB :: Int -> B.Put
232putIntB = B.putWord32be . fromIntegral
233{-# INLINE putIntB #-}
234
235instance Serialize BlockIx where
236 {-# SPECIALIZE instance Serialize BlockIx #-}
237 get = BlockIx <$> getInt <*> getInt <*> getInt
238 {-# INLINE get #-}
239
240 put i = do putInt (ixPiece i)
241 putInt (ixOffset i)
242 putInt (ixLength i)
243 {-# INLINE put #-}
244
245instance Binary BlockIx where
246 {-# SPECIALIZE instance Binary BlockIx #-}
247 get = BlockIx <$> getIntB <*> getIntB <*> getIntB
248 {-# INLINE get #-}
249
250 put BlockIx {..} = do
251 putIntB ixPiece
252 putIntB ixOffset
253 putIntB ixLength
254
255-- | Format block index in human readable form.
256ppBlockIx :: BlockIx -> Doc
257ppBlockIx BlockIx {..} =
258 "piece = " <> int ixPiece <> "," <+>
259 "offset = " <> int ixOffset <> "," <+>
260 "length = " <> int ixLength
261
262{-----------------------------------------------------------------------
263 Block
264-----------------------------------------------------------------------}
265
266data Block = Block {
267 -- | Zero-based piece index.
268 blkPiece :: {-# UNPACK #-} !PieceLIx
269
270 -- | Zero-based byte offset within the piece.
271 , blkOffset :: {-# UNPACK #-} !Int
272
273 -- | Payload.
274 , blkData :: !Lazy.ByteString
275 } deriving (Show, Eq)
276
277-- | Format block in human readable form. Payload is ommitted.
278ppBlock :: Block -> Doc
279ppBlock = ppBlockIx . blockIx
280
281blockSize :: Block -> Int
282blockSize blk = fromIntegral (Lazy.length (blkData blk))
283{-# INLINE blockSize #-}
284
285-- | Widely used semi-official block size.
286defaultBlockSize :: Int
287defaultBlockSize = 16 * 1024
288
289
290isPiece :: Int -> Block -> Bool
291isPiece pieceSize (Block i offset bs) =
292 offset == 0
293 && fromIntegral (Lazy.length bs) == pieceSize
294 && i >= 0
295{-# INLINE isPiece #-}
296
297pieceIx :: Int -> Int -> BlockIx
298pieceIx i = BlockIx i 0
299{-# INLINE pieceIx #-}
300
301blockIx :: Block -> BlockIx
302blockIx = BlockIx <$> blkPiece <*> blkOffset <*> blockSize
303
304blockRange :: (Num a, Integral a) => Int -> Block -> (a, a)
305blockRange pieceSize blk = (offset, offset + len)
306 where
307 offset = fromIntegral pieceSize * fromIntegral (blkPiece blk)
308 + fromIntegral (blkOffset blk)
309 len = fromIntegral (Lazy.length (blkData blk))
310{-# INLINE blockRange #-}
311
312ixRange :: (Num a, Integral a) => Int -> BlockIx -> (a, a)
313ixRange pieceSize i = (offset, offset + len)
314 where
315 offset = fromIntegral pieceSize * fromIntegral (ixPiece i)
316 + fromIntegral (ixOffset i)
317 len = fromIntegral (ixLength i)
318{-# INLINE ixRange #-}
319
320{-----------------------------------------------------------------------
321 Regular messages 205 Regular messages
322-----------------------------------------------------------------------} 206-----------------------------------------------------------------------}
323 207
diff --git a/src/Network/BitTorrent/Sessions.hs b/src/Network/BitTorrent/Sessions.hs
index 1d0d21b4..ba0c60a1 100644
--- a/src/Network/BitTorrent/Sessions.hs
+++ b/src/Network/BitTorrent/Sessions.hs
@@ -73,8 +73,8 @@ import Network hiding (accept)
73import Network.BSD 73import Network.BSD
74import Network.Socket 74import Network.Socket
75 75
76import Data.Bitfield as BF 76import Data.Torrent.Bitfield as BF
77import Data.Torrent 77import Data.Torrent.Metainfo
78import Network.BitTorrent.Extension 78import Network.BitTorrent.Extension
79import Network.BitTorrent.Peer 79import Network.BitTorrent.Peer
80import Network.BitTorrent.Sessions.Types 80import Network.BitTorrent.Sessions.Types
diff --git a/src/Network/BitTorrent/Sessions/Types.lhs b/src/Network/BitTorrent/Sessions/Types.lhs
index 5571e23a..e62e362f 100644
--- a/src/Network/BitTorrent/Sessions/Types.lhs
+++ b/src/Network/BitTorrent/Sessions/Types.lhs
@@ -59,8 +59,8 @@
59 59
60> import Network 60> import Network
61 61
62> import Data.Bitfield as BF 62> import Data.Torrent.Bitfield as BF
63> import Data.Torrent 63> import Data.Torrent.Metainfo
64> import Network.BitTorrent.Extension 64> import Network.BitTorrent.Extension
65> import Network.BitTorrent.Peer 65> import Network.BitTorrent.Peer
66> import Network.BitTorrent.Exchange.Protocol as BT 66> import Network.BitTorrent.Exchange.Protocol as BT
@@ -248,7 +248,7 @@ fresh required extensions.
248Normally, you would have one client session, however, if we needed, in 248Normally, you would have one client session, however, if we needed, in
249one application we could have many clients with different peer ID's 249one application we could have many clients with different peer ID's
250and different enabled extensions at the same time. 250and different enabled extensions at the same time.
251 251
252> -- | 252> -- |
253> data ClientSession = ClientSession { 253> data ClientSession = ClientSession {
254> -- | Used in handshakes and discovery mechanism. 254> -- | Used in handshakes and discovery mechanism.
diff --git a/src/Network/BitTorrent/Tracker.hs b/src/Network/BitTorrent/Tracker.hs
index e1e6ea71..e98f1e94 100644
--- a/src/Network/BitTorrent/Tracker.hs
+++ b/src/Network/BitTorrent/Tracker.hs
@@ -39,7 +39,7 @@ import Data.IORef
39import Network 39import Network
40import Network.URI 40import Network.URI
41 41
42import Data.Torrent 42import Data.Torrent.Metainfo
43import Network.BitTorrent.Peer 43import Network.BitTorrent.Peer
44import Network.BitTorrent.Sessions.Types 44import Network.BitTorrent.Sessions.Types
45import Network.BitTorrent.Tracker.Protocol 45import Network.BitTorrent.Tracker.Protocol
diff --git a/src/Network/BitTorrent/Tracker/HTTP.hs b/src/Network/BitTorrent/Tracker/HTTP.hs
index 0ada154b..f781b847 100644
--- a/src/Network/BitTorrent/Tracker/HTTP.hs
+++ b/src/Network/BitTorrent/Tracker/HTTP.hs
@@ -30,7 +30,7 @@ import Data.URLEncoded as URL
30import Network.URI 30import Network.URI
31import Network.HTTP 31import Network.HTTP
32 32
33import Data.Torrent 33import Data.Torrent.Metainfo
34import Network.BitTorrent.Tracker.Protocol 34import Network.BitTorrent.Tracker.Protocol
35 35
36{----------------------------------------------------------------------- 36{-----------------------------------------------------------------------
diff --git a/src/Network/BitTorrent/Tracker/Protocol.hs b/src/Network/BitTorrent/Tracker/Protocol.hs
index ee395883..c468656f 100644
--- a/src/Network/BitTorrent/Tracker/Protocol.hs
+++ b/src/Network/BitTorrent/Tracker/Protocol.hs
@@ -44,7 +44,7 @@ import Data.Text (Text)
44import Data.Text.Encoding 44import Data.Text.Encoding
45import Data.Serialize hiding (Result) 45import Data.Serialize hiding (Result)
46import Data.URLEncoded as URL 46import Data.URLEncoded as URL
47import Data.Torrent 47import Data.Torrent.Metainfo
48 48
49import Network 49import Network
50import Network.Socket 50import Network.Socket
diff --git a/src/Network/BitTorrent/Tracker/UDP.hs b/src/Network/BitTorrent/Tracker/UDP.hs
index 43de7663..13e1298b 100644
--- a/src/Network/BitTorrent/Tracker/UDP.hs
+++ b/src/Network/BitTorrent/Tracker/UDP.hs
@@ -24,7 +24,7 @@ import Data.Text.Encoding
24import Network.Socket hiding (Connected) 24import Network.Socket hiding (Connected)
25import Network.Socket.ByteString as BS 25import Network.Socket.ByteString as BS
26 26
27import Data.Torrent () 27import Data.Torrent.Metainfo ()
28import Network.BitTorrent.Tracker.Protocol 28import Network.BitTorrent.Tracker.Protocol
29 29
30 30