{-# 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 Any -> IO Config
forall a. Socket -> Int -> Int -> STM a -> IO Config
allocSimpleConfigWith Socket
s Int
bufsiz Int
usec STM Any
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
Ptr Word8
confWriteBuffer <- Int -> IO (Ptr Word8)
forall a. Int -> IO (Ptr a)
mallocBytes Int
bufsiz
let confBufferSize :: Int
confBufferSize = Int
bufsiz
let confSendAll :: ByteString -> IO ()
confSendAll = Socket -> ByteString -> IO ()
sendAll Socket
s
confSendAllWith :: Maybe (STM a -> ByteString -> IO (Either a ()))
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'
IORef (Maybe ByteString)
ref <- Maybe ByteString -> IO (IORef (Maybe ByteString))
forall a. a -> IO (IORef a)
newIORef Maybe ByteString
forall a. Maybe a
Nothing
let confReadN :: ReadN
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 :: Maybe (STM () -> ReadN)
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
confPositionReadMaker = PositionReadMaker
defaultPositionReadMaker
Manager
confTimeoutManager <- Int -> IO Manager
T.initialize Int
usec
SockAddr
confMySockAddr <- Socket -> IO SockAddr
getSocketName Socket
s
SockAddr
confPeerSockAddr <- Socket -> IO SockAddr
getPeerName Socket
s
let confReadNTimeout :: Bool
confReadNTimeout = Bool
False
let confOnInformational :: p -> p -> IO ()
confOnInformational = \p
_ p
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Config -> IO Config
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Config{Bool
Int
Maybe (STM () -> ReadN)
Maybe (STM () -> ByteString -> IO (Either () ()))
Ptr Word8
SockAddr
Manager
ReadN
Int -> TokenHeaderTable -> IO ()
PositionReadMaker
ByteString -> IO ()
forall {a}. Maybe (STM a -> ByteString -> IO (Either a ()))
forall {p} {p}. p -> p -> IO ()
confSendAllWith :: Maybe (STM () -> ByteString -> IO (Either () ()))
confReadNWith :: Maybe (STM () -> ReadN)
confWriteBuffer :: Ptr Word8
confBufferSize :: Int
confSendAll :: ByteString -> IO ()
confSendAllWith :: forall {a}. Maybe (STM a -> ByteString -> IO (Either a ()))
confReadN :: ReadN
confReadNWith :: Maybe (STM () -> ReadN)
confPositionReadMaker :: PositionReadMaker
confTimeoutManager :: Manager
confMySockAddr :: SockAddr
confPeerSockAddr :: SockAddr
confReadNTimeout :: Bool
confOnInformational :: forall {p} {p}. p -> p -> IO ()
confOnInformational :: Int -> TokenHeaderTable -> IO ()
confReadNTimeout :: Bool
confPeerSockAddr :: SockAddr
confMySockAddr :: SockAddr
confTimeoutManager :: Manager
confPositionReadMaker :: PositionReadMaker
confReadN :: ReadN
confSendAll :: ByteString -> IO ()
confBufferSize :: Int
confWriteBuffer :: Ptr Word8
..}
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