{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Network.HTTP2.Server.Sender (frameSender) where
import Control.Concurrent.STM
import qualified Control.Exception as E
import qualified Data.ByteString as BS
import Data.ByteString.Builder (Builder)
import qualified Data.ByteString.Builder.Extra as B
import Data.IORef
import Foreign.Ptr (plusPtr)
import Network.ByteOrder
import Imports
import Network.HPACK (setLimitForEncoding, toHeaderTable)
import Network.HTTP2
import Network.HTTP2.Priority (isEmptySTM, dequeueSTM, Precedence)
import Network.HTTP2.Server.API
import Network.HTTP2.Server.EncodeFrame
import Network.HTTP2.Server.HPACK
import Network.HTTP2.Server.Manager hiding (start)
import Network.HTTP2.Server.Types
import Network.HTTP2.Server.Queue
import Network.HTTP2.Server.Context
import Network.HTTP2.Server.Stream
data Leftover = LZero
| LOne B.BufferWriter
| LTwo ByteString B.BufferWriter
{-# INLINE getStreamWindowSize #-}
getStreamWindowSize :: Stream -> IO WindowSize
getStreamWindowSize :: Stream -> IO WindowSize
getStreamWindowSize Stream{TVar WindowSize
streamWindow :: Stream -> TVar WindowSize
streamWindow :: TVar WindowSize
streamWindow} = TVar WindowSize -> IO WindowSize
forall a. TVar a -> IO a
readTVarIO TVar WindowSize
streamWindow
{-# INLINE waitStreamWindowSize #-}
waitStreamWindowSize :: Stream -> IO ()
waitStreamWindowSize :: Stream -> IO ()
waitStreamWindowSize Stream{TVar WindowSize
streamWindow :: TVar WindowSize
streamWindow :: Stream -> TVar WindowSize
streamWindow} = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
WindowSize
w <- TVar WindowSize -> STM WindowSize
forall a. TVar a -> STM a
readTVar TVar WindowSize
streamWindow
Bool -> STM ()
check (WindowSize
w WindowSize -> WindowSize -> Bool
forall a. Ord a => a -> a -> Bool
> WindowSize
0)
{-# INLINE waitStreaming #-}
waitStreaming :: TBQueue a -> IO ()
waitStreaming :: TBQueue a -> IO ()
waitStreaming TBQueue a
tbq = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Bool
isEmpty <- TBQueue a -> STM Bool
forall a. TBQueue a -> STM Bool
isEmptyTBQueue TBQueue a
tbq
Bool -> STM ()
check (Bool -> Bool
not Bool
isEmpty)
data Switch = C Control
| O (StreamId,Precedence,Output)
| Flush
frameSender :: Context -> Config -> Manager -> IO ()
frameSender :: Context -> Config -> Manager -> IO ()
frameSender ctx :: Context
ctx@Context{PriorityTree Output
outputQ :: Context -> PriorityTree Output
outputQ :: PriorityTree Output
outputQ,TQueue Control
controlQ :: Context -> TQueue Control
controlQ :: TQueue Control
controlQ,TVar WindowSize
connectionWindow :: Context -> TVar WindowSize
connectionWindow :: TVar WindowSize
connectionWindow,DynamicTable
encodeDynamicTable :: Context -> DynamicTable
encodeDynamicTable :: DynamicTable
encodeDynamicTable}
conf :: Config
conf@Config{WindowSize
Buffer
WindowSize -> IO ByteString
PositionReadMaker
ByteString -> IO ()
confPositionReadMaker :: Config -> PositionReadMaker
confReadN :: Config -> WindowSize -> IO ByteString
confSendAll :: Config -> ByteString -> IO ()
confBufferSize :: Config -> WindowSize
confWriteBuffer :: Config -> Buffer
confPositionReadMaker :: PositionReadMaker
confReadN :: WindowSize -> IO ByteString
confSendAll :: ByteString -> IO ()
confBufferSize :: WindowSize
confWriteBuffer :: Buffer
..}
Manager
mgr = WindowSize -> IO ()
loop WindowSize
0 IO () -> (SomeException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` SomeException -> IO ()
ignore
where
dequeue :: a -> STM Switch
dequeue a
off = do
Bool
isEmpty <- TQueue Control -> STM Bool
forall a. TQueue a -> STM Bool
isEmptyTQueue TQueue Control
controlQ
if Bool
isEmpty then do
WindowSize
w <- TVar WindowSize -> STM WindowSize
forall a. TVar a -> STM a
readTVar TVar WindowSize
connectionWindow
Bool -> STM ()
check (WindowSize
w WindowSize -> WindowSize -> Bool
forall a. Ord a => a -> a -> Bool
> WindowSize
0)
Bool
emp <- PriorityTree Output -> STM Bool
forall a. PriorityTree a -> STM Bool
isEmptySTM PriorityTree Output
outputQ
if Bool
emp then
if a
off a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0 then Switch -> STM Switch
forall (m :: * -> *) a. Monad m => a -> m a
return Switch
Flush else STM Switch
forall a. STM a
retry
else
(WindowSize, Precedence, Output) -> Switch
O ((WindowSize, Precedence, Output) -> Switch)
-> STM (WindowSize, Precedence, Output) -> STM Switch
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PriorityTree Output -> STM (WindowSize, Precedence, Output)
forall a. PriorityTree a -> STM (WindowSize, Precedence, a)
dequeueSTM PriorityTree Output
outputQ
else
Control -> Switch
C (Control -> Switch) -> STM Control -> STM Switch
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TQueue Control -> STM Control
forall a. TQueue a -> STM a
readTQueue TQueue Control
controlQ
hardLimit :: WindowSize
hardLimit = WindowSize
confBufferSize WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
512
loop :: WindowSize -> IO ()
loop WindowSize
off = do
Switch
x <- STM Switch -> IO Switch
forall a. STM a -> IO a
atomically (STM Switch -> IO Switch) -> STM Switch -> IO Switch
forall a b. (a -> b) -> a -> b
$ WindowSize -> STM Switch
forall a. (Eq a, Num a) => a -> STM Switch
dequeue WindowSize
off
case Switch
x of
C Control
ctl -> do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (WindowSize
off WindowSize -> WindowSize -> Bool
forall a. Eq a => a -> a -> Bool
/= WindowSize
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ WindowSize -> IO ()
flushN WindowSize
off
WindowSize
off' <- Control -> WindowSize -> IO WindowSize
control Control
ctl WindowSize
off
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (WindowSize
off' WindowSize -> WindowSize -> Bool
forall a. Ord a => a -> a -> Bool
>= WindowSize
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ WindowSize -> IO ()
loop WindowSize
off'
O (WindowSize
_,Precedence
pre,Output
out) -> do
let strm :: Stream
strm = Output -> Stream
outputStream Output
out
IORef Precedence -> Precedence -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Stream -> IORef Precedence
streamPrecedence Stream
strm) Precedence
pre
WindowSize
off' <- Output -> WindowSize -> IO WindowSize
outputOrEnqueueAgain Output
out WindowSize
off
case WindowSize
off' of
WindowSize
0 -> WindowSize -> IO ()
loop WindowSize
0
WindowSize
_ | WindowSize
off' WindowSize -> WindowSize -> Bool
forall a. Ord a => a -> a -> Bool
> WindowSize
hardLimit -> WindowSize -> IO ()
flushN WindowSize
off' IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> WindowSize -> IO ()
loop WindowSize
0
| Bool
otherwise -> WindowSize -> IO ()
loop WindowSize
off'
Switch
Flush -> WindowSize -> IO ()
flushN WindowSize
off IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> WindowSize -> IO ()
loop WindowSize
0
control :: Control -> WindowSize -> IO WindowSize
control Control
CFinish WindowSize
_ = WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return (-WindowSize
1)
control (CGoaway ByteString
frame) WindowSize
_ = ByteString -> IO ()
confSendAll ByteString
frame IO () -> IO WindowSize -> IO WindowSize
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return (-WindowSize
1)
control (CFrame ByteString
frame) WindowSize
_ = ByteString -> IO ()
confSendAll ByteString
frame IO () -> IO WindowSize -> IO WindowSize
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
0
control (CSettings ByteString
frame SettingsList
alist) WindowSize
_ = do
ByteString -> IO ()
confSendAll ByteString
frame
SettingsList -> IO ()
setLimit SettingsList
alist
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
0
control (CSettings0 ByteString
frame1 ByteString
frame2 SettingsList
alist) WindowSize
off = do
let !buf :: Ptr b
buf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
!off' :: WindowSize
off' = WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ ByteString -> WindowSize
BS.length ByteString
frame1 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ ByteString -> WindowSize
BS.length ByteString
frame2
Buffer
buf' <- Buffer -> ByteString -> IO Buffer
copy Buffer
forall b. Ptr b
buf ByteString
frame1
IO Buffer -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Buffer -> IO ()) -> IO Buffer -> IO ()
forall a b. (a -> b) -> a -> b
$ Buffer -> ByteString -> IO Buffer
copy Buffer
buf' ByteString
frame2
SettingsList -> IO ()
setLimit SettingsList
alist
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off'
{-# INLINE setLimit #-}
setLimit :: SettingsList -> IO ()
setLimit SettingsList
alist = case SettingsKeyId -> SettingsList -> Maybe WindowSize
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup SettingsKeyId
SettingsHeaderTableSize SettingsList
alist of
Maybe WindowSize
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just WindowSize
siz -> WindowSize -> DynamicTable -> IO ()
setLimitForEncoding WindowSize
siz DynamicTable
encodeDynamicTable
output :: Output -> WindowSize -> WindowSize -> IO WindowSize
output out :: Output
out@(Output Stream
strm (Response Status
_ ResponseHeaders
_ ResponseBody
_ TrailersMaker
_) (ONext DynaNext
curr TrailersMaker
tlrmkr) Maybe (TBQueue RspStreaming)
_ IO ()
sentinel) WindowSize
off0 WindowSize
lim = do
let !buf :: Ptr b
buf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off0
!siz :: WindowSize
siz = WindowSize
confBufferSize WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
off0
!payloadOff :: WindowSize
payloadOff = WindowSize
off0 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength
Next WindowSize
datPayloadLen Maybe DynaNext
mnext <- DynaNext
curr Buffer
forall b. Ptr b
buf WindowSize
siz WindowSize
lim
NextTrailersMaker !TrailersMaker
tlrmkr' <- TrailersMaker -> WindowSize -> WindowSize -> IO NextTrailersMaker
forall a.
(Maybe ByteString -> IO a) -> WindowSize -> WindowSize -> IO a
runTrailersMaker TrailersMaker
tlrmkr WindowSize
payloadOff WindowSize
datPayloadLen
Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO ()
-> Output
-> IO WindowSize
forall a.
Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO a
-> Output
-> IO WindowSize
fillDataHeaderEnqueueNext Stream
strm WindowSize
off0 WindowSize
datPayloadLen Maybe DynaNext
mnext TrailersMaker
tlrmkr' IO ()
sentinel Output
out
output out :: Output
out@(Output Stream
strm (Response Status
st ResponseHeaders
hdr ResponseBody
body TrailersMaker
tlrmkr) OutputType
ORspn Maybe (TBQueue RspStreaming)
mtbq IO ()
sentinel) WindowSize
off0 WindowSize
lim = do
let !sid :: WindowSize
sid = Stream -> WindowSize
streamNumber Stream
strm
!endOfStream :: Bool
endOfStream = case ResponseBody
body of
ResponseBody
RspNoBody -> Bool
True
ResponseBody
_ -> Bool
False
(TokenHeaderList
ths,ValueTable
_) <- ResponseHeaders -> IO (TokenHeaderList, ValueTable)
toHeaderTable (ResponseHeaders -> IO (TokenHeaderList, ValueTable))
-> ResponseHeaders -> IO (TokenHeaderList, ValueTable)
forall a b. (a -> b) -> a -> b
$ Status -> ResponseHeaders -> ResponseHeaders
fixHeaders Status
st ResponseHeaders
hdr
WindowSize
kvlen <- WindowSize
-> TokenHeaderList -> Bool -> WindowSize -> IO WindowSize
headerContinue WindowSize
sid TokenHeaderList
ths Bool
endOfStream WindowSize
off0
WindowSize
off <- WindowSize -> IO WindowSize
sendHeadersIfNecessary (WindowSize -> IO WindowSize) -> WindowSize -> IO WindowSize
forall a b. (a -> b) -> a -> b
$ WindowSize
off0 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
kvlen
case ResponseBody
body of
ResponseBody
RspNoBody -> do
Context -> Stream -> ClosedCode -> IO ()
halfClosedLocal Context
ctx Stream
strm ClosedCode
Finished
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off
RspFile (FileSpec FilePath
path FileOffset
fileoff FileOffset
bytecount) -> do
let payloadOff :: WindowSize
payloadOff = WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength
Next WindowSize
datPayloadLen Maybe DynaNext
mnext <-
Config
-> WindowSize
-> WindowSize
-> FilePath
-> FileOffset
-> FileOffset
-> Manager
-> PositionReadMaker
-> IO Next
fillFileBodyGetNext Config
conf WindowSize
payloadOff WindowSize
lim FilePath
path FileOffset
fileoff FileOffset
bytecount Manager
mgr PositionReadMaker
confPositionReadMaker
NextTrailersMaker !TrailersMaker
tlrmkr' <- TrailersMaker -> WindowSize -> WindowSize -> IO NextTrailersMaker
forall a.
(Maybe ByteString -> IO a) -> WindowSize -> WindowSize -> IO a
runTrailersMaker TrailersMaker
tlrmkr WindowSize
payloadOff WindowSize
datPayloadLen
Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO ()
-> Output
-> IO WindowSize
forall a.
Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO a
-> Output
-> IO WindowSize
fillDataHeaderEnqueueNext Stream
strm WindowSize
off WindowSize
datPayloadLen Maybe DynaNext
mnext TrailersMaker
tlrmkr' IO ()
sentinel Output
out
RspBuilder Builder
builder -> do
let payloadOff :: WindowSize
payloadOff = WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength
Next WindowSize
datPayloadLen Maybe DynaNext
mnext <-
Config -> WindowSize -> WindowSize -> Builder -> IO Next
fillBuilderBodyGetNext Config
conf WindowSize
payloadOff WindowSize
lim Builder
builder
NextTrailersMaker !TrailersMaker
tlrmkr' <- TrailersMaker -> WindowSize -> WindowSize -> IO NextTrailersMaker
forall a.
(Maybe ByteString -> IO a) -> WindowSize -> WindowSize -> IO a
runTrailersMaker TrailersMaker
tlrmkr WindowSize
payloadOff WindowSize
datPayloadLen
Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO ()
-> Output
-> IO WindowSize
forall a.
Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO a
-> Output
-> IO WindowSize
fillDataHeaderEnqueueNext Stream
strm WindowSize
off WindowSize
datPayloadLen Maybe DynaNext
mnext TrailersMaker
tlrmkr' IO ()
sentinel Output
out
RspStreaming (Builder -> IO ()) -> IO () -> IO ()
_ -> do
let payloadOff :: WindowSize
payloadOff = WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength
Next WindowSize
datPayloadLen Maybe DynaNext
mnext <-
Config
-> WindowSize
-> WindowSize
-> TBQueue RspStreaming
-> Stream
-> IO Next
fillStreamBodyGetNext Config
conf WindowSize
payloadOff WindowSize
lim (Maybe (TBQueue RspStreaming) -> TBQueue RspStreaming
forall a. HasCallStack => Maybe a -> a
fromJust Maybe (TBQueue RspStreaming)
mtbq) Stream
strm
NextTrailersMaker !TrailersMaker
tlrmkr' <- TrailersMaker -> WindowSize -> WindowSize -> IO NextTrailersMaker
forall a.
(Maybe ByteString -> IO a) -> WindowSize -> WindowSize -> IO a
runTrailersMaker TrailersMaker
tlrmkr WindowSize
payloadOff WindowSize
datPayloadLen
Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO ()
-> Output
-> IO WindowSize
forall a.
Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO a
-> Output
-> IO WindowSize
fillDataHeaderEnqueueNext Stream
strm WindowSize
off WindowSize
datPayloadLen Maybe DynaNext
mnext TrailersMaker
tlrmkr' IO ()
sentinel Output
out
output out :: Output
out@(Output Stream
strm Response
_ (OPush TokenHeaderList
ths WindowSize
pid) Maybe (TBQueue RspStreaming)
_ IO ()
_) WindowSize
off0 WindowSize
lim = do
let !sid :: WindowSize
sid = Stream -> WindowSize
streamNumber Stream
strm
WindowSize
len <- WindowSize
-> WindowSize -> TokenHeaderList -> WindowSize -> IO WindowSize
forall a.
Integral a =>
WindowSize -> a -> TokenHeaderList -> WindowSize -> IO WindowSize
pushPromise WindowSize
pid WindowSize
sid TokenHeaderList
ths WindowSize
off0
WindowSize
off <- WindowSize -> IO WindowSize
sendHeadersIfNecessary (WindowSize -> IO WindowSize) -> WindowSize -> IO WindowSize
forall a b. (a -> b) -> a -> b
$ WindowSize
off0 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
len
Output -> WindowSize -> WindowSize -> IO WindowSize
output Output
out{outputType :: OutputType
outputType=OutputType
ORspn} WindowSize
off WindowSize
lim
output Output
_ WindowSize
_ WindowSize
_ = IO WindowSize
forall a. HasCallStack => a
undefined
outputOrEnqueueAgain :: Output -> Int -> IO Int
outputOrEnqueueAgain :: Output -> WindowSize -> IO WindowSize
outputOrEnqueueAgain out :: Output
out@(Output Stream
strm Response
_ OutputType
otyp Maybe (TBQueue RspStreaming)
_ IO ()
_) WindowSize
off = (SomeException -> IO WindowSize) -> IO WindowSize -> IO WindowSize
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
E.handle SomeException -> IO WindowSize
resetStream (IO WindowSize -> IO WindowSize) -> IO WindowSize -> IO WindowSize
forall a b. (a -> b) -> a -> b
$ do
StreamState
state <- Stream -> IO StreamState
readStreamState Stream
strm
if StreamState -> Bool
isHalfClosedLocal StreamState
state then
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off
else case OutputType
otyp of
OWait IO ()
wait -> do
IO () -> PriorityTree Output -> Output -> Manager -> IO ()
forkAndEnqueueWhenReady IO ()
wait PriorityTree Output
outputQ Output
out{outputType :: OutputType
outputType=OutputType
ORspn} Manager
mgr
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off
OutputType
_ -> case Maybe (TBQueue RspStreaming)
mtbq of
Just TBQueue RspStreaming
tbq -> TBQueue RspStreaming -> IO WindowSize
forall a. TBQueue a -> IO WindowSize
checkStreaming TBQueue RspStreaming
tbq
Maybe (TBQueue RspStreaming)
_ -> IO WindowSize
checkStreamWindowSize
where
mtbq :: Maybe (TBQueue RspStreaming)
mtbq = Output -> Maybe (TBQueue RspStreaming)
outputStrmQ Output
out
checkStreaming :: TBQueue a -> IO WindowSize
checkStreaming TBQueue a
tbq = do
Bool
isEmpty <- 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
$ TBQueue a -> STM Bool
forall a. TBQueue a -> STM Bool
isEmptyTBQueue TBQueue a
tbq
if Bool
isEmpty then do
IO () -> PriorityTree Output -> Output -> Manager -> IO ()
forkAndEnqueueWhenReady (TBQueue a -> IO ()
forall a. TBQueue a -> IO ()
waitStreaming TBQueue a
tbq) PriorityTree Output
outputQ Output
out Manager
mgr
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off
else
IO WindowSize
checkStreamWindowSize
checkStreamWindowSize :: IO WindowSize
checkStreamWindowSize = do
WindowSize
sws <- Stream -> IO WindowSize
getStreamWindowSize Stream
strm
if WindowSize
sws WindowSize -> WindowSize -> Bool
forall a. Eq a => a -> a -> Bool
== WindowSize
0 then do
IO () -> PriorityTree Output -> Output -> Manager -> IO ()
forkAndEnqueueWhenReady (Stream -> IO ()
waitStreamWindowSize Stream
strm) PriorityTree Output
outputQ Output
out Manager
mgr
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off
else do
WindowSize
cws <- TVar WindowSize -> IO WindowSize
forall a. TVar a -> IO a
readTVarIO TVar WindowSize
connectionWindow
let !lim :: WindowSize
lim = WindowSize -> WindowSize -> WindowSize
forall a. Ord a => a -> a -> a
min WindowSize
cws WindowSize
sws
Output -> WindowSize -> WindowSize -> IO WindowSize
output Output
out WindowSize
off WindowSize
lim
resetStream :: SomeException -> IO WindowSize
resetStream SomeException
e = do
Context -> Stream -> ClosedCode -> IO ()
closed Context
ctx Stream
strm (SomeException -> ClosedCode
ResetByMe SomeException
e)
let !rst :: ByteString
rst = ErrorCodeId -> WindowSize -> ByteString
resetFrame ErrorCodeId
InternalError (WindowSize -> ByteString) -> WindowSize -> ByteString
forall a b. (a -> b) -> a -> b
$ Stream -> WindowSize
streamNumber Stream
strm
TQueue Control -> Control -> IO ()
enqueueControl TQueue Control
controlQ (Control -> IO ()) -> Control -> IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> Control
CFrame ByteString
rst
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off
{-# INLINE flushN #-}
flushN :: Int -> IO ()
flushN :: WindowSize -> IO ()
flushN WindowSize
n = Buffer -> WindowSize -> (ByteString -> IO ()) -> IO ()
forall a. Buffer -> WindowSize -> (ByteString -> IO a) -> IO a
bufferIO Buffer
confWriteBuffer WindowSize
n ByteString -> IO ()
confSendAll
headerContinue :: WindowSize
-> TokenHeaderList -> Bool -> WindowSize -> IO WindowSize
headerContinue WindowSize
sid TokenHeaderList
ths Bool
endOfStream WindowSize
off = do
let !offkv :: WindowSize
offkv = WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength
let !bufkv :: Ptr b
bufkv = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
offkv
!limkv :: WindowSize
limkv = WindowSize
confBufferSize WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
offkv
(TokenHeaderList
hs,WindowSize
kvlen) <- Context
-> Buffer
-> WindowSize
-> TokenHeaderList
-> IO (TokenHeaderList, WindowSize)
hpackEncodeHeader Context
ctx Buffer
forall b. Ptr b
bufkv WindowSize
limkv TokenHeaderList
ths
let flag0 :: FrameFlags
flag0 = case TokenHeaderList
hs of
[] -> FrameFlags -> FrameFlags
setEndHeader FrameFlags
defaultFlags
TokenHeaderList
_ -> FrameFlags
defaultFlags
flag :: FrameFlags
flag = if Bool
endOfStream then FrameFlags -> FrameFlags
setEndStream FrameFlags
flag0 else FrameFlags
flag0
let buf :: Ptr b
buf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
FrameTypeId
-> WindowSize -> WindowSize -> FrameFlags -> Buffer -> IO ()
fillFrameHeader FrameTypeId
FrameHeaders WindowSize
kvlen WindowSize
sid FrameFlags
flag Buffer
forall b. Ptr b
buf
WindowSize -> WindowSize -> TokenHeaderList -> IO WindowSize
continue WindowSize
sid WindowSize
kvlen TokenHeaderList
hs
!bufHeaderPayload :: Ptr b
bufHeaderPayload = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
frameHeaderLength
!headerPayloadLim :: WindowSize
headerPayloadLim = WindowSize
confBufferSize WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
frameHeaderLength
continue :: WindowSize -> WindowSize -> TokenHeaderList -> IO WindowSize
continue WindowSize
_ WindowSize
kvlen [] = WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
kvlen
continue WindowSize
sid WindowSize
kvlen TokenHeaderList
ths = do
WindowSize -> IO ()
flushN (WindowSize -> IO ()) -> WindowSize -> IO ()
forall a b. (a -> b) -> a -> b
$ WindowSize
kvlen WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength
(TokenHeaderList
ths', WindowSize
kvlen') <- Context
-> Buffer
-> WindowSize
-> TokenHeaderList
-> IO (TokenHeaderList, WindowSize)
hpackEncodeHeaderLoop Context
ctx Buffer
forall b. Ptr b
bufHeaderPayload WindowSize
headerPayloadLim TokenHeaderList
ths
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TokenHeaderList
ths TokenHeaderList -> TokenHeaderList -> Bool
forall a. Eq a => a -> a -> Bool
== TokenHeaderList
ths') (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ HTTP2Error -> IO ()
forall e a. Exception e => e -> IO a
E.throwIO (HTTP2Error -> IO ()) -> HTTP2Error -> IO ()
forall a b. (a -> b) -> a -> b
$ ErrorCodeId -> ByteString -> HTTP2Error
ConnectionError ErrorCodeId
CompressionError ByteString
"cannot compress the header"
let flag :: FrameFlags
flag = case TokenHeaderList
ths' of
[] -> FrameFlags -> FrameFlags
setEndHeader FrameFlags
defaultFlags
TokenHeaderList
_ -> FrameFlags
defaultFlags
FrameTypeId
-> WindowSize -> WindowSize -> FrameFlags -> Buffer -> IO ()
fillFrameHeader FrameTypeId
FrameContinuation WindowSize
kvlen' WindowSize
sid FrameFlags
flag Buffer
confWriteBuffer
WindowSize -> WindowSize -> TokenHeaderList -> IO WindowSize
continue WindowSize
sid WindowSize
kvlen' TokenHeaderList
ths'
{-# INLINE sendHeadersIfNecessary #-}
sendHeadersIfNecessary :: WindowSize -> IO WindowSize
sendHeadersIfNecessary WindowSize
off
| WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength WindowSize -> WindowSize -> Bool
forall a. Ord a => a -> a -> Bool
< WindowSize
confBufferSize = WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off
| Bool
otherwise = do
WindowSize -> IO ()
flushN WindowSize
off
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
0
fillDataHeaderEnqueueNext :: Stream
-> WindowSize
-> WindowSize
-> Maybe DynaNext
-> TrailersMaker
-> IO a
-> Output
-> IO WindowSize
fillDataHeaderEnqueueNext strm :: Stream
strm@Stream{TVar WindowSize
streamWindow :: TVar WindowSize
streamWindow :: Stream -> TVar WindowSize
streamWindow,WindowSize
streamNumber :: WindowSize
streamNumber :: Stream -> WindowSize
streamNumber}
WindowSize
off WindowSize
datPayloadLen Maybe DynaNext
Nothing TrailersMaker
tlrmkr IO a
tell Output
_ = do
let !buf :: Ptr b
buf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
!off' :: WindowSize
off' = WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
datPayloadLen
(Maybe ResponseHeaders
mtrailers, FrameFlags
flag) <- do
Trailers !ResponseHeaders
trailers <- TrailersMaker
tlrmkr Maybe ByteString
forall a. Maybe a
Nothing
if ResponseHeaders -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ResponseHeaders
trailers then
(Maybe ResponseHeaders, FrameFlags)
-> IO (Maybe ResponseHeaders, FrameFlags)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe ResponseHeaders
forall a. Maybe a
Nothing, FrameFlags -> FrameFlags
setEndStream FrameFlags
defaultFlags)
else
(Maybe ResponseHeaders, FrameFlags)
-> IO (Maybe ResponseHeaders, FrameFlags)
forall (m :: * -> *) a. Monad m => a -> m a
return (ResponseHeaders -> Maybe ResponseHeaders
forall a. a -> Maybe a
Just ResponseHeaders
trailers, FrameFlags
defaultFlags)
FrameTypeId
-> WindowSize -> WindowSize -> FrameFlags -> Buffer -> IO ()
fillFrameHeader FrameTypeId
FrameData WindowSize
datPayloadLen WindowSize
streamNumber FrameFlags
flag Buffer
forall b. Ptr b
buf
WindowSize
off'' <- Maybe ResponseHeaders -> WindowSize -> IO WindowSize
handleTrailers Maybe ResponseHeaders
mtrailers WindowSize
off'
IO a -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void IO a
tell
Context -> Stream -> ClosedCode -> IO ()
halfClosedLocal Context
ctx Stream
strm ClosedCode
Finished
STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar WindowSize -> (WindowSize -> WindowSize) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar WindowSize
connectionWindow (WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
subtract WindowSize
datPayloadLen)
STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar WindowSize -> (WindowSize -> WindowSize) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar WindowSize
streamWindow (WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
subtract WindowSize
datPayloadLen)
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off''
where
handleTrailers :: Maybe ResponseHeaders -> WindowSize -> IO WindowSize
handleTrailers Maybe ResponseHeaders
Nothing WindowSize
off0 = WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off0
handleTrailers (Just ResponseHeaders
trailers) WindowSize
off0 = do
(TokenHeaderList
ths,ValueTable
_) <- ResponseHeaders -> IO (TokenHeaderList, ValueTable)
toHeaderTable ResponseHeaders
trailers
WindowSize
kvlen <- WindowSize
-> TokenHeaderList -> Bool -> WindowSize -> IO WindowSize
headerContinue WindowSize
streamNumber TokenHeaderList
ths Bool
True WindowSize
off0
WindowSize -> IO WindowSize
sendHeadersIfNecessary (WindowSize -> IO WindowSize) -> WindowSize -> IO WindowSize
forall a b. (a -> b) -> a -> b
$ WindowSize
off0 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
kvlen
fillDataHeaderEnqueueNext Stream{TVar WindowSize
streamWindow :: TVar WindowSize
streamWindow :: Stream -> TVar WindowSize
streamWindow,WindowSize
streamNumber :: WindowSize
streamNumber :: Stream -> WindowSize
streamNumber}
WindowSize
off WindowSize
datPayloadLen (Just DynaNext
next) TrailersMaker
tlrmkr IO a
_ Output
out = do
let !buf :: Ptr b
buf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
!off' :: WindowSize
off' = WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
datPayloadLen
flag :: FrameFlags
flag = FrameFlags
defaultFlags
FrameTypeId
-> WindowSize -> WindowSize -> FrameFlags -> Buffer -> IO ()
fillFrameHeader FrameTypeId
FrameData WindowSize
datPayloadLen WindowSize
streamNumber FrameFlags
flag Buffer
forall b. Ptr b
buf
STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar WindowSize -> (WindowSize -> WindowSize) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar WindowSize
connectionWindow (WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
subtract WindowSize
datPayloadLen)
STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar WindowSize -> (WindowSize -> WindowSize) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar WindowSize
streamWindow (WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
subtract WindowSize
datPayloadLen)
let !out' :: Output
out' = Output
out { outputType :: OutputType
outputType = DynaNext -> TrailersMaker -> OutputType
ONext DynaNext
next TrailersMaker
tlrmkr }
PriorityTree Output -> Output -> IO ()
enqueueOutput PriorityTree Output
outputQ Output
out'
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
off'
pushPromise :: WindowSize -> a -> TokenHeaderList -> WindowSize -> IO WindowSize
pushPromise WindowSize
pid a
sid TokenHeaderList
ths WindowSize
off = do
let !offsid :: WindowSize
offsid = WindowSize
off WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
frameHeaderLength
!bufsid :: Ptr b
bufsid = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
offsid
Word32 -> Buffer -> WindowSize -> IO ()
poke32 (a -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
sid) Buffer
forall b. Ptr b
bufsid WindowSize
0
let !offkv :: WindowSize
offkv = WindowSize
offsid WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
4
!bufkv :: Ptr b
bufkv = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
offkv
!limkv :: WindowSize
limkv = WindowSize
confBufferSize WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
offkv
(TokenHeaderList
_,WindowSize
kvlen) <- Context
-> Buffer
-> WindowSize
-> TokenHeaderList
-> IO (TokenHeaderList, WindowSize)
hpackEncodeHeader Context
ctx Buffer
forall b. Ptr b
bufkv WindowSize
limkv TokenHeaderList
ths
let !flag :: FrameFlags
flag = FrameFlags -> FrameFlags
setEndHeader FrameFlags
defaultFlags
!buf :: Ptr b
buf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
!len :: WindowSize
len = WindowSize
kvlen WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
4
FrameTypeId
-> WindowSize -> WindowSize -> FrameFlags -> Buffer -> IO ()
fillFrameHeader FrameTypeId
FramePushPromise WindowSize
len WindowSize
pid FrameFlags
flag Buffer
forall b. Ptr b
buf
WindowSize -> IO WindowSize
forall (m :: * -> *) a. Monad m => a -> m a
return WindowSize
len
{-# INLINE fillFrameHeader #-}
fillFrameHeader :: FrameTypeId
-> WindowSize -> WindowSize -> FrameFlags -> Buffer -> IO ()
fillFrameHeader FrameTypeId
ftyp WindowSize
len WindowSize
sid FrameFlags
flag Buffer
buf = FrameTypeId -> FrameHeader -> Buffer -> IO ()
encodeFrameHeaderBuf FrameTypeId
ftyp FrameHeader
hinfo Buffer
buf
where
hinfo :: FrameHeader
hinfo = WindowSize -> FrameFlags -> WindowSize -> FrameHeader
FrameHeader WindowSize
len FrameFlags
flag WindowSize
sid
runTrailersMaker :: (Maybe ByteString -> IO a) -> WindowSize -> WindowSize -> IO a
runTrailersMaker Maybe ByteString -> IO a
tlrmkr WindowSize
off WindowSize
siz = do
let datBuf :: Ptr b
datBuf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
Buffer -> WindowSize -> (ByteString -> IO a) -> IO a
forall a. Buffer -> WindowSize -> (ByteString -> IO a) -> IO a
bufferIO Buffer
forall b. Ptr b
datBuf WindowSize
siz ((ByteString -> IO a) -> IO a) -> (ByteString -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \ByteString
bs -> Maybe ByteString -> IO a
tlrmkr (ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
bs)
{-# INLINE ignore #-}
ignore :: E.SomeException -> IO ()
ignore :: SomeException -> IO ()
ignore SomeException
_ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
fillBuilderBodyGetNext :: Config -> Int -> WindowSize -> Builder -> IO Next
fillBuilderBodyGetNext :: Config -> WindowSize -> WindowSize -> Builder -> IO Next
fillBuilderBodyGetNext Config{Buffer
confWriteBuffer :: Buffer
confWriteBuffer :: Config -> Buffer
confWriteBuffer,WindowSize
confBufferSize :: WindowSize
confBufferSize :: Config -> WindowSize
confBufferSize}
WindowSize
off WindowSize
lim Builder
bb = do
let datBuf :: Ptr b
datBuf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
room :: WindowSize
room = WindowSize -> WindowSize -> WindowSize
forall a. Ord a => a -> a -> a
min (WindowSize
confBufferSize WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
off) WindowSize
lim
(WindowSize
len, Next
signal) <- Builder -> BufferWriter
B.runBuilder Builder
bb Buffer
forall b. Ptr b
datBuf WindowSize
room
Next -> IO Next
forall (m :: * -> *) a. Monad m => a -> m a
return (Next -> IO Next) -> Next -> IO Next
forall a b. (a -> b) -> a -> b
$ WindowSize -> Next -> Next
nextForBuilder WindowSize
len Next
signal
fillFileBodyGetNext :: Config -> Int -> WindowSize -> FilePath -> FileOffset -> ByteCount -> Manager -> PositionReadMaker -> IO Next
fillFileBodyGetNext :: Config
-> WindowSize
-> WindowSize
-> FilePath
-> FileOffset
-> FileOffset
-> Manager
-> PositionReadMaker
-> IO Next
fillFileBodyGetNext Config{Buffer
confWriteBuffer :: Buffer
confWriteBuffer :: Config -> Buffer
confWriteBuffer,WindowSize
confBufferSize :: WindowSize
confBufferSize :: Config -> WindowSize
confBufferSize}
WindowSize
off WindowSize
lim FilePath
path FileOffset
start FileOffset
bytecount Manager
mgr PositionReadMaker
prmaker = do
let datBuf :: Ptr b
datBuf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
room :: WindowSize
room = WindowSize -> WindowSize -> WindowSize
forall a. Ord a => a -> a -> a
min (WindowSize
confBufferSize WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
off) WindowSize
lim
(PositionRead
pread, Sentinel
sentinel) <- PositionReadMaker
prmaker FilePath
path
IO ()
refresh <- case Sentinel
sentinel of
Closer IO ()
closer -> Manager -> IO () -> IO (IO ())
timeoutClose Manager
mgr IO ()
closer
Refresher IO ()
refresher -> IO () -> IO (IO ())
forall (m :: * -> *) a. Monad m => a -> m a
return IO ()
refresher
FileOffset
len <- PositionRead
pread FileOffset
start (WindowSize -> FileOffset -> FileOffset
mini WindowSize
room FileOffset
bytecount) Buffer
forall b. Ptr b
datBuf
let len' :: WindowSize
len' = FileOffset -> WindowSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral FileOffset
len
Next -> IO Next
forall (m :: * -> *) a. Monad m => a -> m a
return (Next -> IO Next) -> Next -> IO Next
forall a b. (a -> b) -> a -> b
$ WindowSize
-> PositionRead -> FileOffset -> FileOffset -> IO () -> Next
nextForFile WindowSize
len' PositionRead
pread (FileOffset
start FileOffset -> FileOffset -> FileOffset
forall a. Num a => a -> a -> a
+ FileOffset
len) (FileOffset
bytecount FileOffset -> FileOffset -> FileOffset
forall a. Num a => a -> a -> a
- FileOffset
len) IO ()
refresh
fillStreamBodyGetNext :: Config -> Int -> WindowSize -> TBQueue RspStreaming -> Stream -> IO Next
fillStreamBodyGetNext :: Config
-> WindowSize
-> WindowSize
-> TBQueue RspStreaming
-> Stream
-> IO Next
fillStreamBodyGetNext Config{Buffer
confWriteBuffer :: Buffer
confWriteBuffer :: Config -> Buffer
confWriteBuffer,WindowSize
confBufferSize :: WindowSize
confBufferSize :: Config -> WindowSize
confBufferSize}
WindowSize
off WindowSize
lim TBQueue RspStreaming
sq Stream
strm = do
let datBuf :: Ptr b
datBuf = Buffer
confWriteBuffer Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
off
room :: WindowSize
room = WindowSize -> WindowSize -> WindowSize
forall a. Ord a => a -> a -> a
min (WindowSize
confBufferSize WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
off) WindowSize
lim
(Leftover
leftover, Bool
cont, WindowSize
len) <- Buffer
-> WindowSize
-> TBQueue RspStreaming
-> IO (Leftover, Bool, WindowSize)
runStreamBuilder Buffer
forall b. Ptr b
datBuf WindowSize
room TBQueue RspStreaming
sq
Next -> IO Next
forall (m :: * -> *) a. Monad m => a -> m a
return (Next -> IO Next) -> Next -> IO Next
forall a b. (a -> b) -> a -> b
$ TBQueue RspStreaming
-> Stream -> Leftover -> Bool -> WindowSize -> Next
nextForStream TBQueue RspStreaming
sq Stream
strm Leftover
leftover Bool
cont WindowSize
len
fillBufBuilder :: Leftover -> DynaNext
fillBufBuilder :: Leftover -> DynaNext
fillBufBuilder Leftover
leftover Buffer
buf0 WindowSize
siz0 WindowSize
lim = do
let payloadBuf :: Ptr b
payloadBuf = Buffer
buf0 Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
frameHeaderLength
room :: WindowSize
room = WindowSize -> WindowSize -> WindowSize
forall a. Ord a => a -> a -> a
min (WindowSize
siz0 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
frameHeaderLength) WindowSize
lim
case Leftover
leftover of
Leftover
LZero -> FilePath -> IO Next
forall a. HasCallStack => FilePath -> a
error FilePath
"fillBufBuilder: LZero"
LOne BufferWriter
writer -> do
(WindowSize
len, Next
signal) <- BufferWriter
writer Buffer
forall b. Ptr b
payloadBuf WindowSize
room
WindowSize -> Next -> IO Next
forall (m :: * -> *). Monad m => WindowSize -> Next -> m Next
getNext WindowSize
len Next
signal
LTwo ByteString
bs BufferWriter
writer
| ByteString -> WindowSize
BS.length ByteString
bs WindowSize -> WindowSize -> Bool
forall a. Ord a => a -> a -> Bool
<= WindowSize
room -> do
Buffer
buf1 <- Buffer -> ByteString -> IO Buffer
copy Buffer
forall b. Ptr b
payloadBuf ByteString
bs
let len1 :: WindowSize
len1 = ByteString -> WindowSize
BS.length ByteString
bs
(WindowSize
len2, Next
signal) <- BufferWriter
writer Buffer
buf1 (WindowSize
room WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
len1)
WindowSize -> Next -> IO Next
forall (m :: * -> *). Monad m => WindowSize -> Next -> m Next
getNext (WindowSize
len1 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
len2) Next
signal
| Bool
otherwise -> do
let (ByteString
bs1,ByteString
bs2) = WindowSize -> ByteString -> (ByteString, ByteString)
BS.splitAt WindowSize
room ByteString
bs
IO Buffer -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Buffer -> IO ()) -> IO Buffer -> IO ()
forall a b. (a -> b) -> a -> b
$ Buffer -> ByteString -> IO Buffer
copy Buffer
forall b. Ptr b
payloadBuf ByteString
bs1
WindowSize -> Next -> IO Next
forall (m :: * -> *). Monad m => WindowSize -> Next -> m Next
getNext WindowSize
room (ByteString -> BufferWriter -> Next
B.Chunk ByteString
bs2 BufferWriter
writer)
where
getNext :: WindowSize -> Next -> m Next
getNext WindowSize
l Next
s = Next -> m Next
forall (m :: * -> *) a. Monad m => a -> m a
return (Next -> m Next) -> Next -> m Next
forall a b. (a -> b) -> a -> b
$ WindowSize -> Next -> Next
nextForBuilder WindowSize
l Next
s
nextForBuilder :: BytesFilled -> B.Next -> Next
nextForBuilder :: WindowSize -> Next -> Next
nextForBuilder WindowSize
len Next
B.Done
= WindowSize -> Maybe DynaNext -> Next
Next WindowSize
len Maybe DynaNext
forall a. Maybe a
Nothing
nextForBuilder WindowSize
len (B.More WindowSize
_ BufferWriter
writer)
= WindowSize -> Maybe DynaNext -> Next
Next WindowSize
len (Maybe DynaNext -> Next) -> Maybe DynaNext -> Next
forall a b. (a -> b) -> a -> b
$ DynaNext -> Maybe DynaNext
forall a. a -> Maybe a
Just (Leftover -> DynaNext
fillBufBuilder (BufferWriter -> Leftover
LOne BufferWriter
writer))
nextForBuilder WindowSize
len (B.Chunk ByteString
bs BufferWriter
writer)
= WindowSize -> Maybe DynaNext -> Next
Next WindowSize
len (Maybe DynaNext -> Next) -> Maybe DynaNext -> Next
forall a b. (a -> b) -> a -> b
$ DynaNext -> Maybe DynaNext
forall a. a -> Maybe a
Just (Leftover -> DynaNext
fillBufBuilder (ByteString -> BufferWriter -> Leftover
LTwo ByteString
bs BufferWriter
writer))
runStreamBuilder :: Buffer -> BufferSize -> TBQueue RspStreaming
-> IO (Leftover, Bool, BytesFilled)
runStreamBuilder :: Buffer
-> WindowSize
-> TBQueue RspStreaming
-> IO (Leftover, Bool, WindowSize)
runStreamBuilder Buffer
buf0 WindowSize
room0 TBQueue RspStreaming
sq = Buffer
-> WindowSize -> WindowSize -> IO (Leftover, Bool, WindowSize)
loop Buffer
buf0 WindowSize
room0 WindowSize
0
where
loop :: Buffer
-> WindowSize -> WindowSize -> IO (Leftover, Bool, WindowSize)
loop !Buffer
buf !WindowSize
room !WindowSize
total = do
Maybe RspStreaming
mbuilder <- STM (Maybe RspStreaming) -> IO (Maybe RspStreaming)
forall a. STM a -> IO a
atomically (STM (Maybe RspStreaming) -> IO (Maybe RspStreaming))
-> STM (Maybe RspStreaming) -> IO (Maybe RspStreaming)
forall a b. (a -> b) -> a -> b
$ TBQueue RspStreaming -> STM (Maybe RspStreaming)
forall a. TBQueue a -> STM (Maybe a)
tryReadTBQueue TBQueue RspStreaming
sq
case Maybe RspStreaming
mbuilder of
Maybe RspStreaming
Nothing -> (Leftover, Bool, WindowSize) -> IO (Leftover, Bool, WindowSize)
forall (m :: * -> *) a. Monad m => a -> m a
return (Leftover
LZero, Bool
True, WindowSize
total)
Just (RSBuilder Builder
builder) -> do
(WindowSize
len, Next
signal) <- Builder -> BufferWriter
B.runBuilder Builder
builder Buffer
buf WindowSize
room
let !total' :: WindowSize
total' = WindowSize
total WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
len
case Next
signal of
Next
B.Done -> Buffer
-> WindowSize -> WindowSize -> IO (Leftover, Bool, WindowSize)
loop (Buffer
buf Buffer -> WindowSize -> Buffer
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
len) (WindowSize
room WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
len) WindowSize
total'
B.More WindowSize
_ BufferWriter
writer -> (Leftover, Bool, WindowSize) -> IO (Leftover, Bool, WindowSize)
forall (m :: * -> *) a. Monad m => a -> m a
return (BufferWriter -> Leftover
LOne BufferWriter
writer, Bool
True, WindowSize
total')
B.Chunk ByteString
bs BufferWriter
writer -> (Leftover, Bool, WindowSize) -> IO (Leftover, Bool, WindowSize)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> BufferWriter -> Leftover
LTwo ByteString
bs BufferWriter
writer, Bool
True, WindowSize
total')
Just RspStreaming
RSFlush -> (Leftover, Bool, WindowSize) -> IO (Leftover, Bool, WindowSize)
forall (m :: * -> *) a. Monad m => a -> m a
return (Leftover
LZero, Bool
True, WindowSize
total)
Just RspStreaming
RSFinish -> (Leftover, Bool, WindowSize) -> IO (Leftover, Bool, WindowSize)
forall (m :: * -> *) a. Monad m => a -> m a
return (Leftover
LZero, Bool
False, WindowSize
total)
fillBufStream :: Leftover -> TBQueue RspStreaming -> Stream -> DynaNext
fillBufStream :: Leftover -> TBQueue RspStreaming -> Stream -> DynaNext
fillBufStream Leftover
leftover0 TBQueue RspStreaming
sq Stream
strm Buffer
buf0 WindowSize
siz0 WindowSize
lim0 = do
let payloadBuf :: Ptr b
payloadBuf = Buffer
buf0 Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
frameHeaderLength
room0 :: WindowSize
room0 = WindowSize -> WindowSize -> WindowSize
forall a. Ord a => a -> a -> a
min (WindowSize
siz0 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
frameHeaderLength) WindowSize
lim0
case Leftover
leftover0 of
Leftover
LZero -> do
(Leftover
leftover, Bool
cont, WindowSize
len) <- Buffer
-> WindowSize
-> TBQueue RspStreaming
-> IO (Leftover, Bool, WindowSize)
runStreamBuilder Buffer
forall b. Ptr b
payloadBuf WindowSize
room0 TBQueue RspStreaming
sq
Leftover -> Bool -> WindowSize -> IO Next
forall (m :: * -> *).
Monad m =>
Leftover -> Bool -> WindowSize -> m Next
getNext Leftover
leftover Bool
cont WindowSize
len
LOne BufferWriter
writer -> BufferWriter -> DynaNext
forall a.
(Ptr a -> WindowSize -> IO (WindowSize, Next))
-> Ptr a -> WindowSize -> WindowSize -> IO Next
write BufferWriter
writer Buffer
forall b. Ptr b
payloadBuf WindowSize
room0 WindowSize
0
LTwo ByteString
bs BufferWriter
writer
| ByteString -> WindowSize
BS.length ByteString
bs WindowSize -> WindowSize -> Bool
forall a. Ord a => a -> a -> Bool
<= WindowSize
room0 -> do
Buffer
buf1 <- Buffer -> ByteString -> IO Buffer
copy Buffer
forall b. Ptr b
payloadBuf ByteString
bs
let len :: WindowSize
len = ByteString -> WindowSize
BS.length ByteString
bs
BufferWriter -> DynaNext
forall a.
(Ptr a -> WindowSize -> IO (WindowSize, Next))
-> Ptr a -> WindowSize -> WindowSize -> IO Next
write BufferWriter
writer Buffer
buf1 (WindowSize
room0 WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
len) WindowSize
len
| Bool
otherwise -> do
let (ByteString
bs1,ByteString
bs2) = WindowSize -> ByteString -> (ByteString, ByteString)
BS.splitAt WindowSize
room0 ByteString
bs
IO Buffer -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Buffer -> IO ()) -> IO Buffer -> IO ()
forall a b. (a -> b) -> a -> b
$ Buffer -> ByteString -> IO Buffer
copy Buffer
forall b. Ptr b
payloadBuf ByteString
bs1
Leftover -> Bool -> WindowSize -> IO Next
forall (m :: * -> *).
Monad m =>
Leftover -> Bool -> WindowSize -> m Next
getNext (ByteString -> BufferWriter -> Leftover
LTwo ByteString
bs2 BufferWriter
writer) Bool
True WindowSize
room0
where
getNext :: Leftover -> Bool -> WindowSize -> m Next
getNext Leftover
l Bool
b WindowSize
r = Next -> m Next
forall (m :: * -> *) a. Monad m => a -> m a
return (Next -> m Next) -> Next -> m Next
forall a b. (a -> b) -> a -> b
$ TBQueue RspStreaming
-> Stream -> Leftover -> Bool -> WindowSize -> Next
nextForStream TBQueue RspStreaming
sq Stream
strm Leftover
l Bool
b WindowSize
r
write :: (Ptr a -> WindowSize -> IO (WindowSize, Next))
-> Ptr a -> WindowSize -> WindowSize -> IO Next
write Ptr a -> WindowSize -> IO (WindowSize, Next)
writer1 Ptr a
buf WindowSize
room WindowSize
sofar = do
(WindowSize
len, Next
signal) <- Ptr a -> WindowSize -> IO (WindowSize, Next)
writer1 Ptr a
buf WindowSize
room
case Next
signal of
Next
B.Done -> do
(Leftover
leftover, Bool
cont, WindowSize
extra) <- Buffer
-> WindowSize
-> TBQueue RspStreaming
-> IO (Leftover, Bool, WindowSize)
runStreamBuilder (Ptr a
buf Ptr a -> WindowSize -> Buffer
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
len) (WindowSize
room WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
len) TBQueue RspStreaming
sq
let !total :: WindowSize
total = WindowSize
sofar WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
len WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
extra
Leftover -> Bool -> WindowSize -> IO Next
forall (m :: * -> *).
Monad m =>
Leftover -> Bool -> WindowSize -> m Next
getNext Leftover
leftover Bool
cont WindowSize
total
B.More WindowSize
_ BufferWriter
writer -> do
let !total :: WindowSize
total = WindowSize
sofar WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
len
Leftover -> Bool -> WindowSize -> IO Next
forall (m :: * -> *).
Monad m =>
Leftover -> Bool -> WindowSize -> m Next
getNext (BufferWriter -> Leftover
LOne BufferWriter
writer) Bool
True WindowSize
total
B.Chunk ByteString
bs BufferWriter
writer -> do
let !total :: WindowSize
total = WindowSize
sofar WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
+ WindowSize
len
Leftover -> Bool -> WindowSize -> IO Next
forall (m :: * -> *).
Monad m =>
Leftover -> Bool -> WindowSize -> m Next
getNext (ByteString -> BufferWriter -> Leftover
LTwo ByteString
bs BufferWriter
writer) Bool
True WindowSize
total
nextForStream :: TBQueue RspStreaming -> Stream
-> Leftover -> Bool -> BytesFilled
-> Next
nextForStream :: TBQueue RspStreaming
-> Stream -> Leftover -> Bool -> WindowSize -> Next
nextForStream TBQueue RspStreaming
_ Stream
_ Leftover
_ Bool
False WindowSize
len = WindowSize -> Maybe DynaNext -> Next
Next WindowSize
len Maybe DynaNext
forall a. Maybe a
Nothing
nextForStream TBQueue RspStreaming
sq Stream
strm Leftover
leftOrZero Bool
True WindowSize
len =
WindowSize -> Maybe DynaNext -> Next
Next WindowSize
len (Maybe DynaNext -> Next) -> Maybe DynaNext -> Next
forall a b. (a -> b) -> a -> b
$ DynaNext -> Maybe DynaNext
forall a. a -> Maybe a
Just (Leftover -> TBQueue RspStreaming -> Stream -> DynaNext
fillBufStream Leftover
leftOrZero TBQueue RspStreaming
sq Stream
strm)
fillBufFile :: PositionRead -> FileOffset -> ByteCount -> IO () -> DynaNext
fillBufFile :: PositionRead -> FileOffset -> FileOffset -> IO () -> DynaNext
fillBufFile PositionRead
pread FileOffset
start FileOffset
bytes IO ()
refresh Buffer
buf WindowSize
siz WindowSize
lim = do
let payloadBuf :: Ptr b
payloadBuf = Buffer
buf Buffer -> WindowSize -> Ptr b
forall a b. Ptr a -> WindowSize -> Ptr b
`plusPtr` WindowSize
frameHeaderLength
room :: WindowSize
room = WindowSize -> WindowSize -> WindowSize
forall a. Ord a => a -> a -> a
min (WindowSize
siz WindowSize -> WindowSize -> WindowSize
forall a. Num a => a -> a -> a
- WindowSize
frameHeaderLength) WindowSize
lim
FileOffset
len <- PositionRead
pread FileOffset
start (WindowSize -> FileOffset -> FileOffset
mini WindowSize
room FileOffset
bytes) Buffer
forall b. Ptr b
payloadBuf
IO ()
refresh
let len' :: WindowSize
len' = FileOffset -> WindowSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral FileOffset
len
Next -> IO Next
forall (m :: * -> *) a. Monad m => a -> m a
return (Next -> IO Next) -> Next -> IO Next
forall a b. (a -> b) -> a -> b
$ WindowSize
-> PositionRead -> FileOffset -> FileOffset -> IO () -> Next
nextForFile WindowSize
len' PositionRead
pread (FileOffset
start FileOffset -> FileOffset -> FileOffset
forall a. Num a => a -> a -> a
+ FileOffset
len) (FileOffset
bytes FileOffset -> FileOffset -> FileOffset
forall a. Num a => a -> a -> a
- FileOffset
len) IO ()
refresh
nextForFile :: BytesFilled -> PositionRead -> FileOffset -> ByteCount -> IO () -> Next
nextForFile :: WindowSize
-> PositionRead -> FileOffset -> FileOffset -> IO () -> Next
nextForFile WindowSize
0 PositionRead
_ FileOffset
_ FileOffset
_ IO ()
_ = WindowSize -> Maybe DynaNext -> Next
Next WindowSize
0 Maybe DynaNext
forall a. Maybe a
Nothing
nextForFile WindowSize
len PositionRead
_ FileOffset
_ FileOffset
0 IO ()
_ = WindowSize -> Maybe DynaNext -> Next
Next WindowSize
len Maybe DynaNext
forall a. Maybe a
Nothing
nextForFile WindowSize
len PositionRead
pread FileOffset
start FileOffset
bytes IO ()
refresh =
WindowSize -> Maybe DynaNext -> Next
Next WindowSize
len (Maybe DynaNext -> Next) -> Maybe DynaNext -> Next
forall a b. (a -> b) -> a -> b
$ DynaNext -> Maybe DynaNext
forall a. a -> Maybe a
Just (PositionRead -> FileOffset -> FileOffset -> IO () -> DynaNext
fillBufFile PositionRead
pread FileOffset
start FileOffset
bytes IO ()
refresh)
{-# INLINE mini #-}
mini :: Int -> Int64 -> Int64
mini :: WindowSize -> FileOffset -> FileOffset
mini WindowSize
i FileOffset
n
| WindowSize -> FileOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral WindowSize
i FileOffset -> FileOffset -> Bool
forall a. Ord a => a -> a -> Bool
< FileOffset
n = WindowSize -> FileOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral WindowSize
i
| Bool
otherwise = FileOffset
n