blob: 8c121334033e06ce08ac1901181aaea843f473fa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
{-# LANGUAGE OverloadedStrings #-}
module Hsm.Core.Zmq
( withSocket
) where
import Data.Text (Text)
import Effectful (Eff, IOE, (:>))
import Effectful.Log (Log, LogLevel(LogTrace))
import Effectful.Resource (Resource, allocate)
import Hsm.Core.Log (withLogIO)
import System.ZMQ4 qualified as Z
withSocket ::
forall t es. (Z.SocketType t, IOE :> es, Log :> es, Resource :> es)
=> t
-> Eff es (Z.Socket t)
withSocket stype = withLogIO >>= bracket
where
bracket :: (LogLevel -> Text -> IO ()) -> Eff es (Z.Socket t)
bracket logIO = snd . snd <$> allocate acquire release
where
acquire :: IO (Z.Context, Z.Socket t)
acquire = do
logIO LogTrace "Acquiring ZMQ context"
cont <- Z.context
sock <- Z.socket cont stype
return (cont, sock)
--
release :: (Z.Context, Z.Socket t) -> IO ()
release (cont, sock) = do
logIO LogTrace "Releasing ZMQ context"
Z.close sock
Z.shutdown cont
|