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

module Network.QPACK.Table.Dynamic where

import Control.Concurrent.STM
import Data.Array.Base (unsafeRead, unsafeWrite)
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = IORef BasePoint -> IO BasePoint
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = do
    InsertionPoint Index
ip <- TVar InsertionPoint -> IO InsertionPoint
forall a. TVar a -> IO a
readTVarIO TVar InsertionPoint
insertionPoint
    IORef BasePoint -> BasePoint -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef BasePoint
basePoint (BasePoint -> IO ()) -> BasePoint -> IO ()
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = TVar InsertionPoint -> IO InsertionPoint
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = TVar InsertionPoint -> STM InsertionPoint
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} InsertionPoint
reqip = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
    InsertionPoint
ip <- TVar InsertionPoint -> STM InsertionPoint
forall a. TVar a -> STM a
readTVar TVar InsertionPoint
insertionPoint
    Bool -> STM ()
check (InsertionPoint
reqip InsertionPoint -> InsertionPoint -> Bool
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 <- InsertionPoint -> IO (IORef InsertionPoint)
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 <- Index -> IO (ForeignPtr Word8)
forall a. Index -> IO (ForeignPtr a)
mallocPlainForeignPtrBytes Index
huftmpsiz
    TVar (Maybe (ForeignPtr Word8, Index))
tvar <- Maybe (ForeignPtr Word8, Index)
-> IO (TVar (Maybe (ForeignPtr Word8, Index)))
forall a. a -> IO (TVar a)
newTVarIO (Maybe (ForeignPtr Word8, Index)
 -> IO (TVar (Maybe (ForeignPtr Word8, Index))))
-> Maybe (ForeignPtr Word8, Index)
-> IO (TVar (Maybe (ForeignPtr Word8, Index)))
forall a b. (a -> b) -> a -> b
$ (ForeignPtr Word8, Index) -> Maybe (ForeignPtr Word8, Index)
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 = IO (ForeignPtr Word8, Index)
-> ((ForeignPtr Word8, Index) -> IO ())
-> ((ForeignPtr Word8, Index) -> IO ByteString)
-> IO ByteString
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 (((ForeignPtr Word8, Index) -> IO ByteString) -> IO ByteString)
-> ((ForeignPtr Word8, Index) -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \(ForeignPtr Word8
gcbuf, Index
bufsiz) ->
    ForeignPtr Word8 -> (Ptr Word8 -> IO ByteString) -> IO ByteString
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
gcbuf ((Ptr Word8 -> IO ByteString) -> IO ByteString)
-> (Ptr Word8 -> IO ByteString) -> IO ByteString
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 = STM (ForeignPtr Word8, Index) -> IO (ForeignPtr Word8, Index)
forall a. STM a -> IO a
atomically (STM (ForeignPtr Word8, Index) -> IO (ForeignPtr Word8, Index))
-> STM (ForeignPtr Word8, Index) -> IO (ForeignPtr Word8, Index)
forall a b. (a -> b) -> a -> b
$ do
        Maybe (ForeignPtr Word8, Index)
mx <- TVar (Maybe (ForeignPtr Word8, Index))
-> STM (Maybe (ForeignPtr Word8, Index))
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 -> STM (ForeignPtr Word8, Index)
forall a. STM a
retry
            Just (ForeignPtr Word8, Index)
x -> do
                TVar (Maybe (ForeignPtr Word8, Index))
-> Maybe (ForeignPtr Word8, Index) -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe (ForeignPtr Word8, Index))
tvar Maybe (ForeignPtr Word8, Index)
forall a. Maybe a
Nothing
                (ForeignPtr Word8, Index) -> STM (ForeignPtr Word8, Index)
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr Word8, Index)
x
    unlock :: (ForeignPtr Word8, Index) -> IO ()
unlock (ForeignPtr Word8, Index)
x = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar (Maybe (ForeignPtr Word8, Index))
-> Maybe (ForeignPtr Word8, Index) -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe (ForeignPtr Word8, Index))
tvar (Maybe (ForeignPtr Word8, Index) -> STM ())
-> Maybe (ForeignPtr Word8, Index) -> STM ()
forall a b. (a -> b) -> a -> b
$ (ForeignPtr Word8, Index) -> Maybe (ForeignPtr Word8, Index)
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 <- STM Table -> IO Table
forall a. STM a -> IO a
atomically (STM Table -> IO Table) -> STM Table -> IO Table
forall a b. (a -> b) -> a -> b
$ (Index, Index) -> Entry -> STM Table
forall i. Ix i => (i, i) -> Entry -> STM (TArray i Entry)
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
        (IORef AbsoluteIndex
 -> IORef AbsoluteIndex
 -> TVar InsertionPoint
 -> IORef BasePoint
 -> TVar Index
 -> TVar Table
 -> IORef Bool
 -> DynamicTable)
-> IO (IORef AbsoluteIndex)
-> IO
     (IORef AbsoluteIndex
      -> TVar InsertionPoint
      -> IORef BasePoint
      -> TVar Index
      -> TVar Table
      -> IORef Bool
      -> DynamicTable)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AbsoluteIndex -> IO (IORef AbsoluteIndex)
forall a. a -> IO (IORef a)
newIORef AbsoluteIndex
0 -- droppingPoint
        IO
  (IORef AbsoluteIndex
   -> TVar InsertionPoint
   -> IORef BasePoint
   -> TVar Index
   -> TVar Table
   -> IORef Bool
   -> DynamicTable)
-> IO (IORef AbsoluteIndex)
-> IO
     (TVar InsertionPoint
      -> IORef BasePoint
      -> TVar Index
      -> TVar Table
      -> IORef Bool
      -> DynamicTable)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> AbsoluteIndex -> IO (IORef AbsoluteIndex)
forall a. a -> IO (IORef a)
newIORef AbsoluteIndex
0 -- drainingPoint
        IO
  (TVar InsertionPoint
   -> IORef BasePoint
   -> TVar Index
   -> TVar Table
   -> IORef Bool
   -> DynamicTable)
-> IO (TVar InsertionPoint)
-> IO
     (IORef BasePoint
      -> TVar Index -> TVar Table -> IORef Bool -> DynamicTable)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> InsertionPoint -> IO (TVar InsertionPoint)
forall a. a -> IO (TVar a)
newTVarIO InsertionPoint
0 -- insertionPoint
        IO
  (IORef BasePoint
   -> TVar Index -> TVar Table -> IORef Bool -> DynamicTable)
-> IO (IORef BasePoint)
-> IO (TVar Index -> TVar Table -> IORef Bool -> DynamicTable)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> BasePoint -> IO (IORef BasePoint)
forall a. a -> IO (IORef a)
newIORef BasePoint
0 -- basePoint
        IO (TVar Index -> TVar Table -> IORef Bool -> DynamicTable)
-> IO (TVar Index) -> IO (TVar Table -> IORef Bool -> DynamicTable)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Index -> IO (TVar Index)
forall a. a -> IO (TVar a)
newTVarIO Index
maxN -- maxNumOfEntries
        IO (TVar Table -> IORef Bool -> DynamicTable)
-> IO (TVar Table) -> IO (IORef Bool -> DynamicTable)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Table -> IO (TVar Table)
forall a. a -> IO (TVar a)
newTVarIO Table
tbl -- maxDynamicTableSize
        IO (IORef Bool -> DynamicTable)
-> IO (IORef Bool) -> IO DynamicTable
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Bool -> IO (IORef Bool)
forall a. a -> IO (IORef a)
newIORef Bool
False -- debugQPACK
  where
    maxN :: Index
maxN = Index -> Index
maxNumbers Index
maxsiz
    end :: Index
end = Index
maxN Index -> Index -> Index
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = IORef Bool -> Bool -> IO ()
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = IORef Bool -> IO Bool
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} IO ()
action = do
    Bool
debug <- IORef Bool -> IO Bool
forall a. IORef a -> IO a
readIORef IORef Bool
debugQPACK
    Bool -> IO () -> IO ()
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = TVar Index -> IO Index
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = 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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = 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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = IORef InsertionPoint -> InsertionPoint -> IO ()
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = IORef InsertionPoint -> IO InsertionPoint
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} (AbsoluteIndex Index
idx) = do
    let nidx :: InsertionPoint
nidx = Index -> InsertionPoint
InsertionPoint Index
idx
    InsertionPoint
oidx <- IORef InsertionPoint -> IO InsertionPoint
forall a. IORef a -> IO a
readIORef IORef InsertionPoint
ref
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (InsertionPoint
nidx InsertionPoint -> InsertionPoint -> Bool
forall a. Ord a => a -> a -> Bool
> InsertionPoint
oidx) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ IORef InsertionPoint -> InsertionPoint -> IO ()
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = do
    InsertionPoint Index
insp <- STM InsertionPoint -> IO InsertionPoint
forall a. STM a -> IO a
atomically (STM InsertionPoint -> IO InsertionPoint)
-> STM InsertionPoint -> IO InsertionPoint
forall a b. (a -> b) -> a -> b
$ do
        InsertionPoint
x <- TVar InsertionPoint -> STM InsertionPoint
forall a. TVar a -> STM a
readTVar TVar InsertionPoint
insertionPoint
        TVar InsertionPoint -> InsertionPoint -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar InsertionPoint
insertionPoint (InsertionPoint
x InsertionPoint -> InsertionPoint -> InsertionPoint
forall a. Num a => a -> a -> a
+ InsertionPoint
1)
        InsertionPoint -> STM InsertionPoint
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return InsertionPoint
x
    Index
maxN <- STM Index -> IO Index
forall a. STM a -> IO a
atomically (STM Index -> IO Index) -> STM Index -> IO Index
forall a b. (a -> b) -> a -> b
$ TVar Index -> STM Index
forall a. TVar a -> STM a
readTVar TVar Index
maxNumOfEntries
    let i :: Index
i = Index
insp Index -> Index -> Index
forall a. Integral a => a -> a -> a
`mod` Index
maxN
    Table
table <- STM Table -> IO Table
forall a. STM a -> IO a
atomically (STM Table -> IO Table) -> STM Table -> IO Table
forall a b. (a -> b) -> a -> b
$ TVar Table -> STM Table
forall a. TVar a -> STM a
readTVar TVar Table
circularTable
    STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ Table -> Index -> Entry -> STM ()
forall i. Ix i => TArray i Entry -> Index -> Entry -> STM ()
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
    AbsoluteIndex -> IO AbsoluteIndex
forall a. a -> IO a
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} = do
    x :: InsertionPoint
x@(InsertionPoint Index
insp) <- TVar InsertionPoint -> STM InsertionPoint
forall a. TVar a -> STM a
readTVar TVar InsertionPoint
insertionPoint
    TVar InsertionPoint -> InsertionPoint -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar InsertionPoint
insertionPoint (InsertionPoint
x InsertionPoint -> InsertionPoint -> InsertionPoint
forall a. Num a => a -> a -> a
+ InsertionPoint
1)
    Index
maxN <- TVar Index -> STM Index
forall a. TVar a -> STM a
readTVar TVar Index
maxNumOfEntries
    let i :: Index
i = Index
insp Index -> Index -> Index
forall a. Integral a => a -> a -> a
`mod` Index
maxN
    Table
table <- TVar Table -> STM Table
forall a. TVar a -> STM a
readTVar TVar Table
circularTable
    Table -> Index -> Entry -> STM ()
forall i. Ix i => TArray i Entry -> Index -> Entry -> STM ()
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
codeInfo :: DynamicTable -> CodeInfo
droppingPoint :: DynamicTable -> IORef AbsoluteIndex
drainingPoint :: DynamicTable -> IORef AbsoluteIndex
insertionPoint :: DynamicTable -> TVar InsertionPoint
basePoint :: DynamicTable -> IORef BasePoint
maxNumOfEntries :: DynamicTable -> TVar Index
circularTable :: DynamicTable -> TVar Table
debugQPACK :: DynamicTable -> IORef Bool
codeInfo :: CodeInfo
droppingPoint :: IORef AbsoluteIndex
drainingPoint :: IORef AbsoluteIndex
insertionPoint :: TVar InsertionPoint
basePoint :: IORef BasePoint
maxNumOfEntries :: TVar Index
circularTable :: TVar Table
debugQPACK :: IORef Bool
..} (AbsoluteIndex Index
idx) = do
    Index
maxN <- TVar Index -> STM Index
forall a. TVar a -> STM a
readTVar TVar Index
maxNumOfEntries
    let i :: Index
i = Index
idx Index -> Index -> Index
forall a. Integral a => a -> a -> a
`mod` Index
maxN
    Table
table <- TVar Table -> STM Table
forall a. TVar a -> STM a
readTVar TVar Table
circularTable
    Table -> Index -> STM Entry
forall i. Ix i => TArray i Entry -> Index -> STM Entry
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Index -> m e
unsafeRead Table
table Index
i