module Network.HTTP2.Arch.Config where

import Data.ByteString (ByteString)
import Data.IORef
import Foreign.Marshal.Alloc (mallocBytes, free)
import Network.Socket
import Network.Socket.ByteString (sendAll)
import qualified System.TimeManager as T

import Network.HPACK
import Network.HTTP2.Arch.File
import Network.HTTP2.Arch.ReadN

-- | HTTP/2 configuration.
data Config = Config {
    -- confWriteBuffer is used only by frameSender.
    -- This MUST be freed after frameSender is terminated.
      Config -> Buffer
confWriteBuffer :: Buffer
    , Config -> BufferSize
confBufferSize  :: BufferSize
    , Config -> ByteString -> IO ()
confSendAll     :: ByteString -> IO ()
    , Config -> BufferSize -> IO ByteString
confReadN       :: Int -> IO ByteString
    , Config -> PositionReadMaker
confPositionReadMaker :: PositionReadMaker
    , Config -> Manager
confTimeoutManager :: T.Manager
    }

-- | Making simple configuration whose IO is not efficient.
--   A write buffer is allocated internally.
allocSimpleConfig :: Socket -> BufferSize -> IO Config
allocSimpleConfig :: Socket -> BufferSize -> IO Config
allocSimpleConfig Socket
s BufferSize
bufsiz = do
    Buffer
buf <- BufferSize -> IO Buffer
forall a. BufferSize -> IO (Ptr a)
mallocBytes BufferSize
bufsiz
    IORef (Maybe ByteString)
ref <- Maybe ByteString -> IO (IORef (Maybe ByteString))
forall a. a -> IO (IORef a)
newIORef Maybe ByteString
forall a. Maybe a
Nothing
    Manager
timmgr <- BufferSize -> IO Manager
T.initialize (BufferSize -> IO Manager) -> BufferSize -> IO Manager
forall a b. (a -> b) -> a -> b
$ BufferSize
30 BufferSize -> BufferSize -> BufferSize
forall a. Num a => a -> a -> a
* BufferSize
1000000
    let config :: Config
config = Config :: Buffer
-> BufferSize
-> (ByteString -> IO ())
-> (BufferSize -> IO ByteString)
-> PositionReadMaker
-> Manager
-> Config
Config {
            confWriteBuffer :: Buffer
confWriteBuffer = Buffer
buf
          , confBufferSize :: BufferSize
confBufferSize = BufferSize
bufsiz
          , confSendAll :: ByteString -> IO ()
confSendAll = Socket -> ByteString -> IO ()
sendAll Socket
s
          , confReadN :: BufferSize -> IO ByteString
confReadN = Socket -> IORef (Maybe ByteString) -> BufferSize -> IO ByteString
defaultReadN Socket
s IORef (Maybe ByteString)
ref
          , confPositionReadMaker :: PositionReadMaker
confPositionReadMaker = PositionReadMaker
defaultPositionReadMaker
          , confTimeoutManager :: Manager
confTimeoutManager = Manager
timmgr
          }
    Config -> IO Config
forall (m :: * -> *) a. Monad m => a -> m a
return Config
config

-- | Deallocating the resource of the simple configuration.
freeSimpleConfig :: Config -> IO ()
freeSimpleConfig :: Config -> IO ()
freeSimpleConfig Config
conf = do
    Buffer -> IO ()
forall a. Ptr a -> IO ()
free (Buffer -> IO ()) -> Buffer -> IO ()
forall a b. (a -> b) -> a -> b
$ Config -> Buffer
confWriteBuffer Config
conf
    Manager -> IO ()
T.killManager (Manager -> IO ()) -> Manager -> IO ()
forall a b. (a -> b) -> a -> b
$ Config -> Manager
confTimeoutManager Config
conf