{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Network.HTTP2.H2.Context where
import Control.Concurrent (ThreadId)
import Control.Concurrent.STM
import qualified Control.Exception as E
import Data.IORef
import qualified Data.IntMap.Strict as IntMap
import qualified Data.IntSet as IntSet
import Network.Control
import Network.Socket (SockAddr)
import qualified System.ThreadManager as T
import qualified System.TimeManager as TM
import System.Timeout (timeout)
import Imports hiding (insert)
import Network.HPACK
import Network.HTTP2.Frame
import Network.HTTP2.H2.Queue
import Network.HTTP2.H2.Settings
import Network.HTTP2.H2.Stream
import Network.HTTP2.H2.StreamTable
import Network.HTTP2.H2.Types
data Role = Client | Server deriving (Role -> Role -> Bool
(Role -> Role -> Bool) -> (Role -> Role -> Bool) -> Eq Role
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Role -> Role -> Bool
== :: Role -> Role -> Bool
$c/= :: Role -> Role -> Bool
/= :: Role -> Role -> Bool
Eq, Int -> Role -> ShowS
[Role] -> ShowS
Role -> String
(Int -> Role -> ShowS)
-> (Role -> String) -> ([Role] -> ShowS) -> Show Role
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Role -> ShowS
showsPrec :: Int -> Role -> ShowS
$cshow :: Role -> String
show :: Role -> String
$cshowList :: [Role] -> ShowS
showList :: [Role] -> ShowS
Show)
stopAfterAndWait
:: T.ThreadManager -> IO a -> (Maybe E.SomeException -> IO ()) -> IO a
stopAfterAndWait :: forall a.
ThreadManager -> IO a -> (Maybe SomeException -> IO ()) -> IO a
stopAfterAndWait ThreadManager
mgr IO a
action Maybe SomeException -> IO ()
cleanup =
ThreadManager -> IO a -> (Maybe SomeException -> IO ()) -> IO a
forall a.
ThreadManager -> IO a -> (Maybe SomeException -> IO ()) -> IO a
T.stopAfter ThreadManager
mgr IO a
action ((Maybe SomeException -> IO ()) -> IO a)
-> (Maybe SomeException -> IO ()) -> IO a
forall a b. (a -> b) -> a -> b
$ \Maybe SomeException
res ->
Maybe SomeException -> IO ()
cleanup Maybe SomeException
res IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO a
`E.finally` IO (Maybe ()) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Int -> IO () -> IO (Maybe ())
forall a. Int -> IO a -> IO (Maybe a)
timeout Int
stopTimeout (IO () -> IO (Maybe ())) -> IO () -> IO (Maybe ())
forall a b. (a -> b) -> a -> b
$ ThreadManager -> IO ()
T.waitUntilAllGone ThreadManager
mgr)
stopTimeout :: Int
stopTimeout :: Int
stopTimeout = Int
5000000
data RoleInfo = RIS ServerInfo | RIC ClientInfo
type Launch = Context -> Stream -> InpObj -> IO ()
newtype ServerInfo = ServerInfo
{ ServerInfo -> Launch
launch :: Launch
}
data ClientInfo = ClientInfo
{ ClientInfo -> ByteString
scheme :: ByteString
, ClientInfo -> String
authority :: Authority
}
toServerInfo :: RoleInfo -> ServerInfo
toServerInfo :: RoleInfo -> ServerInfo
toServerInfo (RIS ServerInfo
x) = ServerInfo
x
toServerInfo RoleInfo
_ = String -> ServerInfo
forall a. HasCallStack => String -> a
error String
"toServerInfo"
toClientInfo :: RoleInfo -> ClientInfo
toClientInfo :: RoleInfo -> ClientInfo
toClientInfo (RIC ClientInfo
x) = ClientInfo
x
toClientInfo RoleInfo
_ = String -> ClientInfo
forall a. HasCallStack => String -> a
error String
"toClientInfo"
newServerInfo :: Launch -> RoleInfo
newServerInfo :: Launch -> RoleInfo
newServerInfo = ServerInfo -> RoleInfo
RIS (ServerInfo -> RoleInfo)
-> (Launch -> ServerInfo) -> Launch -> RoleInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Launch -> ServerInfo
ServerInfo
newClientInfo :: ByteString -> Authority -> RoleInfo
newClientInfo :: ByteString -> String -> RoleInfo
newClientInfo ByteString
scm String
auth = ClientInfo -> RoleInfo
RIC (ClientInfo -> RoleInfo) -> ClientInfo -> RoleInfo
forall a b. (a -> b) -> a -> b
$ ByteString -> String -> ClientInfo
ClientInfo ByteString
scm String
auth
data Context = Context
{ Context -> Role
role :: Role
, Context -> RoleInfo
roleInfo :: RoleInfo
,
Context -> Settings
mySettings :: Settings
, Context -> IORef Bool
myFirstSettings :: IORef Bool
, Context -> IORef Settings
peerSettings :: IORef Settings
, Context -> TVar OddStreamTable
oddStreamTable :: TVar OddStreamTable
, Context -> TVar EvenStreamTable
evenStreamTable :: TVar EvenStreamTable
, Context -> IORef (Maybe HeaderContinuation)
continued :: IORef (Maybe HeaderContinuation)
, Context -> TVar Int
myStreamId :: TVar StreamId
, Context -> IORef Int
peerStreamId :: IORef StreamId
, Context -> TVar Int
peerLastStreamId :: TVar StreamId
, Context -> IORef Int
outputBufferLimit :: IORef Int
, Context -> TQueue Output
outputQ :: TQueue Output
, Context -> TVar Int
outputQStreamID :: TVar StreamId
, Context -> ControlQ
controlQ :: ControlQ
, Context -> DynamicTable
encodeDynamicTable :: DynamicTable
, Context -> DynamicTable
decodeDynamicTable :: DynamicTable
,
Context -> TVar TxFlow
txFlow :: TVar TxFlow
, Context -> IORef RxFlow
rxFlow :: IORef RxFlow
, Context -> Rate
pingRate :: Rate
, Context -> Rate
settingsRate :: Rate
, Context -> Rate
emptyFrameRate :: Rate
, Context -> Rate
rstRate :: Rate
, Context -> SockAddr
mySockAddr :: SockAddr
, Context -> SockAddr
peerSockAddr :: SockAddr
, Context -> ThreadManager
threadManager :: T.ThreadManager
, Context -> Manager
timeoutManager :: TM.Manager
, Context -> TVar (Maybe SomeException)
receiverDone :: TVar (Maybe E.SomeException)
, Context -> TVar Bool
stopReceiving :: TVar Bool
, Context -> Bool
receiverStoppable :: Bool
, Context -> STM Bool
appDone :: STM Bool
, Context -> Int -> TokenHeaderTable -> IO ()
informationalCallback :: StreamId -> TokenHeaderTable -> IO ()
, Context -> TVar (Maybe Int)
peerGoAway :: TVar (Maybe StreamId)
, Context -> TVar (Maybe Int)
myGoAway :: TVar (Maybe StreamId)
, Context -> TVar ByteString
pingAcked :: TVar ByteString
, Context -> TVar Int
activeRequests :: TVar Int
, Context -> IORef IntSet
resetByMe :: IORef IntSet.IntSet
}
data =
{ HeaderContinuation -> Int
hcStreamId :: StreamId
, HeaderContinuation -> PartialHeaderBlock
hcBlock :: PartialHeaderBlock
, HeaderContinuation -> Bool
hcEndOfStream :: Bool
}
newContext
:: RoleInfo
-> Config
-> Int
-> Int
-> Settings
-> TM.Manager
-> Maybe (STM Bool)
-> IO Context
newContext :: RoleInfo
-> Config
-> Int
-> Int
-> Settings
-> Manager
-> Maybe (STM Bool)
-> IO Context
newContext RoleInfo
roleInfo Config{Bool
Int
Maybe (STM () -> Int -> IO ByteString)
Maybe (STM () -> ByteString -> IO (Either () ()))
Buffer
Manager
SockAddr
Int -> IO ByteString
Int -> TokenHeaderTable -> IO ()
PositionReadMaker
ByteString -> IO ()
confOnInformational :: Config -> Int -> TokenHeaderTable -> IO ()
confWriteBuffer :: Buffer
confBufferSize :: Int
confSendAll :: ByteString -> IO ()
confSendAllWith :: Maybe (STM () -> ByteString -> IO (Either () ()))
confReadN :: Int -> IO ByteString
confReadNWith :: Maybe (STM () -> Int -> IO ByteString)
confPositionReadMaker :: PositionReadMaker
confTimeoutManager :: Manager
confMySockAddr :: SockAddr
confPeerSockAddr :: SockAddr
confReadNTimeout :: Bool
confOnInformational :: Int -> TokenHeaderTable -> IO ()
confBufferSize :: Config -> Int
confMySockAddr :: Config -> SockAddr
confPeerSockAddr :: Config -> SockAddr
confPositionReadMaker :: Config -> PositionReadMaker
confReadN :: Config -> Int -> IO ByteString
confReadNTimeout :: Config -> Bool
confReadNWith :: Config -> Maybe (STM () -> Int -> IO ByteString)
confSendAll :: Config -> ByteString -> IO ()
confSendAllWith :: Config -> Maybe (STM () -> ByteString -> IO (Either () ()))
confTimeoutManager :: Config -> Manager
confWriteBuffer :: Config -> Buffer
..} Int
cacheSiz Int
connRxWS Settings
mySettings Manager
timmgr Maybe (STM Bool)
mdone = do
myFirstSettings <- Bool -> IO (IORef Bool)
forall a. a -> IO (IORef a)
newIORef Bool
False
peerSettings <-
newIORef baseSettings{maxConcurrentStreams = Just defaultMaxStreams}
oddStreamTable <- newTVarIO emptyOddStreamTable
evenStreamTable <- newTVarIO (emptyEvenStreamTable cacheSiz)
continued <- newIORef Nothing
myStreamId <- newTVarIO sid0
peerStreamId <- newIORef 0
peerLastStreamId <- newTVarIO 0
outputBufferLimit <- newIORef buflim
outputQ <- newTQueueIO
outputQStreamID <- newTVarIO sid0
controlQ <- newControlQ
encodeDynamicTable <- newDynamicTableForEncoding defaultDynamicTableSize
decodeDynamicTable <-
newDynamicTableForDecoding (headerTableSize mySettings) 4096
setHeaderListSizeLimit
(Just (fromMaybe defaultHeaderListSizeLimit (maxHeaderListSize mySettings)))
decodeDynamicTable
txFlow <- newTVarIO (newTxFlow defaultWindowSize)
rxFlow <- newIORef (newRxFlow connRxWS)
pingRate <- newRate
settingsRate <- newRate
emptyFrameRate <- newRate
rstRate <- newRate
let mySockAddr = SockAddr
confMySockAddr
let peerSockAddr = SockAddr
confPeerSockAddr
threadManager <- T.newThreadManager
let timeoutManager = Manager
timmgr
receiverDone <- newTVarIO Nothing
stopReceiving <- newTVarIO False
let receiverStoppable = Maybe (STM () -> Int -> IO ByteString) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (STM () -> Int -> IO ByteString)
confReadNWith
let informationalCallback = Int -> TokenHeaderTable -> IO ()
confOnInformational
let appDone = STM Bool -> Maybe (STM Bool) -> STM Bool
forall a. a -> Maybe a -> a
fromMaybe (Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False) Maybe (STM Bool)
mdone
peerGoAway <- newTVarIO Nothing
myGoAway <- newTVarIO Nothing
pingAcked <- newTVarIO ""
activeRequests <- newTVarIO 0
resetByMe <- newIORef IntSet.empty
return Context{..}
where
role :: Role
role = case RoleInfo
roleInfo of
RIC{} -> Role
Client
RoleInfo
_ -> Role
Server
sid0 :: Int
sid0
| Role
role Role -> Role -> Bool
forall a. Eq a => a -> a -> Bool
== Role
Client = Int
1
| Bool
otherwise = Int
2
dlim :: Int
dlim = Int
defaultPayloadLength Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
frameHeaderLength
buflim :: Int
buflim
| Int
confBufferSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
dlim = Int
dlim
| Bool
otherwise = Int
confBufferSize
stopReceiver :: Context -> ThreadId -> IO ()
stopReceiver :: Context -> ThreadId -> IO ()
stopReceiver Context{TVar Bool
stopReceiving :: Context -> TVar Bool
stopReceiving :: TVar Bool
stopReceiving, Bool
receiverStoppable :: Context -> Bool
receiverStoppable :: Bool
receiverStoppable} ThreadId
receiver
| Bool
receiverStoppable = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar Bool -> Bool -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar Bool
stopReceiving Bool
True
| Bool
otherwise = ThreadId -> HTTP2Error -> IO ()
forall e. Exception e => ThreadId -> e -> IO ()
E.throwTo ThreadId
receiver HTTP2Error
ConnectionIsClosed
receiverStopped :: Context -> STM ()
receiverStopped :: Context -> STM ()
receiverStopped Context{TVar Bool
stopReceiving :: Context -> TVar Bool
stopReceiving :: TVar Bool
stopReceiving} = TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
stopReceiving STM Bool -> (Bool -> STM ()) -> STM ()
forall a b. STM a -> (a -> STM b) -> STM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> STM ()
check
timedOut :: Context -> STM ()
timedOut :: Context -> STM ()
timedOut Context{TVar (Maybe SomeException)
receiverDone :: Context -> TVar (Maybe SomeException)
receiverDone :: TVar (Maybe SomeException)
receiverDone} = do
done <- TVar (Maybe SomeException) -> STM (Maybe SomeException)
forall a. TVar a -> STM a
readTVar TVar (Maybe SomeException)
receiverDone
case done >>= E.fromException of
Just HTTP2Error
ConnectionIsTimeout -> () -> STM ()
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Maybe HTTP2Error
_ -> STM ()
forall a. STM a
retry
isClient :: Context -> Bool
isClient :: Context -> Bool
isClient Context
ctx = Context -> Role
role Context
ctx Role -> Role -> Bool
forall a. Eq a => a -> a -> Bool
== Role
Client
isServer :: Context -> Bool
isServer :: Context -> Bool
isServer Context
ctx = Context -> Role
role Context
ctx Role -> Role -> Bool
forall a. Eq a => a -> a -> Bool
== Role
Server
getMyNewStreamId :: Context -> STM StreamId
getMyNewStreamId :: Context -> STM Int
getMyNewStreamId Context{Bool
STM Bool
TVar Bool
TVar Int
TVar (Maybe Int)
TVar (Maybe SomeException)
TVar ByteString
TVar TxFlow
TVar EvenStreamTable
TVar OddStreamTable
IORef Bool
IORef Int
IORef (Maybe HeaderContinuation)
IORef RxFlow
IORef IntSet
IORef Settings
DynamicTable
Rate
Settings
ControlQ
Manager
SockAddr
TQueue Output
Role
RoleInfo
ThreadManager
Int -> TokenHeaderTable -> IO ()
role :: Context -> Role
roleInfo :: Context -> RoleInfo
mySettings :: Context -> Settings
myFirstSettings :: Context -> IORef Bool
peerSettings :: Context -> IORef Settings
oddStreamTable :: Context -> TVar OddStreamTable
evenStreamTable :: Context -> TVar EvenStreamTable
continued :: Context -> IORef (Maybe HeaderContinuation)
myStreamId :: Context -> TVar Int
peerStreamId :: Context -> IORef Int
peerLastStreamId :: Context -> TVar Int
outputBufferLimit :: Context -> IORef Int
outputQ :: Context -> TQueue Output
outputQStreamID :: Context -> TVar Int
controlQ :: Context -> ControlQ
encodeDynamicTable :: Context -> DynamicTable
decodeDynamicTable :: Context -> DynamicTable
txFlow :: Context -> TVar TxFlow
rxFlow :: Context -> IORef RxFlow
pingRate :: Context -> Rate
settingsRate :: Context -> Rate
emptyFrameRate :: Context -> Rate
rstRate :: Context -> Rate
mySockAddr :: Context -> SockAddr
peerSockAddr :: Context -> SockAddr
threadManager :: Context -> ThreadManager
timeoutManager :: Context -> Manager
receiverDone :: Context -> TVar (Maybe SomeException)
stopReceiving :: Context -> TVar Bool
receiverStoppable :: Context -> Bool
appDone :: Context -> STM Bool
informationalCallback :: Context -> Int -> TokenHeaderTable -> IO ()
peerGoAway :: Context -> TVar (Maybe Int)
myGoAway :: Context -> TVar (Maybe Int)
pingAcked :: Context -> TVar ByteString
activeRequests :: Context -> TVar Int
resetByMe :: Context -> IORef IntSet
role :: Role
roleInfo :: RoleInfo
mySettings :: Settings
myFirstSettings :: IORef Bool
peerSettings :: IORef Settings
oddStreamTable :: TVar OddStreamTable
evenStreamTable :: TVar EvenStreamTable
continued :: IORef (Maybe HeaderContinuation)
myStreamId :: TVar Int
peerStreamId :: IORef Int
peerLastStreamId :: TVar Int
outputBufferLimit :: IORef Int
outputQ :: TQueue Output
outputQStreamID :: TVar Int
controlQ :: ControlQ
encodeDynamicTable :: DynamicTable
decodeDynamicTable :: DynamicTable
txFlow :: TVar TxFlow
rxFlow :: IORef RxFlow
pingRate :: Rate
settingsRate :: Rate
emptyFrameRate :: Rate
rstRate :: Rate
mySockAddr :: SockAddr
peerSockAddr :: SockAddr
threadManager :: ThreadManager
timeoutManager :: Manager
receiverDone :: TVar (Maybe SomeException)
stopReceiving :: TVar Bool
receiverStoppable :: Bool
appDone :: STM Bool
informationalCallback :: Int -> TokenHeaderTable -> IO ()
peerGoAway :: TVar (Maybe Int)
myGoAway :: TVar (Maybe Int)
pingAcked :: TVar ByteString
activeRequests :: TVar Int
resetByMe :: IORef IntSet
..} = do
n <- TVar Int -> STM Int
forall a. TVar a -> STM a
readTVar TVar Int
myStreamId
let n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2
writeTVar myStreamId n'
return n
getPeerStreamID :: Context -> IO StreamId
getPeerStreamID :: Context -> IO Int
getPeerStreamID Context
ctx = IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (IORef Int -> IO Int) -> IORef Int -> IO Int
forall a b. (a -> b) -> a -> b
$ Context -> IORef Int
peerStreamId Context
ctx
setPeerStreamID :: Context -> StreamId -> IO ()
setPeerStreamID :: Context -> Int -> IO ()
setPeerStreamID Context
ctx Int
sid = IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Context -> IORef Int
peerStreamId Context
ctx) Int
sid
getPeerLastStreamId :: Context -> IO StreamId
getPeerLastStreamId :: Context -> IO Int
getPeerLastStreamId Context
ctx = TVar Int -> IO Int
forall a. TVar a -> IO a
readTVarIO (TVar Int -> IO Int) -> TVar Int -> IO Int
forall a b. (a -> b) -> a -> b
$ Context -> TVar Int
peerLastStreamId Context
ctx
admitPeerStream :: Context -> StreamId -> IO Bool
admitPeerStream :: Context -> Int -> IO Bool
admitPeerStream Context{TVar Int
peerLastStreamId :: Context -> TVar Int
peerLastStreamId :: TVar Int
peerLastStreamId, TVar (Maybe Int)
myGoAway :: Context -> TVar (Maybe Int)
myGoAway :: TVar (Maybe Int)
myGoAway} Int
sid = STM Bool -> IO Bool
forall a. STM a -> IO a
atomically (STM Bool -> IO Bool) -> STM Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ do
mlast <- TVar (Maybe Int) -> STM (Maybe Int)
forall a. TVar a -> STM a
readTVar TVar (Maybe Int)
myGoAway
case mlast of
Just Int
lastSid | Int
sid Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
lastSid -> Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Maybe Int
_ -> do
TVar Int -> (Int -> Int) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar Int
peerLastStreamId ((Int -> Int) -> STM ()) -> (Int -> Int) -> STM ()
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
sid
Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
{-# INLINE setStreamState #-}
setStreamState :: Context -> Stream -> StreamState -> IO ()
setStreamState :: Context -> Stream -> StreamState -> IO ()
setStreamState Context
_ Stream{Int
streamNumber :: Int
streamNumber :: Stream -> Int
streamNumber, TVar StreamState
streamState :: TVar StreamState
streamState :: Stream -> TVar StreamState
streamState} StreamState
newState = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
oldState <- TVar StreamState -> STM StreamState
forall a. TVar a -> STM a
readTVar TVar StreamState
streamState
informReplaced streamNumber oldState newState
writeTVar streamState newState
setOpenState :: Context -> Stream -> OpenState -> IO ()
setOpenState :: Context -> Stream -> OpenState -> IO ()
setOpenState Context
_ Stream{Int
streamNumber :: Stream -> Int
streamNumber :: Int
streamNumber, TVar StreamState
streamState :: Stream -> TVar StreamState
streamState :: TVar StreamState
streamState} OpenState
o = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
oldState <- TVar StreamState -> STM StreamState
forall a. TVar a -> STM a
readTVar TVar StreamState
streamState
case oldState of
Open Maybe ClosedCode
hcl OpenState
_ -> do
let newState :: StreamState
newState = Maybe ClosedCode -> OpenState -> StreamState
Open Maybe ClosedCode
hcl OpenState
o
Int -> StreamState -> StreamState -> STM ()
informReplaced Int
streamNumber StreamState
oldState StreamState
newState
TVar StreamState -> StreamState -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar StreamState
streamState StreamState
newState
StreamState
_otherwise -> () -> STM ()
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
informReplaced :: StreamId -> StreamState -> StreamState -> STM ()
informReplaced :: Int -> StreamState -> StreamState -> STM ()
informReplaced Int
streamNumber StreamState
oldState StreamState
newState =
case (StreamState
oldState, StreamState
newState) of
(Open Maybe ClosedCode
_ (Body TQueue (Either SomeException (ByteString, Bool))
q Maybe Int
_ IORef Int
_ IORef (Maybe TokenHeaderTable)
_), Open Maybe ClosedCode
_ (Body TQueue (Either SomeException (ByteString, Bool))
q' Maybe Int
_ IORef Int
_ IORef (Maybe TokenHeaderTable)
_))
| TQueue (Either SomeException (ByteString, Bool))
q TQueue (Either SomeException (ByteString, Bool))
-> TQueue (Either SomeException (ByteString, Bool)) -> Bool
forall a. Eq a => a -> a -> Bool
== TQueue (Either SomeException (ByteString, Bool))
q' ->
() -> STM ()
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
(Open Maybe ClosedCode
_ (Body TQueue (Either SomeException (ByteString, Bool))
q Maybe Int
_ IORef Int
_ IORef (Maybe TokenHeaderTable)
_), Closed ClosedCode
cc) ->
TQueue (Either SomeException (ByteString, Bool))
-> Either SomeException (ByteString, Bool) -> STM ()
forall a. TQueue a -> a -> STM ()
writeTQueue TQueue (Either SomeException (ByteString, Bool))
q (Either SomeException (ByteString, Bool) -> STM ())
-> Either SomeException (ByteString, Bool) -> STM ()
forall a b. (a -> b) -> a -> b
$ SomeException -> Either SomeException (ByteString, Bool)
forall a b. a -> Either a b
Left (SomeException -> Either SomeException (ByteString, Bool))
-> SomeException -> Either SomeException (ByteString, Bool)
forall a b. (a -> b) -> a -> b
$ HTTP2Error -> SomeException
forall e. Exception e => e -> SomeException
E.toException (HTTP2Error -> SomeException) -> HTTP2Error -> SomeException
forall a b. (a -> b) -> a -> b
$ Int -> ClosedCode -> HTTP2Error
closedCodeToError Int
streamNumber ClosedCode
cc
(Open Maybe ClosedCode
_ (Body TQueue (Either SomeException (ByteString, Bool))
q Maybe Int
_ IORef Int
_ IORef (Maybe TokenHeaderTable)
_), StreamState
_) ->
TQueue (Either SomeException (ByteString, Bool))
-> Either SomeException (ByteString, Bool) -> STM ()
forall a. TQueue a -> a -> STM ()
writeTQueue TQueue (Either SomeException (ByteString, Bool))
q (Either SomeException (ByteString, Bool) -> STM ())
-> Either SomeException (ByteString, Bool) -> STM ()
forall a b. (a -> b) -> a -> b
$ SomeException -> Either SomeException (ByteString, Bool)
forall a b. a -> Either a b
Left (SomeException -> Either SomeException (ByteString, Bool))
-> SomeException -> Either SomeException (ByteString, Bool)
forall a b. (a -> b) -> a -> b
$ HTTP2Error -> SomeException
forall e. Exception e => e -> SomeException
E.toException HTTP2Error
ConnectionIsClosed
(StreamState, StreamState)
_otherwise ->
() -> STM ()
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
opened :: Context -> Stream -> IO ()
opened :: Context -> Stream -> IO ()
opened Context
_ Stream{TVar StreamState
streamState :: Stream -> TVar StreamState
streamState :: TVar StreamState
streamState} = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar StreamState -> (StreamState -> StreamState) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar StreamState
streamState StreamState -> StreamState
open
where
open :: StreamState -> StreamState
open StreamState
Idle = Maybe ClosedCode -> OpenState -> StreamState
Open Maybe ClosedCode
forall a. Maybe a
Nothing OpenState
JustOpened
open StreamState
st = StreamState
st
halfClosedRemote :: Context -> Stream -> IO ()
halfClosedRemote :: Context -> Stream -> IO ()
halfClosedRemote Context
ctx Stream
stream = Context -> Stream -> STM () -> IO ()
forall a. Context -> Stream -> STM a -> IO a
halfClosedRemoteWith Context
ctx Stream
stream (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> STM ()
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
halfClosedRemoteWith :: Context -> Stream -> STM a -> IO a
halfClosedRemoteWith :: forall a. Context -> Stream -> STM a -> IO a
halfClosedRemoteWith Context
ctx stream :: Stream
stream@Stream{TVar StreamState
streamState :: Stream -> TVar StreamState
streamState :: TVar StreamState
streamState} STM a
deliver = do
(a, closingCode) <- STM (a, Maybe ClosedCode) -> IO (a, Maybe ClosedCode)
forall a. STM a -> IO a
atomically (STM (a, Maybe ClosedCode) -> IO (a, Maybe ClosedCode))
-> STM (a, Maybe ClosedCode) -> IO (a, Maybe ClosedCode)
forall a b. (a -> b) -> a -> b
$ do
a <- STM a
deliver
closingCode <- stateTVar streamState closeHalf
return (a, closingCode)
traverse_ (closed ctx stream) closingCode
return a
where
closeHalf :: StreamState -> (Maybe ClosedCode, StreamState)
closeHalf :: StreamState -> (Maybe ClosedCode, StreamState)
closeHalf x :: StreamState
x@(Closed ClosedCode
_) = (Maybe ClosedCode
forall a. Maybe a
Nothing, StreamState
x)
closeHalf (Open (Just ClosedCode
cc) OpenState
_) = (ClosedCode -> Maybe ClosedCode
forall a. a -> Maybe a
Just ClosedCode
cc, ClosedCode -> StreamState
Closed ClosedCode
cc)
closeHalf StreamState
_ = (Maybe ClosedCode
forall a. Maybe a
Nothing, StreamState
HalfClosedRemote)
halfClosedLocal :: Context -> Stream -> ClosedCode -> IO ()
halfClosedLocal :: Context -> Stream -> ClosedCode -> IO ()
halfClosedLocal Context
ctx stream :: Stream
stream@Stream{TVar StreamState
streamState :: Stream -> TVar StreamState
streamState :: TVar StreamState
streamState} ClosedCode
cc = do
shouldFinalize <- STM Bool -> IO Bool
forall a. STM a -> IO a
atomically (STM Bool -> IO Bool) -> STM Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ TVar StreamState
-> (StreamState -> (Bool, StreamState)) -> STM Bool
forall s a. TVar s -> (s -> (a, s)) -> STM a
stateTVar TVar StreamState
streamState StreamState -> (Bool, StreamState)
closeHalf
when shouldFinalize $
closed ctx stream cc
where
closeHalf :: StreamState -> (Bool, StreamState)
closeHalf :: StreamState -> (Bool, StreamState)
closeHalf x :: StreamState
x@(Closed ClosedCode
_) = (Bool
False, StreamState
x)
closeHalf StreamState
HalfClosedRemote = (Bool
True, ClosedCode -> StreamState
Closed ClosedCode
cc)
closeHalf (Open Maybe ClosedCode
Nothing OpenState
o) = (Bool
False, Maybe ClosedCode -> OpenState -> StreamState
Open (ClosedCode -> Maybe ClosedCode
forall a. a -> Maybe a
Just ClosedCode
cc) OpenState
o)
closeHalf StreamState
_ = (Bool
False, Maybe ClosedCode -> OpenState -> StreamState
Open (ClosedCode -> Maybe ClosedCode
forall a. a -> Maybe a
Just ClosedCode
cc) OpenState
JustOpened)
closeIfReceiving :: Context -> Stream -> ClosedCode -> IO Bool
closeIfReceiving :: Context -> Stream -> ClosedCode -> IO Bool
closeIfReceiving Context
ctx strm :: Stream
strm@Stream{Int
streamNumber :: Stream -> Int
streamNumber :: Int
streamNumber, TVar StreamState
streamState :: Stream -> TVar StreamState
streamState :: TVar StreamState
streamState} ClosedCode
cc = do
receiving <- STM Bool -> IO Bool
forall a. STM a -> IO a
atomically (STM Bool -> IO Bool) -> STM Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ do
st <- TVar StreamState -> STM StreamState
forall a. TVar a -> STM a
readTVar TVar StreamState
streamState
case st of
Open Maybe ClosedCode
_ (NoBody TokenHeaderTable
_) -> Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Open{} -> do
Int -> StreamState -> StreamState -> STM ()
informReplaced Int
streamNumber StreamState
st (ClosedCode -> StreamState
Closed ClosedCode
cc)
TVar StreamState -> StreamState -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar StreamState
streamState (ClosedCode -> StreamState
Closed ClosedCode
cc)
Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
StreamState
_otherwise -> Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
when receiving $ closed ctx strm cc
return receiving
closed :: Context -> Stream -> ClosedCode -> IO ()
closed :: Context -> Stream -> ClosedCode -> IO ()
closed ctx :: Context
ctx@Context{TVar OddStreamTable
oddStreamTable :: Context -> TVar OddStreamTable
oddStreamTable :: TVar OddStreamTable
oddStreamTable, TVar EvenStreamTable
evenStreamTable :: Context -> TVar EvenStreamTable
evenStreamTable :: TVar EvenStreamTable
evenStreamTable} strm :: Stream
strm@Stream{Int
streamNumber :: Stream -> Int
streamNumber :: Int
streamNumber} ClosedCode
cc = do
if Int -> Bool
isServerInitiated Int
streamNumber
then TVar EvenStreamTable -> Int -> SomeException -> IO ()
deleteEven TVar EvenStreamTable
evenStreamTable Int
streamNumber SomeException
err
else TVar OddStreamTable -> Int -> SomeException -> IO ()
deleteOdd TVar OddStreamTable
oddStreamTable Int
streamNumber SomeException
err
Context -> Stream -> StreamState -> IO ()
setStreamState Context
ctx Stream
strm (ClosedCode -> StreamState
Closed ClosedCode
cc)
case ClosedCode
cc of
ResetByMe SomeException
_
| Context -> Bool
isServer Context
ctx Bool -> Bool -> Bool
&& Bool -> Bool
not (Int -> Bool
isServerInitiated Int
streamNumber) ->
Context -> Int -> IO ()
rememberReset Context
ctx Int
streamNumber
ClosedCode
_otherwise -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
err :: E.SomeException
err :: SomeException
err = HTTP2Error -> SomeException
forall e. Exception e => e -> SomeException
E.toException (Int -> ClosedCode -> HTTP2Error
closedCodeToError Int
streamNumber ClosedCode
cc)
rememberReset :: Context -> StreamId -> IO ()
rememberReset :: Context -> Int -> IO ()
rememberReset Context{IORef IntSet
resetByMe :: Context -> IORef IntSet
resetByMe :: IORef IntSet
resetByMe} Int
sid =
IORef IntSet -> (IntSet -> (IntSet, ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef IntSet
resetByMe ((IntSet -> (IntSet, ())) -> IO ())
-> (IntSet -> (IntSet, ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \IntSet
s ->
let s' :: IntSet
s' = Int -> IntSet -> IntSet
IntSet.insert Int
sid IntSet
s
in (if IntSet -> Int
IntSet.size IntSet
s' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
resetByMeLimit then IntSet -> IntSet
IntSet.deleteMin IntSet
s' else IntSet
s', ())
wasResetByMe :: Context -> StreamId -> IO Bool
wasResetByMe :: Context -> Int -> IO Bool
wasResetByMe Context{IORef IntSet
resetByMe :: Context -> IORef IntSet
resetByMe :: IORef IntSet
resetByMe} Int
sid = Int -> IntSet -> Bool
IntSet.member Int
sid (IntSet -> Bool) -> IO IntSet -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IORef IntSet -> IO IntSet
forall a. IORef a -> IO a
readIORef IORef IntSet
resetByMe
resetByMeLimit :: Int
resetByMeLimit :: Int
resetByMeLimit = Int
1000
openOddStreamCheck :: Context -> StreamId -> FrameType -> IO Stream
openOddStreamCheck :: Context -> Int -> FrameType -> IO Stream
openOddStreamCheck ctx :: Context
ctx@Context{TVar OddStreamTable
oddStreamTable :: Context -> TVar OddStreamTable
oddStreamTable :: TVar OddStreamTable
oddStreamTable, IORef Settings
peerSettings :: Context -> IORef Settings
peerSettings :: IORef Settings
peerSettings, Settings
mySettings :: Context -> Settings
mySettings :: Settings
mySettings} Int
sid FrameType
ftyp = do
txws <- Settings -> Int
initialWindowSize (Settings -> Int) -> IO Settings -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IORef Settings -> IO Settings
forall a. IORef a -> IO a
readIORef IORef Settings
peerSettings
let rxws = Settings -> Int
initialWindowSize Settings
mySettings
newstrm <- newOddStream sid txws rxws
when (ftyp == FrameHeaders || ftyp == FramePushPromise) $ opened ctx newstrm
insertOdd oddStreamTable sid newstrm
return newstrm
openEvenStreamCacheCheck :: Context -> StreamId -> Method -> ByteString -> IO ()
openEvenStreamCacheCheck :: Context -> Int -> ByteString -> ByteString -> IO ()
openEvenStreamCacheCheck Context{TVar EvenStreamTable
evenStreamTable :: Context -> TVar EvenStreamTable
evenStreamTable :: TVar EvenStreamTable
evenStreamTable, IORef Settings
peerSettings :: Context -> IORef Settings
peerSettings :: IORef Settings
peerSettings, Settings
mySettings :: Context -> Settings
mySettings :: Settings
mySettings} Int
sid ByteString
method ByteString
path = do
conc <- TVar EvenStreamTable -> IO Int
getEvenConcurrency TVar EvenStreamTable
evenStreamTable
checkMyConcurrency sid mySettings (conc + 1)
txws <- initialWindowSize <$> readIORef peerSettings
let rxws = Settings -> Int
initialWindowSize Settings
mySettings
newstrm <- newEvenStream sid txws rxws
insertEvenCache evenStreamTable method path newstrm
checkOddConcurrency :: Context -> StreamId -> IO ()
checkOddConcurrency :: Context -> Int -> IO ()
checkOddConcurrency Context{TVar OddStreamTable
oddStreamTable :: Context -> TVar OddStreamTable
oddStreamTable :: TVar OddStreamTable
oddStreamTable, Settings
mySettings :: Context -> Settings
mySettings :: Settings
mySettings} Int
sid = do
conc <- TVar OddStreamTable -> IO Int
getOddConcurrency TVar OddStreamTable
oddStreamTable
checkMyConcurrency sid mySettings conc
checkMyConcurrency
:: StreamId -> Settings -> Int -> IO ()
checkMyConcurrency :: Int -> Settings -> Int -> IO ()
checkMyConcurrency Int
sid Settings
settings Int
conc = do
let mMaxConc :: Maybe Int
mMaxConc = Settings -> Maybe Int
maxConcurrentStreams Settings
settings
case Maybe Int
mMaxConc of
Maybe Int
Nothing -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Int
maxConc ->
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
conc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxConc) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
HTTP2Error -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO (HTTP2Error -> IO ()) -> HTTP2Error -> IO ()
forall a b. (a -> b) -> a -> b
$
ErrorCode -> Int -> ShortByteString -> HTTP2Error
StreamErrorIsSent ErrorCode
RefusedStream Int
sid ShortByteString
"exceeds max concurrent"
openOddStreamWait :: Context -> IO (StreamId, Stream)
openOddStreamWait :: Context -> IO (Int, Stream)
openOddStreamWait ctx :: Context
ctx@Context{TVar OddStreamTable
oddStreamTable :: Context -> TVar OddStreamTable
oddStreamTable :: TVar OddStreamTable
oddStreamTable, Settings
mySettings :: Context -> Settings
mySettings :: Settings
mySettings, IORef Settings
peerSettings :: Context -> IORef Settings
peerSettings :: IORef Settings
peerSettings} = do
mMaxConc <- Settings -> Maybe Int
maxConcurrentStreams (Settings -> Maybe Int) -> IO Settings -> IO (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IORef Settings -> IO Settings
forall a. IORef a -> IO a
readIORef IORef Settings
peerSettings
let rxws = Settings -> Int
initialWindowSize Settings
mySettings
case mMaxConc of
Maybe Int
Nothing -> do
sid <- STM Int -> IO Int
forall a. STM a -> IO a
atomically (STM Int -> IO Int) -> STM Int -> IO Int
forall a b. (a -> b) -> a -> b
$ do
Context -> STM ()
refuseIfGoingAway Context
ctx
Context -> STM Int
getMyNewStreamId Context
ctx
txws <- initialWindowSize <$> readIORef peerSettings
newstrm <- newOddStream sid txws rxws
insertOdd oddStreamTable sid newstrm
return (sid, newstrm)
Just Int
maxConc -> do
sid <- STM Int -> IO Int
forall a. STM a -> IO a
atomically (STM Int -> IO Int) -> STM Int -> IO Int
forall a b. (a -> b) -> a -> b
$ do
Context -> STM ()
refuseIfGoingAway Context
ctx
TVar OddStreamTable -> Int -> STM ()
waitIncOdd TVar OddStreamTable
oddStreamTable Int
maxConc
Context -> STM Int
getMyNewStreamId Context
ctx
txws <- initialWindowSize <$> readIORef peerSettings
newstrm <- newOddStream sid txws rxws
insertOdd' oddStreamTable sid newstrm
return (sid, newstrm)
openEvenStreamTry :: Context -> IO (Maybe (StreamId, Stream))
openEvenStreamTry :: Context -> IO (Maybe (Int, Stream))
openEvenStreamTry ctx :: Context
ctx@Context{Bool
STM Bool
TVar Bool
TVar Int
TVar (Maybe Int)
TVar (Maybe SomeException)
TVar ByteString
TVar TxFlow
TVar EvenStreamTable
TVar OddStreamTable
IORef Bool
IORef Int
IORef (Maybe HeaderContinuation)
IORef RxFlow
IORef IntSet
IORef Settings
DynamicTable
Rate
Settings
ControlQ
Manager
SockAddr
TQueue Output
Role
RoleInfo
ThreadManager
Int -> TokenHeaderTable -> IO ()
role :: Context -> Role
roleInfo :: Context -> RoleInfo
mySettings :: Context -> Settings
myFirstSettings :: Context -> IORef Bool
peerSettings :: Context -> IORef Settings
oddStreamTable :: Context -> TVar OddStreamTable
evenStreamTable :: Context -> TVar EvenStreamTable
continued :: Context -> IORef (Maybe HeaderContinuation)
myStreamId :: Context -> TVar Int
peerStreamId :: Context -> IORef Int
peerLastStreamId :: Context -> TVar Int
outputBufferLimit :: Context -> IORef Int
outputQ :: Context -> TQueue Output
outputQStreamID :: Context -> TVar Int
controlQ :: Context -> ControlQ
encodeDynamicTable :: Context -> DynamicTable
decodeDynamicTable :: Context -> DynamicTable
txFlow :: Context -> TVar TxFlow
rxFlow :: Context -> IORef RxFlow
pingRate :: Context -> Rate
settingsRate :: Context -> Rate
emptyFrameRate :: Context -> Rate
rstRate :: Context -> Rate
mySockAddr :: Context -> SockAddr
peerSockAddr :: Context -> SockAddr
threadManager :: Context -> ThreadManager
timeoutManager :: Context -> Manager
receiverDone :: Context -> TVar (Maybe SomeException)
stopReceiving :: Context -> TVar Bool
receiverStoppable :: Context -> Bool
appDone :: Context -> STM Bool
informationalCallback :: Context -> Int -> TokenHeaderTable -> IO ()
peerGoAway :: Context -> TVar (Maybe Int)
myGoAway :: Context -> TVar (Maybe Int)
pingAcked :: Context -> TVar ByteString
activeRequests :: Context -> TVar Int
resetByMe :: Context -> IORef IntSet
role :: Role
roleInfo :: RoleInfo
mySettings :: Settings
myFirstSettings :: IORef Bool
peerSettings :: IORef Settings
oddStreamTable :: TVar OddStreamTable
evenStreamTable :: TVar EvenStreamTable
continued :: IORef (Maybe HeaderContinuation)
myStreamId :: TVar Int
peerStreamId :: IORef Int
peerLastStreamId :: TVar Int
outputBufferLimit :: IORef Int
outputQ :: TQueue Output
outputQStreamID :: TVar Int
controlQ :: ControlQ
encodeDynamicTable :: DynamicTable
decodeDynamicTable :: DynamicTable
txFlow :: TVar TxFlow
rxFlow :: IORef RxFlow
pingRate :: Rate
settingsRate :: Rate
emptyFrameRate :: Rate
rstRate :: Rate
mySockAddr :: SockAddr
peerSockAddr :: SockAddr
threadManager :: ThreadManager
timeoutManager :: Manager
receiverDone :: TVar (Maybe SomeException)
stopReceiving :: TVar Bool
receiverStoppable :: Bool
appDone :: STM Bool
informationalCallback :: Int -> TokenHeaderTable -> IO ()
peerGoAway :: TVar (Maybe Int)
myGoAway :: TVar (Maybe Int)
pingAcked :: TVar ByteString
activeRequests :: TVar Int
resetByMe :: IORef IntSet
..} = do
goingaway <- Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Int -> Bool) -> IO (Maybe Int) -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar (Maybe Int) -> IO (Maybe Int)
forall a. TVar a -> IO a
readTVarIO TVar (Maybe Int)
peerGoAway
if goingaway then return Nothing else openEvenStreamTry' ctx
openEvenStreamTry' :: Context -> IO (Maybe (StreamId, Stream))
openEvenStreamTry' :: Context -> IO (Maybe (Int, Stream))
openEvenStreamTry' ctx :: Context
ctx@Context{Bool
STM Bool
TVar Bool
TVar Int
TVar (Maybe Int)
TVar (Maybe SomeException)
TVar ByteString
TVar TxFlow
TVar EvenStreamTable
TVar OddStreamTable
IORef Bool
IORef Int
IORef (Maybe HeaderContinuation)
IORef RxFlow
IORef IntSet
IORef Settings
DynamicTable
Rate
Settings
ControlQ
Manager
SockAddr
TQueue Output
Role
RoleInfo
ThreadManager
Int -> TokenHeaderTable -> IO ()
role :: Context -> Role
roleInfo :: Context -> RoleInfo
mySettings :: Context -> Settings
myFirstSettings :: Context -> IORef Bool
peerSettings :: Context -> IORef Settings
oddStreamTable :: Context -> TVar OddStreamTable
evenStreamTable :: Context -> TVar EvenStreamTable
continued :: Context -> IORef (Maybe HeaderContinuation)
myStreamId :: Context -> TVar Int
peerStreamId :: Context -> IORef Int
peerLastStreamId :: Context -> TVar Int
outputBufferLimit :: Context -> IORef Int
outputQ :: Context -> TQueue Output
outputQStreamID :: Context -> TVar Int
controlQ :: Context -> ControlQ
encodeDynamicTable :: Context -> DynamicTable
decodeDynamicTable :: Context -> DynamicTable
txFlow :: Context -> TVar TxFlow
rxFlow :: Context -> IORef RxFlow
pingRate :: Context -> Rate
settingsRate :: Context -> Rate
emptyFrameRate :: Context -> Rate
rstRate :: Context -> Rate
mySockAddr :: Context -> SockAddr
peerSockAddr :: Context -> SockAddr
threadManager :: Context -> ThreadManager
timeoutManager :: Context -> Manager
receiverDone :: Context -> TVar (Maybe SomeException)
stopReceiving :: Context -> TVar Bool
receiverStoppable :: Context -> Bool
appDone :: Context -> STM Bool
informationalCallback :: Context -> Int -> TokenHeaderTable -> IO ()
peerGoAway :: Context -> TVar (Maybe Int)
myGoAway :: Context -> TVar (Maybe Int)
pingAcked :: Context -> TVar ByteString
activeRequests :: Context -> TVar Int
resetByMe :: Context -> IORef IntSet
role :: Role
roleInfo :: RoleInfo
mySettings :: Settings
myFirstSettings :: IORef Bool
peerSettings :: IORef Settings
oddStreamTable :: TVar OddStreamTable
evenStreamTable :: TVar EvenStreamTable
continued :: IORef (Maybe HeaderContinuation)
myStreamId :: TVar Int
peerStreamId :: IORef Int
peerLastStreamId :: TVar Int
outputBufferLimit :: IORef Int
outputQ :: TQueue Output
outputQStreamID :: TVar Int
controlQ :: ControlQ
encodeDynamicTable :: DynamicTable
decodeDynamicTable :: DynamicTable
txFlow :: TVar TxFlow
rxFlow :: IORef RxFlow
pingRate :: Rate
settingsRate :: Rate
emptyFrameRate :: Rate
rstRate :: Rate
mySockAddr :: SockAddr
peerSockAddr :: SockAddr
threadManager :: ThreadManager
timeoutManager :: Manager
receiverDone :: TVar (Maybe SomeException)
stopReceiving :: TVar Bool
receiverStoppable :: Bool
appDone :: STM Bool
informationalCallback :: Int -> TokenHeaderTable -> IO ()
peerGoAway :: TVar (Maybe Int)
myGoAway :: TVar (Maybe Int)
pingAcked :: TVar ByteString
activeRequests :: TVar Int
resetByMe :: IORef IntSet
..} = do
mMaxConc <- Settings -> Maybe Int
maxConcurrentStreams (Settings -> Maybe Int) -> IO Settings -> IO (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IORef Settings -> IO Settings
forall a. IORef a -> IO a
readIORef IORef Settings
peerSettings
let rxws = Settings -> Int
initialWindowSize Settings
mySettings
case mMaxConc of
Maybe Int
Nothing -> do
sid <- STM Int -> IO Int
forall a. STM a -> IO a
atomically (STM Int -> IO Int) -> STM Int -> IO Int
forall a b. (a -> b) -> a -> b
$ Context -> STM Int
getMyNewStreamId Context
ctx
txws <- initialWindowSize <$> readIORef peerSettings
newstrm <- newEvenStream sid txws rxws
insertEven evenStreamTable sid newstrm
return $ Just (sid, newstrm)
Just Int
maxConc -> do
msid <- STM (Maybe Int) -> IO (Maybe Int)
forall a. STM a -> IO a
atomically (STM (Maybe Int) -> IO (Maybe Int))
-> STM (Maybe Int) -> IO (Maybe Int)
forall a b. (a -> b) -> a -> b
$ do
let open :: STM (Maybe Int)
open = do
TVar EvenStreamTable -> Int -> STM ()
waitIncEven TVar EvenStreamTable
evenStreamTable Int
maxConc
Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> STM Int -> STM (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> STM Int
getMyNewStreamId Context
ctx
STM (Maybe Int)
open STM (Maybe Int) -> STM (Maybe Int) -> STM (Maybe Int)
forall a. STM a -> STM a -> STM a
`orElse` Maybe Int -> STM (Maybe Int)
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Int
forall a. Maybe a
Nothing
forM msid $ \Int
sid -> do
txws <- Settings -> Int
initialWindowSize (Settings -> Int) -> IO Settings -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IORef Settings -> IO Settings
forall a. IORef a -> IO a
readIORef IORef Settings
peerSettings
newstrm <- newEvenStream sid txws rxws
insertEven' evenStreamTable sid newstrm
return (sid, newstrm)
refuseIfGoingAway :: Context -> STM ()
refuseIfGoingAway :: Context -> STM ()
refuseIfGoingAway Context{TVar (Maybe Int)
peerGoAway :: Context -> TVar (Maybe Int)
peerGoAway :: TVar (Maybe Int)
peerGoAway} = do
goingaway <- Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Int -> Bool) -> STM (Maybe Int) -> STM Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar (Maybe Int) -> STM (Maybe Int)
forall a. TVar a -> STM a
readTVar TVar (Maybe Int)
peerGoAway
when goingaway $ throwSTM ConnectionIsClosed
goingAway :: Context -> StreamId -> IO Bool
goingAway :: Context -> Int -> IO Bool
goingAway ctx :: Context
ctx@Context{TVar (Maybe Int)
peerGoAway :: Context -> TVar (Maybe Int)
peerGoAway :: TVar (Maybe Int)
peerGoAway, TVar OddStreamTable
oddStreamTable :: Context -> TVar OddStreamTable
oddStreamTable :: TVar OddStreamTable
oddStreamTable, TVar EvenStreamTable
evenStreamTable :: Context -> TVar EvenStreamTable
evenStreamTable :: TVar EvenStreamTable
evenStreamTable} Int
lastSid = do
first <- STM Bool -> IO Bool
forall a. STM a -> IO a
atomically (STM Bool -> IO Bool) -> STM Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ do
old <- TVar (Maybe Int) -> STM (Maybe Int)
forall a. TVar a -> STM a
readTVar TVar (Maybe Int)
peerGoAway
writeTVar peerGoAway $ Just $ maybe lastSid (min lastSid) old
return $ isNothing old
mine <-
if isClient ctx
then getOddStreams oddStreamTable
else getEvenStreams evenStreamTable
let (_, above) = IntMap.split lastSid mine
forM_ above $ \Stream
strm -> Context -> Stream -> ClosedCode -> IO ()
closed Context
ctx Stream
strm ClosedCode
Finished
return first
drained :: Context -> STM ()
drained :: Context -> STM ()
drained ctx :: Context
ctx@Context{TVar (Maybe Int)
peerGoAway :: Context -> TVar (Maybe Int)
peerGoAway :: TVar (Maybe Int)
peerGoAway, TVar OddStreamTable
oddStreamTable :: Context -> TVar OddStreamTable
oddStreamTable :: TVar OddStreamTable
oddStreamTable, TVar EvenStreamTable
evenStreamTable :: Context -> TVar EvenStreamTable
evenStreamTable :: TVar EvenStreamTable
evenStreamTable, TVar Int
activeRequests :: Context -> TVar Int
activeRequests :: TVar Int
activeRequests} = do
mlast <- TVar (Maybe Int) -> STM (Maybe Int)
forall a. TVar a -> STM a
readTVar TVar (Maybe Int)
peerGoAway
case mlast of
Maybe Int
Nothing -> STM ()
forall a. STM a
retry
Just Int
lastSid -> do
odds <- OddStreamTable -> IntMap Stream
oddTable (OddStreamTable -> IntMap Stream)
-> STM OddStreamTable -> STM (IntMap Stream)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar OddStreamTable -> STM OddStreamTable
forall a. TVar a -> STM a
readTVar TVar OddStreamTable
oddStreamTable
evens <- evenTable <$> readTVar evenStreamTable
active <- readTVar activeRequests
let (mine, theirs)
| isClient ctx = (odds, evens)
| otherwise = (evens, odds)
noneOfMine = Bool -> ((Int, Stream) -> Bool) -> Maybe (Int, Stream) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True ((Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
lastSid) (Int -> Bool) -> ((Int, Stream) -> Int) -> (Int, Stream) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, Stream) -> Int
forall a b. (a, b) -> a
fst) (Maybe (Int, Stream) -> Bool) -> Maybe (Int, Stream) -> Bool
forall a b. (a -> b) -> a -> b
$ IntMap Stream -> Maybe (Int, Stream)
forall a. IntMap a -> Maybe (Int, a)
IntMap.lookupMin IntMap Stream
mine
check $ IntMap.null theirs && noneOfMine && active == 0
goingAwayMine :: Context -> STM (Maybe StreamId)
goingAwayMine :: Context -> STM (Maybe Int)
goingAwayMine Context{TVar Int
peerLastStreamId :: Context -> TVar Int
peerLastStreamId :: TVar Int
peerLastStreamId, TVar (Maybe Int)
myGoAway :: Context -> TVar (Maybe Int)
myGoAway :: TVar (Maybe Int)
myGoAway} = do
old <- TVar (Maybe Int) -> STM (Maybe Int)
forall a. TVar a -> STM a
readTVar TVar (Maybe Int)
myGoAway
case old of
Just Int
_ -> Maybe Int -> STM (Maybe Int)
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Int
forall a. Maybe a
Nothing
Maybe Int
Nothing -> do
lastSid <- TVar Int -> STM Int
forall a. TVar a -> STM a
readTVar TVar Int
peerLastStreamId
writeTVar myGoAway $ Just lastSid
return $ Just lastSid
drainedMine :: Context -> STM ()
drainedMine :: Context -> STM ()
drainedMine Context{TVar (Maybe Int)
myGoAway :: Context -> TVar (Maybe Int)
myGoAway :: TVar (Maybe Int)
myGoAway, TVar OddStreamTable
oddStreamTable :: Context -> TVar OddStreamTable
oddStreamTable :: TVar OddStreamTable
oddStreamTable} = do
TVar (Maybe Int) -> STM (Maybe Int)
forall a. TVar a -> STM a
readTVar TVar (Maybe Int)
myGoAway STM (Maybe Int) -> (Maybe Int -> STM ()) -> STM ()
forall a b. STM a -> (a -> STM b) -> STM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> STM ()
check (Bool -> STM ()) -> (Maybe Int -> Bool) -> Maybe Int -> STM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust
odds <- OddStreamTable -> IntMap Stream
oddTable (OddStreamTable -> IntMap Stream)
-> STM OddStreamTable -> STM (IntMap Stream)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar OddStreamTable -> STM OddStreamTable
forall a. TVar a -> STM a
readTVar TVar OddStreamTable
oddStreamTable
check $ IntMap.null odds