diff options
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r-- | src/Network/BitTorrent/DHT/Protocol.hs | 2 | ||||
-rw-r--r-- | src/Network/BitTorrent/Exchange.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/Exchange/Protocol.hs | 152 | ||||
-rw-r--r-- | src/Network/BitTorrent/Sessions.hs | 4 | ||||
-rw-r--r-- | src/Network/BitTorrent/Sessions/Types.lhs | 6 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker.hs | 2 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/HTTP.hs | 2 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/Protocol.hs | 2 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/UDP.hs | 2 |
9 files changed, 30 insertions, 145 deletions
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 | |||
32 | import Remote.KRPC | 32 | import Remote.KRPC |
33 | import Remote.KRPC.Protocol | 33 | import Remote.KRPC.Protocol |
34 | import Data.BEncode | 34 | import Data.BEncode |
35 | import Data.Torrent | 35 | import Data.Torrent.Metainfo |
36 | import Network.BitTorrent.Peer | 36 | import Network.BitTorrent.Peer |
37 | import Network.BitTorrent.Exchange.Protocol () | 37 | import 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 | ||
92 | import Network | 92 | import Network |
93 | 93 | ||
94 | import Data.Bitfield as BF | 94 | import Data.Torrent.Block |
95 | import Data.Torrent.Bitfield as BF | ||
95 | import Network.BitTorrent.Extension | 96 | import Network.BitTorrent.Extension |
96 | import Network.BitTorrent.Exchange.Protocol | 97 | import Network.BitTorrent.Exchange.Protocol |
97 | import Network.BitTorrent.Sessions.Types | 98 | import 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 | |||
89 | import Network | 79 | import Network |
90 | import Network.Socket.ByteString | 80 | import Network.Socket.ByteString |
91 | 81 | ||
92 | import Data.Bitfield | 82 | import Data.Torrent.Bitfield |
93 | import Data.Torrent | 83 | import Data.Torrent.Block |
84 | import Data.Torrent.Metainfo | ||
94 | import Network.BitTorrent.Extension | 85 | import Network.BitTorrent.Extension |
95 | import Network.BitTorrent.Peer | 86 | import Network.BitTorrent.Peer |
96 | 87 | ||
97 | 88 | ||
89 | getInt :: S.Get Int | ||
90 | getInt = fromIntegral <$> S.getWord32be | ||
91 | {-# INLINE getInt #-} | ||
92 | |||
93 | putInt :: S.Putter Int | ||
94 | putInt = S.putWord32be . fromIntegral | ||
95 | {-# INLINE putInt #-} | ||
96 | |||
97 | getIntB :: B.Get Int | ||
98 | getIntB = fromIntegral <$> B.getWord32be | ||
99 | {-# INLINE getIntB #-} | ||
100 | |||
101 | putIntB :: Int -> B.Put | ||
102 | putIntB = 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 | |||
202 | type BlockLIx = Int | ||
203 | type PieceLIx = Int | ||
204 | |||
205 | |||
206 | data 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 | |||
219 | getInt :: S.Get Int | ||
220 | getInt = fromIntegral <$> S.getWord32be | ||
221 | {-# INLINE getInt #-} | ||
222 | |||
223 | putInt :: S.Putter Int | ||
224 | putInt = S.putWord32be . fromIntegral | ||
225 | {-# INLINE putInt #-} | ||
226 | |||
227 | getIntB :: B.Get Int | ||
228 | getIntB = fromIntegral <$> B.getWord32be | ||
229 | {-# INLINE getIntB #-} | ||
230 | |||
231 | putIntB :: Int -> B.Put | ||
232 | putIntB = B.putWord32be . fromIntegral | ||
233 | {-# INLINE putIntB #-} | ||
234 | |||
235 | instance 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 | |||
245 | instance 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. | ||
256 | ppBlockIx :: BlockIx -> Doc | ||
257 | ppBlockIx BlockIx {..} = | ||
258 | "piece = " <> int ixPiece <> "," <+> | ||
259 | "offset = " <> int ixOffset <> "," <+> | ||
260 | "length = " <> int ixLength | ||
261 | |||
262 | {----------------------------------------------------------------------- | ||
263 | Block | ||
264 | -----------------------------------------------------------------------} | ||
265 | |||
266 | data 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. | ||
278 | ppBlock :: Block -> Doc | ||
279 | ppBlock = ppBlockIx . blockIx | ||
280 | |||
281 | blockSize :: Block -> Int | ||
282 | blockSize blk = fromIntegral (Lazy.length (blkData blk)) | ||
283 | {-# INLINE blockSize #-} | ||
284 | |||
285 | -- | Widely used semi-official block size. | ||
286 | defaultBlockSize :: Int | ||
287 | defaultBlockSize = 16 * 1024 | ||
288 | |||
289 | |||
290 | isPiece :: Int -> Block -> Bool | ||
291 | isPiece pieceSize (Block i offset bs) = | ||
292 | offset == 0 | ||
293 | && fromIntegral (Lazy.length bs) == pieceSize | ||
294 | && i >= 0 | ||
295 | {-# INLINE isPiece #-} | ||
296 | |||
297 | pieceIx :: Int -> Int -> BlockIx | ||
298 | pieceIx i = BlockIx i 0 | ||
299 | {-# INLINE pieceIx #-} | ||
300 | |||
301 | blockIx :: Block -> BlockIx | ||
302 | blockIx = BlockIx <$> blkPiece <*> blkOffset <*> blockSize | ||
303 | |||
304 | blockRange :: (Num a, Integral a) => Int -> Block -> (a, a) | ||
305 | blockRange 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 | |||
312 | ixRange :: (Num a, Integral a) => Int -> BlockIx -> (a, a) | ||
313 | ixRange 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) | |||
73 | import Network.BSD | 73 | import Network.BSD |
74 | import Network.Socket | 74 | import Network.Socket |
75 | 75 | ||
76 | import Data.Bitfield as BF | 76 | import Data.Torrent.Bitfield as BF |
77 | import Data.Torrent | 77 | import Data.Torrent.Metainfo |
78 | import Network.BitTorrent.Extension | 78 | import Network.BitTorrent.Extension |
79 | import Network.BitTorrent.Peer | 79 | import Network.BitTorrent.Peer |
80 | import Network.BitTorrent.Sessions.Types | 80 | import 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. | |||
248 | Normally, you would have one client session, however, if we needed, in | 248 | Normally, you would have one client session, however, if we needed, in |
249 | one application we could have many clients with different peer ID's | 249 | one application we could have many clients with different peer ID's |
250 | and different enabled extensions at the same time. | 250 | and 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 | |||
39 | import Network | 39 | import Network |
40 | import Network.URI | 40 | import Network.URI |
41 | 41 | ||
42 | import Data.Torrent | 42 | import Data.Torrent.Metainfo |
43 | import Network.BitTorrent.Peer | 43 | import Network.BitTorrent.Peer |
44 | import Network.BitTorrent.Sessions.Types | 44 | import Network.BitTorrent.Sessions.Types |
45 | import Network.BitTorrent.Tracker.Protocol | 45 | import 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 | |||
30 | import Network.URI | 30 | import Network.URI |
31 | import Network.HTTP | 31 | import Network.HTTP |
32 | 32 | ||
33 | import Data.Torrent | 33 | import Data.Torrent.Metainfo |
34 | import Network.BitTorrent.Tracker.Protocol | 34 | import 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) | |||
44 | import Data.Text.Encoding | 44 | import Data.Text.Encoding |
45 | import Data.Serialize hiding (Result) | 45 | import Data.Serialize hiding (Result) |
46 | import Data.URLEncoded as URL | 46 | import Data.URLEncoded as URL |
47 | import Data.Torrent | 47 | import Data.Torrent.Metainfo |
48 | 48 | ||
49 | import Network | 49 | import Network |
50 | import Network.Socket | 50 | import 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 | |||
24 | import Network.Socket hiding (Connected) | 24 | import Network.Socket hiding (Connected) |
25 | import Network.Socket.ByteString as BS | 25 | import Network.Socket.ByteString as BS |
26 | 26 | ||
27 | import Data.Torrent () | 27 | import Data.Torrent.Metainfo () |
28 | import Network.BitTorrent.Tracker.Protocol | 28 | import Network.BitTorrent.Tracker.Protocol |
29 | 29 | ||
30 | 30 | ||