From 388458ebf9b774d3149cba659a3ffb3503d85ab0 Mon Sep 17 00:00:00 2001 From: Bernardo Gomes Negri <38192358+BernardoGomesNegri@users.noreply.github.com> Date: Sat, 30 Oct 2021 22:49:09 -0300 Subject: [PATCH] small reformating --- app/Main.hs | 2 +- app/Rcon.hs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 45bf80b..37a5af4 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -109,5 +109,5 @@ serveFile f = do randomString :: Int -> IO Token randomString size = let all = ['0'..'9'] ++ ['a'..'z'] ++ ['A'..'Z'] in - let randomChar = (!!) all <$> getStdRandom (randomR (0, 61)) in + let randomChar = (!!) all <$> getStdRandom (randomR (0, length all - 1)) in (sequenceA . replicate size) randomChar diff --git a/app/Rcon.hs b/app/Rcon.hs index d1e3a76..9b7bee9 100644 --- a/app/Rcon.hs +++ b/app/Rcon.hs @@ -14,6 +14,10 @@ import System.Random(randomIO) import TransHelpers import Control.Monad (join) + +individualWait = 100000 +numberWait = 6 + type Port = Int data RequestType = Auth | Command | AuthResponse | CommandResponse deriving Show @@ -36,10 +40,13 @@ data ConnErr = BadPassword | UnexpectedResponse | ConnError String deriving Show errHandler :: SomeException -> IO (Either ConnErr Connection) errHandler e = return $ Left $ ConnError (show e) +errHandlerMaybe :: SomeException -> IO (Maybe a) +errHandlerMaybe _ = return Nothing + withConn :: Connection -> (Socket -> IO a) -> MaybeT IO a withConn conn@Connection {adress = adress, port = port, password = pwd} action = do MaybeT $ - handle errHandler $ + handle errHandlerMaybe $ connect adress (show port) $ \(socket,_) -> do authenticated <- authenticateConn conn socket if authenticated then do @@ -100,10 +107,10 @@ getAndWait socket s = MaybeT $ go 0 B.empty s where case stuff of Just x -> do let newTotal = B.concat [total,x] - if B.length x < size && count < 6 then threadDelay 100000 >> go (count + 1) newTotal size else + if B.length x < size && count < numberWait then threadDelay individualWait >> go (count + 1) newTotal size else return $ return newTotal Nothing -> - if count < 6 then threadDelay 100000 >> go (count + 1) total size else return Nothing + if count < numberWait then threadDelay individualWait >> go (count + 1) total size else return Nothing getServerPacket :: Socket -> Maybe Int32 -> MaybeT IO Packet getServerPacket socket idM = do