{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
{-# LANGUAGE RecordWildCards #-}

module Network.QPACK.Table.Dynamic where

import Control.Concurrent.STM
import Data.Array.Base (unsafeWrite, unsafeRead)
import Data.Array.MArray (newArray)
import Data.IORef
import Network.ByteOrder
import Network.HPACK.Internal
import qualified UnliftIO.Exception as E

import Imports
import Network.QPACK.Table.RevIndex
import Network.QPACK.Types

data CodeInfo =
    EncodeInfo RevIndex -- Reverse index
               (IORef InsertionPoint)
  | DecodeInfo HuffmanDecoder

-- | Dynamic table for QPACK.
data DynamicTable = DynamicTable {
    DynamicTable -> CodeInfo
codeInfo          :: CodeInfo
  , DynamicTable -> IORef AbsoluteIndex
droppingPoint     :: IORef AbsoluteIndex
  , DynamicTable -> IORef AbsoluteIndex
drainingPoint     :: IORef AbsoluteIndex
  , DynamicTable -> TVar InsertionPoint
insertionPoint    :: TVar InsertionPoint
  , DynamicTable -> IORef BasePoint
basePoint         :: IORef BasePoint
  , DynamicTable -> TVar Index
maxNumOfEntries   :: TVar Int
  , DynamicTable -> TVar Table
circularTable     :: TVar Table
  , DynamicTable -> IORef Bool
debugQPACK        :: IORef Bool
  }

type Table = TArray Index Entry

----------------------------------------------------------------

getBasePoint :: DynamicTable -> IO BasePoint
getBasePoint :: DynamicTable -> IO BasePoint
getBasePoint DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = forall a. IORef a -> IO a
readIORef IORef BasePoint
basePoint

setBasePointToInsersionPoint :: DynamicTable -> IO ()
setBasePointToInsersionPoint :: DynamicTable -> IO ()
setBasePointToInsersionPoint DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = do
    InsertionPoint Index
ip <- forall a. TVar a -> IO a
readTVarIO TVar InsertionPoint
insertionPoint
    forall a. IORef a -> a -> IO ()
writeIORef IORef BasePoint
basePoint forall a b. (a -> b) -> a -> b
$ Index -> BasePoint
BasePoint Index
ip

getInsertionPoint :: DynamicTable -> IO InsertionPoint
getInsertionPoint :: DynamicTable -> IO InsertionPoint
getInsertionPoint DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = forall a. TVar a -> IO a
readTVarIO TVar InsertionPoint
insertionPoint

getInsertionPointSTM :: DynamicTable -> STM InsertionPoint
getInsertionPointSTM :: DynamicTable -> STM InsertionPoint
getInsertionPointSTM DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = forall a. TVar a -> STM a
readTVar TVar InsertionPoint
insertionPoint

checkInsertionPoint :: DynamicTable -> InsertionPoint -> IO ()
checkInsertionPoint :: DynamicTable -> InsertionPoint -> IO ()
checkInsertionPoint DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} InsertionPoint
reqip = forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ do
    InsertionPoint
ip <- forall a. TVar a -> STM a
readTVar TVar InsertionPoint
insertionPoint
    Bool -> STM ()
check (InsertionPoint
reqip forall a. Ord a => a -> a -> Bool
<= InsertionPoint
ip)

----------------------------------------------------------------

-- | Creating 'DynamicTable' for encoding.
newDynamicTableForEncoding :: Size -- ^ The dynamic table size
                           -> IO DynamicTable
newDynamicTableForEncoding :: Index -> IO DynamicTable
newDynamicTableForEncoding Index
maxsiz = do
    RevIndex
rev <- IO RevIndex
newRevIndex
    IORef InsertionPoint
ref <- forall a. a -> IO (IORef a)
newIORef InsertionPoint
0
    let info :: CodeInfo
info = RevIndex -> IORef InsertionPoint -> CodeInfo
EncodeInfo RevIndex
rev IORef InsertionPoint
ref
    Index -> CodeInfo -> IO DynamicTable
newDynamicTable Index
maxsiz CodeInfo
info

-- | Creating 'DynamicTable' for decoding.
newDynamicTableForDecoding :: Size -- ^ The dynamic table size
                           -> Size -- ^ The size of temporary buffer for Huffman decoding
                           -> IO DynamicTable
newDynamicTableForDecoding :: Index -> Index -> IO DynamicTable
newDynamicTableForDecoding Index
maxsiz Index
huftmpsiz = do
    ForeignPtr Word8
gcbuf <- forall a. Index -> IO (ForeignPtr a)
mallocPlainForeignPtrBytes Index
huftmpsiz
    TVar (Maybe (ForeignPtr Word8, Index))
tvar <- forall a. a -> IO (TVar a)
newTVarIO forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (ForeignPtr Word8
gcbuf,Index
huftmpsiz)
    let decoder :: ReadBuffer -> Index -> IO ByteString
decoder = TVar (Maybe (ForeignPtr Word8, Index))
-> ReadBuffer -> Index -> IO ByteString
decodeHLock TVar (Maybe (ForeignPtr Word8, Index))
tvar
        info :: CodeInfo
info = (ReadBuffer -> Index -> IO ByteString) -> CodeInfo
DecodeInfo ReadBuffer -> Index -> IO ByteString
decoder
    Index -> CodeInfo -> IO DynamicTable
newDynamicTable Index
maxsiz CodeInfo
info

decodeHLock :: TVar (Maybe (GCBuffer,Int)) -> ReadBuffer -> Int -> IO ByteString
decodeHLock :: TVar (Maybe (ForeignPtr Word8, Index))
-> ReadBuffer -> Index -> IO ByteString
decodeHLock TVar (Maybe (ForeignPtr Word8, Index))
tvar ReadBuffer
rbuf Index
len = forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
E.bracket IO (ForeignPtr Word8, Index)
lock (ForeignPtr Word8, Index) -> IO ()
unlock forall a b. (a -> b) -> a -> b
$ \(ForeignPtr Word8
gcbuf,Index
bufsiz) ->
  forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
gcbuf forall a b. (a -> b) -> a -> b
$ \Ptr Word8
buf -> do
    WriteBuffer
wbuf <- Ptr Word8 -> Index -> IO WriteBuffer
newWriteBuffer Ptr Word8
buf Index
bufsiz
    WriteBuffer -> ReadBuffer -> Index -> IO ()
decH WriteBuffer
wbuf ReadBuffer
rbuf Index
len
    WriteBuffer -> IO ByteString
toByteString WriteBuffer
wbuf
  where
    lock :: IO (ForeignPtr Word8, Index)
lock = forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ do
        Maybe (ForeignPtr Word8, Index)
mx <- forall a. TVar a -> STM a
readTVar TVar (Maybe (ForeignPtr Word8, Index))
tvar
        case Maybe (ForeignPtr Word8, Index)
mx of
          Maybe (ForeignPtr Word8, Index)
Nothing -> forall a. STM a
retry
          Just (ForeignPtr Word8, Index)
x   -> do
              forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe (ForeignPtr Word8, Index))
tvar forall a. Maybe a
Nothing
              forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr Word8, Index)
x
    unlock :: (ForeignPtr Word8, Index) -> IO ()
unlock (ForeignPtr Word8, Index)
x = forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe (ForeignPtr Word8, Index))
tvar forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (ForeignPtr Word8, Index)
x

newDynamicTable :: Size -> CodeInfo -> IO DynamicTable
newDynamicTable :: Index -> CodeInfo -> IO DynamicTable
newDynamicTable Index
maxsiz CodeInfo
info = do
    Table
tbl <- forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Index
0,Index
end) Entry
dummyEntry
    CodeInfo
-> IORef AbsoluteIndex
-> IORef AbsoluteIndex
-> TVar InsertionPoint
-> IORef BasePoint
-> TVar Index
-> TVar Table
-> IORef Bool
-> DynamicTable
DynamicTable CodeInfo
info forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. a -> IO (IORef a)
newIORef AbsoluteIndex
0       -- droppingPoint
                      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. a -> IO (IORef a)
newIORef AbsoluteIndex
0       -- drainingPoint
                      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. a -> IO (TVar a)
newTVarIO InsertionPoint
0      -- insertionPoint
                      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. a -> IO (IORef a)
newIORef BasePoint
0       -- basePoint
                      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. a -> IO (TVar a)
newTVarIO Index
maxN   -- maxNumOfEntries
                      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. a -> IO (TVar a)
newTVarIO Table
tbl    -- maxDynamicTableSize
                      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. a -> IO (IORef a)
newIORef Bool
False   -- debugQPACK
  where
    maxN :: Index
maxN = Index -> Index
maxNumbers Index
maxsiz
    end :: Index
end = Index
maxN forall a. Num a => a -> a -> a
- Index
1

----------------------------------------------------------------

setDebugQPACK :: DynamicTable -> IO ()
setDebugQPACK :: DynamicTable -> IO ()
setDebugQPACK DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = forall a. IORef a -> a -> IO ()
writeIORef IORef Bool
debugQPACK Bool
True

getDebugQPACK :: DynamicTable -> IO Bool
getDebugQPACK :: DynamicTable -> IO Bool
getDebugQPACK DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = forall a. IORef a -> IO a
readIORef IORef Bool
debugQPACK

qpackDebug :: DynamicTable -> IO () -> IO ()
qpackDebug :: DynamicTable -> IO () -> IO ()
qpackDebug DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} IO ()
action = do
    Bool
debug <- forall a. IORef a -> IO a
readIORef IORef Bool
debugQPACK
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
debug IO ()
action

----------------------------------------------------------------

getMaxNumOfEntries :: DynamicTable -> IO Int
getMaxNumOfEntries :: DynamicTable -> IO Index
getMaxNumOfEntries DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = forall a. TVar a -> IO a
readTVarIO TVar Index
maxNumOfEntries

----------------------------------------------------------------

{-# INLINE getRevIndex #-}
getRevIndex :: DynamicTable-> RevIndex
getRevIndex :: DynamicTable -> RevIndex
getRevIndex DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = RevIndex
rev
  where
    EncodeInfo RevIndex
rev IORef InsertionPoint
_ = CodeInfo
codeInfo

getHuffmanDecoder :: DynamicTable -> HuffmanDecoder
getHuffmanDecoder :: DynamicTable -> ReadBuffer -> Index -> IO ByteString
getHuffmanDecoder DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = ReadBuffer -> Index -> IO ByteString
huf
  where
    DecodeInfo ReadBuffer -> Index -> IO ByteString
huf = CodeInfo
codeInfo

----------------------------------------------------------------

clearLargestReference :: DynamicTable -> IO ()
clearLargestReference :: DynamicTable -> IO ()
clearLargestReference DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = forall a. IORef a -> a -> IO ()
writeIORef IORef InsertionPoint
ref InsertionPoint
0
  where
    EncodeInfo RevIndex
_ IORef InsertionPoint
ref = CodeInfo
codeInfo

getLargestReference :: DynamicTable -> IO InsertionPoint
getLargestReference :: DynamicTable -> IO InsertionPoint
getLargestReference DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = forall a. IORef a -> IO a
readIORef IORef InsertionPoint
ref
  where
    EncodeInfo RevIndex
_ IORef InsertionPoint
ref = CodeInfo
codeInfo

updateLargestReference :: DynamicTable -> AbsoluteIndex -> IO ()
updateLargestReference :: DynamicTable -> AbsoluteIndex -> IO ()
updateLargestReference DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} (AbsoluteIndex Index
idx) = do
    let nidx :: InsertionPoint
nidx = Index -> InsertionPoint
InsertionPoint Index
idx
    InsertionPoint
oidx <- forall a. IORef a -> IO a
readIORef IORef InsertionPoint
ref
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (InsertionPoint
nidx forall a. Ord a => a -> a -> Bool
> InsertionPoint
oidx) forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> a -> IO ()
writeIORef IORef InsertionPoint
ref InsertionPoint
nidx
  where
    EncodeInfo RevIndex
_ IORef InsertionPoint
ref = CodeInfo
codeInfo

----------------------------------------------------------------

insertEntryToEncoder :: Entry -> DynamicTable -> IO AbsoluteIndex
insertEntryToEncoder :: Entry -> DynamicTable -> IO AbsoluteIndex
insertEntryToEncoder Entry
ent dyntbl :: DynamicTable
dyntbl@DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = do
    InsertionPoint Index
insp <- forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ do
        InsertionPoint
x <- forall a. TVar a -> STM a
readTVar TVar InsertionPoint
insertionPoint
        forall a. TVar a -> a -> STM ()
writeTVar TVar InsertionPoint
insertionPoint (InsertionPoint
x forall a. Num a => a -> a -> a
+ InsertionPoint
1)
        forall (m :: * -> *) a. Monad m => a -> m a
return InsertionPoint
x
    Index
maxN <- forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> STM a
readTVar TVar Index
maxNumOfEntries
    let i :: Index
i = Index
insp forall a. Integral a => a -> a -> a
`mod` Index
maxN
    Table
table <- forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> STM a
readTVar TVar Table
circularTable
    forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Index -> e -> m ()
unsafeWrite Table
table Index
i Entry
ent
    let revtbl :: RevIndex
revtbl = DynamicTable -> RevIndex
getRevIndex DynamicTable
dyntbl
    let ai :: AbsoluteIndex
ai = Index -> AbsoluteIndex
AbsoluteIndex Index
insp
    Entry -> HIndex -> RevIndex -> IO ()
insertRevIndex Entry
ent (AbsoluteIndex -> HIndex
DIndex AbsoluteIndex
ai) RevIndex
revtbl
    forall (m :: * -> *) a. Monad m => a -> m a
return AbsoluteIndex
ai

insertEntryToDecoder :: Entry -> DynamicTable -> STM ()
insertEntryToDecoder :: Entry -> DynamicTable -> STM ()
insertEntryToDecoder Entry
ent DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} = do
    x :: InsertionPoint
x@(InsertionPoint Index
insp) <- forall a. TVar a -> STM a
readTVar TVar InsertionPoint
insertionPoint
    forall a. TVar a -> a -> STM ()
writeTVar TVar InsertionPoint
insertionPoint (InsertionPoint
x forall a. Num a => a -> a -> a
+ InsertionPoint
1)
    Index
maxN <- forall a. TVar a -> STM a
readTVar TVar Index
maxNumOfEntries
    let i :: Index
i = Index
insp forall a. Integral a => a -> a -> a
`mod` Index
maxN
    Table
table <- forall a. TVar a -> STM a
readTVar TVar Table
circularTable
    forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Index -> e -> m ()
unsafeWrite Table
table Index
i Entry
ent

toDynamicEntry :: DynamicTable -> AbsoluteIndex -> STM Entry
toDynamicEntry :: DynamicTable -> AbsoluteIndex -> STM Entry
toDynamicEntry DynamicTable{TVar Index
TVar Table
TVar InsertionPoint
IORef Bool
IORef BasePoint
IORef AbsoluteIndex
CodeInfo
debugQPACK :: IORef Bool
circularTable :: TVar Table
maxNumOfEntries :: TVar Index
basePoint :: IORef BasePoint
insertionPoint :: TVar InsertionPoint
drainingPoint :: IORef AbsoluteIndex
droppingPoint :: IORef AbsoluteIndex
codeInfo :: CodeInfo
debugQPACK :: DynamicTable -> IORef Bool
circularTable :: DynamicTable -> TVar Table
maxNumOfEntries :: DynamicTable -> TVar Index
basePoint :: DynamicTable -> IORef BasePoint
insertionPoint :: DynamicTable -> TVar InsertionPoint
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
codeInfo :: DynamicTable -> CodeInfo
..} (AbsoluteIndex Index
idx) = do
    Index
maxN <- forall a. TVar a -> STM a
readTVar TVar Index
maxNumOfEntries
    let i :: Index
i = Index
idx forall a. Integral a => a -> a -> a
`mod` Index
maxN
    Table
table <- forall a. TVar a -> STM a
readTVar TVar Table
circularTable
    forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Index -> m e
unsafeRead Table
table Index
i