module Network.HTTP2.H2.Config where
import Data.IORef
import Foreign.Marshal.Alloc (free, mallocBytes)
import Network.Socket
import Network.Socket.ByteString (sendAll)
import qualified System.TimeManager as T
import Network.HPACK
import Network.HTTP2.H2.File
import Network.HTTP2.H2.ReadN
import Network.HTTP2.H2.Types
allocSimpleConfig :: Socket -> BufferSize -> IO Config
allocSimpleConfig :: Socket -> BufferSize -> IO Config
allocSimpleConfig Socket
s BufferSize
bufsiz = do
Ptr Word8
buf <- BufferSize -> IO (Ptr Word8)
forall a. BufferSize -> IO (Ptr a)
mallocBytes BufferSize
bufsiz
IORef (Maybe ByteString)
ref <- Maybe ByteString -> IO (IORef (Maybe ByteString))
forall a. a -> IO (IORef a)
newIORef Maybe ByteString
forall a. Maybe a
Nothing
Manager
timmgr <- BufferSize -> IO Manager
T.initialize (BufferSize -> IO Manager) -> BufferSize -> IO Manager
forall a b. (a -> b) -> a -> b
$ BufferSize
30 BufferSize -> BufferSize -> BufferSize
forall a. Num a => a -> a -> a
* BufferSize
1000000
SockAddr
mysa <- Socket -> IO SockAddr
getSocketName Socket
s
SockAddr
peersa <- Socket -> IO SockAddr
getPeerName Socket
s
let config :: Config
config =
Config :: Ptr Word8
-> BufferSize
-> (ByteString -> IO ())
-> (BufferSize -> IO ByteString)
-> PositionReadMaker
-> Manager
-> SockAddr
-> SockAddr
-> Config
Config
{ confWriteBuffer :: Ptr Word8
confWriteBuffer = Ptr Word8
buf
, confBufferSize :: BufferSize
confBufferSize = BufferSize
bufsiz
, confSendAll :: ByteString -> IO ()
confSendAll = Socket -> ByteString -> IO ()
sendAll Socket
s
, confReadN :: BufferSize -> IO ByteString
confReadN = Socket -> IORef (Maybe ByteString) -> BufferSize -> IO ByteString
defaultReadN Socket
s IORef (Maybe ByteString)
ref
, confPositionReadMaker :: PositionReadMaker
confPositionReadMaker = PositionReadMaker
defaultPositionReadMaker
, confTimeoutManager :: Manager
confTimeoutManager = Manager
timmgr
, confMySockAddr :: SockAddr
confMySockAddr = SockAddr
mysa
, confPeerSockAddr :: SockAddr
confPeerSockAddr = SockAddr
peersa
}
Config -> IO Config
forall (m :: * -> *) a. Monad m => a -> m a
return Config
config
freeSimpleConfig :: Config -> IO ()
freeSimpleConfig :: Config -> IO ()
freeSimpleConfig Config
conf = do
Ptr Word8 -> IO ()
forall a. Ptr a -> IO ()
free (Ptr Word8 -> IO ()) -> Ptr Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Config -> Ptr Word8
confWriteBuffer Config
conf
Manager -> IO ()
T.killManager (Manager -> IO ()) -> Manager -> IO ()
forall a b. (a -> b) -> a -> b
$ Config -> Manager
confTimeoutManager Config
conf