{-# LANGUAGE RecordWildCards #-}

module Network.HTTP2.H2.Config where

import Control.Concurrent.STM
import Control.Monad (void)
import Data.IORef
import Foreign.Marshal.Alloc (free, mallocBytes)
import Network.HTTP.Semantics.Client
import Network.Socket
import Network.Socket.ByteString (sendAll, sendAllWith)
import qualified System.TimeManager as T

import Network.HPACK
import Network.HTTP2.H2.Types

-- | Making simple configuration whose IO is not efficient.
--   A write buffer is allocated internally.
--   WAI timeout manger is initialized with 30_000_000 microseconds.
allocSimpleConfig :: Socket -> BufferSize -> IO Config
allocSimpleConfig :: Socket -> Int -> IO Config
allocSimpleConfig Socket
s Int
bufsiz = Socket -> Int -> Int -> IO Config
allocSimpleConfig' Socket
s Int
bufsiz (Int
30 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1000000)

-- | Making simple configuration whose IO is not efficient.
--   A write buffer is allocated internally.
--   The third argument is microseconds to initialize WAI
--   timeout manager.
allocSimpleConfig' :: Socket -> BufferSize -> Int -> IO Config
allocSimpleConfig' :: Socket -> Int -> Int -> IO Config
allocSimpleConfig' Socket
s Int
bufsiz Int
usec = Socket -> Int -> Int -> STM (ZonkAny 0) -> IO Config
forall a. Socket -> Int -> Int -> STM a -> IO Config
allocSimpleConfigWith Socket
s Int
bufsiz Int
usec STM (ZonkAny 0)
forall a. STM a
retry

-- | 'allocSimpleConfig'' whose receive the caller can give up on.  The
--   'STM' action is waited for beside each receive; when it fires, the
--   reader ends as it does at end of input.  Nothing is closed and no
--   thread is interrupted to make it return.
--
--   It also fills 'confSendAllWith' and 'confReadNWith' (since 5.4.13), so
--   that the library can give up on a send or a read in the same way.
--
--   @since 5.4.11
allocSimpleConfigWith :: Socket -> BufferSize -> Int -> STM a -> IO Config
allocSimpleConfigWith :: forall a. Socket -> Int -> Int -> STM a -> IO Config
allocSimpleConfigWith Socket
s Int
bufsiz Int
usec STM a
giveUp = do
    confWriteBuffer <- Int -> IO (Ptr Word8)
forall a. Int -> IO (Ptr a)
mallocBytes Int
bufsiz
    let confBufferSize = Int
bufsiz
    let confSendAll = Socket -> ByteString -> IO ()
sendAll Socket
s
        confSendAllWith = (STM a -> ByteString -> IO (Either a ()))
-> Maybe (STM a -> ByteString -> IO (Either a ()))
forall a. a -> Maybe a
Just ((STM a -> ByteString -> IO (Either a ()))
 -> Maybe (STM a -> ByteString -> IO (Either a ())))
-> (STM a -> ByteString -> IO (Either a ()))
-> Maybe (STM a -> ByteString -> IO (Either a ()))
forall a b. (a -> b) -> a -> b
$ \STM a
giveUp' ByteString
bs -> Socket -> ByteString -> STM a -> IO (Either a ())
forall a. Socket -> ByteString -> STM a -> IO (Either a ())
sendAllWith Socket
s ByteString
bs STM a
giveUp'
    ref <- newIORef Nothing
    let confReadN = Socket -> IORef (Maybe ByteString) -> STM a -> ReadN
forall a. Socket -> IORef (Maybe ByteString) -> STM a -> ReadN
defaultReadNWith Socket
s IORef (Maybe ByteString)
ref STM a
giveUp
        confReadNWith = (STM () -> ReadN) -> Maybe (STM () -> ReadN)
forall a. a -> Maybe a
Just ((STM () -> ReadN) -> Maybe (STM () -> ReadN))
-> (STM () -> ReadN) -> Maybe (STM () -> ReadN)
forall a b. (a -> b) -> a -> b
$ \STM ()
stop -> Socket -> IORef (Maybe ByteString) -> STM () -> ReadN
forall a. Socket -> IORef (Maybe ByteString) -> STM a -> ReadN
defaultReadNWith Socket
s IORef (Maybe ByteString)
ref (STM a -> STM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void STM a
giveUp STM () -> STM () -> STM ()
forall a. STM a -> STM a -> STM a
`orElse` STM ()
stop)
    let confPositionReadMaker = PositionReadMaker
defaultPositionReadMaker
    confTimeoutManager <- T.initialize usec
    confMySockAddr <- getSocketName s
    confPeerSockAddr <- getPeerName s
    let confReadNTimeout = Bool
False
    let confOnInformational = \p
_ p
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    return Config{..}

-- | Deallocating the resource of the simple configuration.
--
--   This does not cancel timeout actions registered with
--   'confTimeoutManager'.  Since time-manager 0.3, a manager holds no
--   registrations, so there is nothing to kill: an action registered with
--   'System.TimeManager.register' runs even after this returns unless it
--   is cancelled with 'System.TimeManager.cancel'.  Use
--   'System.TimeManager.withHandle', which cancels it when the scope ends.
freeSimpleConfig :: Config -> IO ()
freeSimpleConfig :: Config -> IO ()
freeSimpleConfig Config
conf = Ptr Word8 -> IO ()
forall a. Ptr a -> IO ()
free (Ptr Word8 -> IO ()) -> Ptr Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Config -> Ptr Word8
confWriteBuffer Config
conf