From: Ramakrishnan Muthukrishnan Date: Sun, 4 Oct 2015 15:59:54 +0000 (+0530) Subject: WIP: UDP tracker: send/receive request/response X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/-?a=commitdiff_plain;h=3dd1ec8312982d53dfc8e5e80f94d7a98f8a9e4e;p=functorrent.git WIP: UDP tracker: send/receive request/response --- diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs index f995f1b..6ae0281 100644 --- a/src/FuncTorrent/Tracker.hs +++ b/src/FuncTorrent/Tracker.hs @@ -7,14 +7,16 @@ module FuncTorrent.Tracker import Prelude hiding (lookup, splitAt) +import System.IO (Handle) import Control.Applicative (liftA2) import Control.Concurrent (threadDelay) import Control.Concurrent.MVar (MVar, newEmptyMVar, newMVar, readMVar, putMVar) -import Data.Binary (Binary(..)) +import Data.Binary (Binary(..), encode, decode) import Data.Binary.Put (putWord16be, putWord32be, putWord64be, putByteString) import Data.Binary.Get (Get, isEmpty, getByteString, getWord16be, getWord32be) -import Data.ByteString (ByteString) +import Data.ByteString (ByteString, hGet, hPut) import Data.ByteString.Char8 as BC (pack, unpack, splitAt) +import Data.ByteString.Lazy (fromStrict, toStrict) import Data.Char (chr) import Data.List (intercalate) import Data.Map as M (lookup) @@ -22,7 +24,8 @@ import Network (PortNumber) import Network.HTTP.Base (urlEncode) import qualified Data.ByteString.Base16 as B16 (encode) -import FuncTorrent.Bencode (BVal(..), decode) +import FuncTorrent.Bencode (BVal(..)) +import qualified FuncTorrent.Bencode as Benc import FuncTorrent.Metainfo (Info(..), Metainfo(..)) import FuncTorrent.Network (sendGetRequest) import FuncTorrent.Peer (Peer(..)) @@ -219,7 +222,7 @@ trackerLoop port peerId m st = do up <- readMVar $ uploaded st down <- readMVar $ downloaded st resp <- sendGetRequest (head . announceList $ m) $ mkArgs port peerId up down m - case decode resp of + case Benc.decode resp of Left e -> return $ pack (show e) Right trackerInfo -> case mkTrackerResponse trackerInfo of @@ -229,3 +232,14 @@ trackerLoop port peerId m st = do _ <- putMVar (connectedPeers st) (peers tresp) trackerLoop port peerId m st +-- udp tracker +getResponse :: Handle -> IO UDPResponse +getResponse h = do + -- connect packet is 16 bytes long + -- announce packet is atleast 20 bytes long + bs <- hGet h (16*1024) + return $ decode $ fromStrict bs + +sendRequest :: Handle -> UDPRequest -> IO () +sendRequest h req = hPut h bsReq + where bsReq = toStrict $ encode req