{-# 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
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)
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
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{..}
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