{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Network.HTTP2.TLS.Config where

import Data.ByteString (ByteString)
import Foreign.Marshal.Alloc (free, mallocBytes)
import Network.HTTP2.Client (
    Config (..),
    defaultPositionReadMaker,
 )
import Network.Socket.BufferPool
import qualified System.TimeManager as T

import Network.HTTP2.TLS.Settings

allocConfigForServer
    :: Settings -> T.Manager -> (ByteString -> IO ()) -> IO ByteString -> IO Config
allocConfigForServer :: Settings
-> Manager -> (ByteString -> IO ()) -> IO ByteString -> IO Config
allocConfigForServer Settings{Int
settingReadBufferLowerLimit :: Settings -> Int
settingReadBufferSize :: Settings -> Int
settingsSlowlorisSize :: Settings -> Int
settingsSendBufferSize :: Settings -> Int
settingsTimeout :: Settings -> Int
settingReadBufferLowerLimit :: Int
settingReadBufferSize :: Int
settingsSlowlorisSize :: Int
settingsSendBufferSize :: Int
settingsTimeout :: Int
..} Manager
mgr ByteString -> IO ()
send IO ByteString
recv = do
    Ptr Word8
buf <- forall a. Int -> IO (Ptr a)
mallocBytes Int
settingsSendBufferSize
    RecvN
recvN <- ByteString -> IO ByteString -> IO RecvN
makeRecvN ByteString
"" IO ByteString
recv
    let config :: Config
config =
            Config
                { confWriteBuffer :: Ptr Word8
confWriteBuffer = Ptr Word8
buf
                , confBufferSize :: Int
confBufferSize = Int
settingsSendBufferSize
                , confSendAll :: ByteString -> IO ()
confSendAll = ByteString -> IO ()
send
                , confReadN :: RecvN
confReadN = RecvN
recvN
                , confPositionReadMaker :: PositionReadMaker
confPositionReadMaker = PositionReadMaker
defaultPositionReadMaker
                , confTimeoutManager :: Manager
confTimeoutManager = Manager
mgr
                }
    forall (m :: * -> *) a. Monad m => a -> m a
return Config
config

-- | Deallocating the resource of the simple configuration.
freeConfigForServer :: Config -> IO ()
freeConfigForServer :: Config -> IO ()
freeConfigForServer Config
conf = forall a. Ptr a -> IO ()
free forall a b. (a -> b) -> a -> b
$ Config -> Ptr Word8
confWriteBuffer Config
conf


allocConfigForClient :: (ByteString -> IO ()) -> IO ByteString -> IO Config
allocConfigForClient :: (ByteString -> IO ()) -> IO ByteString -> IO Config
allocConfigForClient ByteString -> IO ()
send IO ByteString
recv = do
    let wbufsiz :: Int
wbufsiz = Int
4096 -- fixme
    Ptr Word8
buf <- forall a. Int -> IO (Ptr a)
mallocBytes Int
wbufsiz
    RecvN
recvN <- ByteString -> IO ByteString -> IO RecvN
makeRecvN ByteString
"" IO ByteString
recv
    -- A global manager does not exist.
    -- So, a timeout manager is created per connection.
    Manager
mgr <- Int -> IO Manager
T.initialize Int
30000000 -- fixme
    let config :: Config
config =
            Config
                { confWriteBuffer :: Ptr Word8
confWriteBuffer = Ptr Word8
buf
                , confBufferSize :: Int
confBufferSize = Int
wbufsiz
                , confSendAll :: ByteString -> IO ()
confSendAll = ByteString -> IO ()
send
                , confReadN :: RecvN
confReadN = RecvN
recvN
                , confPositionReadMaker :: PositionReadMaker
confPositionReadMaker = PositionReadMaker
defaultPositionReadMaker
                , confTimeoutManager :: Manager
confTimeoutManager = Manager
mgr
                }
    forall (m :: * -> *) a. Monad m => a -> m a
return Config
config

-- | Deallocating the resource of the simple configuration.
freeConfigForClient :: Config -> IO ()
freeConfigForClient :: Config -> IO ()
freeConfigForClient Config{Int
Ptr Word8
Manager
RecvN
PositionReadMaker
ByteString -> IO ()
confTimeoutManager :: Manager
confPositionReadMaker :: PositionReadMaker
confReadN :: RecvN
confSendAll :: ByteString -> IO ()
confBufferSize :: Int
confWriteBuffer :: Ptr Word8
confTimeoutManager :: Config -> Manager
confPositionReadMaker :: Config -> PositionReadMaker
confReadN :: Config -> RecvN
confSendAll :: Config -> ByteString -> IO ()
confBufferSize :: Config -> Int
confWriteBuffer :: Config -> Ptr Word8
..} = do
    forall a. Ptr a -> IO ()
free Ptr Word8
confWriteBuffer
    Manager -> IO ()
T.killManager Manager
confTimeoutManager