{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GADTs              #-}
{-# LANGUAGE StandaloneDeriving #-}

-- | /Warning/: This is an internal module and subject
-- to change without notice.
module System.ZMQ4.Internal
    ( Context           (..)
    , Socket            (..)
    , SocketRepr        (..)
    , SocketType        (..)
    , SocketLike        (..)
    , Message           (..)
    , Flag              (..)
    , Timeout
    , Size
    , Switch            (..)
    , EventType         (..)
    , EventMsg          (..)
    , SecurityMechanism (..)
    , KeyFormat         (..)

    , messageOf
    , messageOfLazy
    , messageClose
    , messageFree
    , messageInit
    , messageInitSize
    , setIntOpt
    , setStrOpt
    , getIntOpt
    , getStrOpt
    , getInt32Option
    , setInt32OptFromRestricted
    , ctxIntOption
    , setCtxIntOption
    , getBytesOpt
    , getByteStringOpt
    , setByteStringOpt

    , z85Encode
    , z85Decode

    , toZMQFlag
    , combine
    , combineFlags
    , mkSocketRepr
    , closeSock
    , onSocket

    , bool2cint
    , toSwitch
    , fromSwitch
    , events2cint
    , eventMessage

    , toMechanism
    , fromMechanism

    , getKey
    ) where

import Control.Applicative
import Control.Monad (foldM_, when, void)
import Control.Monad.IO.Class
import Data.IORef (IORef, mkWeakIORef, readIORef, atomicModifyIORef)

import Foreign hiding (throwIfNull, void)
import Foreign.C.String
import Foreign.C.Types (CInt)

import Data.IORef (newIORef)
import Data.Restricted
import Data.Typeable
import Prelude

import System.Posix.Types (Fd(..))
import System.ZMQ4.Internal.Base
import System.ZMQ4.Internal.Error

import qualified Data.ByteString        as SB
import qualified Data.ByteString.Lazy   as LB
import qualified Data.ByteString.Unsafe as UB

type Timeout = Int64
type Size    = Word

-- | Flags to apply on send operations (cf. man zmq_send)
data Flag =
    DontWait -- ^ ZMQ_DONTWAIT (Only relevant on Windows.)
  | SendMore -- ^ ZMQ_SNDMORE
  deriving (Flag -> Flag -> Bool
(Flag -> Flag -> Bool) -> (Flag -> Flag -> Bool) -> Eq Flag
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Flag -> Flag -> Bool
$c/= :: Flag -> Flag -> Bool
== :: Flag -> Flag -> Bool
$c== :: Flag -> Flag -> Bool
Eq, Eq Flag
Eq Flag =>
(Flag -> Flag -> Ordering)
-> (Flag -> Flag -> Bool)
-> (Flag -> Flag -> Bool)
-> (Flag -> Flag -> Bool)
-> (Flag -> Flag -> Bool)
-> (Flag -> Flag -> Flag)
-> (Flag -> Flag -> Flag)
-> Ord Flag
Flag -> Flag -> Bool
Flag -> Flag -> Ordering
Flag -> Flag -> Flag
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Flag -> Flag -> Flag
$cmin :: Flag -> Flag -> Flag
max :: Flag -> Flag -> Flag
$cmax :: Flag -> Flag -> Flag
>= :: Flag -> Flag -> Bool
$c>= :: Flag -> Flag -> Bool
> :: Flag -> Flag -> Bool
$c> :: Flag -> Flag -> Bool
<= :: Flag -> Flag -> Bool
$c<= :: Flag -> Flag -> Bool
< :: Flag -> Flag -> Bool
$c< :: Flag -> Flag -> Bool
compare :: Flag -> Flag -> Ordering
$ccompare :: Flag -> Flag -> Ordering
$cp1Ord :: Eq Flag
Ord, Int -> Flag -> ShowS
[Flag] -> ShowS
Flag -> String
(Int -> Flag -> ShowS)
-> (Flag -> String) -> ([Flag] -> ShowS) -> Show Flag
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Flag] -> ShowS
$cshowList :: [Flag] -> ShowS
show :: Flag -> String
$cshow :: Flag -> String
showsPrec :: Int -> Flag -> ShowS
$cshowsPrec :: Int -> Flag -> ShowS
Show)

-- | Configuration switch
data Switch =
    Default -- ^ Use default setting
  | On      -- ^ Activate setting
  | Off     -- ^ De-activate setting
  deriving (Switch -> Switch -> Bool
(Switch -> Switch -> Bool)
-> (Switch -> Switch -> Bool) -> Eq Switch
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Switch -> Switch -> Bool
$c/= :: Switch -> Switch -> Bool
== :: Switch -> Switch -> Bool
$c== :: Switch -> Switch -> Bool
Eq, Eq Switch
Eq Switch =>
(Switch -> Switch -> Ordering)
-> (Switch -> Switch -> Bool)
-> (Switch -> Switch -> Bool)
-> (Switch -> Switch -> Bool)
-> (Switch -> Switch -> Bool)
-> (Switch -> Switch -> Switch)
-> (Switch -> Switch -> Switch)
-> Ord Switch
Switch -> Switch -> Bool
Switch -> Switch -> Ordering
Switch -> Switch -> Switch
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Switch -> Switch -> Switch
$cmin :: Switch -> Switch -> Switch
max :: Switch -> Switch -> Switch
$cmax :: Switch -> Switch -> Switch
>= :: Switch -> Switch -> Bool
$c>= :: Switch -> Switch -> Bool
> :: Switch -> Switch -> Bool
$c> :: Switch -> Switch -> Bool
<= :: Switch -> Switch -> Bool
$c<= :: Switch -> Switch -> Bool
< :: Switch -> Switch -> Bool
$c< :: Switch -> Switch -> Bool
compare :: Switch -> Switch -> Ordering
$ccompare :: Switch -> Switch -> Ordering
$cp1Ord :: Eq Switch
Ord, Int -> Switch -> ShowS
[Switch] -> ShowS
Switch -> String
(Int -> Switch -> ShowS)
-> (Switch -> String) -> ([Switch] -> ShowS) -> Show Switch
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Switch] -> ShowS
$cshowList :: [Switch] -> ShowS
show :: Switch -> String
$cshow :: Switch -> String
showsPrec :: Int -> Switch -> ShowS
$cshowsPrec :: Int -> Switch -> ShowS
Show)

-- | Event types to monitor.
data EventType =
    ConnectedEvent
  | ConnectDelayedEvent
  | ConnectRetriedEvent
  | ListeningEvent
  | BindFailedEvent
  | AcceptedEvent
  | AcceptFailedEvent
  | ClosedEvent
  | CloseFailedEvent
  | DisconnectedEvent
  | MonitorStoppedEvent
  | AllEvents
  deriving (EventType -> EventType -> Bool
(EventType -> EventType -> Bool)
-> (EventType -> EventType -> Bool) -> Eq EventType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EventType -> EventType -> Bool
$c/= :: EventType -> EventType -> Bool
== :: EventType -> EventType -> Bool
$c== :: EventType -> EventType -> Bool
Eq, Eq EventType
Eq EventType =>
(EventType -> EventType -> Ordering)
-> (EventType -> EventType -> Bool)
-> (EventType -> EventType -> Bool)
-> (EventType -> EventType -> Bool)
-> (EventType -> EventType -> Bool)
-> (EventType -> EventType -> EventType)
-> (EventType -> EventType -> EventType)
-> Ord EventType
EventType -> EventType -> Bool
EventType -> EventType -> Ordering
EventType -> EventType -> EventType
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: EventType -> EventType -> EventType
$cmin :: EventType -> EventType -> EventType
max :: EventType -> EventType -> EventType
$cmax :: EventType -> EventType -> EventType
>= :: EventType -> EventType -> Bool
$c>= :: EventType -> EventType -> Bool
> :: EventType -> EventType -> Bool
$c> :: EventType -> EventType -> Bool
<= :: EventType -> EventType -> Bool
$c<= :: EventType -> EventType -> Bool
< :: EventType -> EventType -> Bool
$c< :: EventType -> EventType -> Bool
compare :: EventType -> EventType -> Ordering
$ccompare :: EventType -> EventType -> Ordering
$cp1Ord :: Eq EventType
Ord, Int -> EventType -> ShowS
[EventType] -> ShowS
EventType -> String
(Int -> EventType -> ShowS)
-> (EventType -> String)
-> ([EventType] -> ShowS)
-> Show EventType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EventType] -> ShowS
$cshowList :: [EventType] -> ShowS
show :: EventType -> String
$cshow :: EventType -> String
showsPrec :: Int -> EventType -> ShowS
$cshowsPrec :: Int -> EventType -> ShowS
Show)

-- | Event Message to receive when monitoring socket events.
data EventMsg =
    Connected      !SB.ByteString !Fd
  | ConnectDelayed !SB.ByteString
  | ConnectRetried !SB.ByteString !Int
  | Listening      !SB.ByteString !Fd
  | BindFailed     !SB.ByteString !Int
  | Accepted       !SB.ByteString !Fd
  | AcceptFailed   !SB.ByteString !Int
  | Closed         !SB.ByteString !Fd
  | CloseFailed    !SB.ByteString !Int
  | Disconnected   !SB.ByteString !Fd
  | MonitorStopped !SB.ByteString !Int
  deriving (EventMsg -> EventMsg -> Bool
(EventMsg -> EventMsg -> Bool)
-> (EventMsg -> EventMsg -> Bool) -> Eq EventMsg
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EventMsg -> EventMsg -> Bool
$c/= :: EventMsg -> EventMsg -> Bool
== :: EventMsg -> EventMsg -> Bool
$c== :: EventMsg -> EventMsg -> Bool
Eq, Int -> EventMsg -> ShowS
[EventMsg] -> ShowS
EventMsg -> String
(Int -> EventMsg -> ShowS)
-> (EventMsg -> String) -> ([EventMsg] -> ShowS) -> Show EventMsg
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EventMsg] -> ShowS
$cshowList :: [EventMsg] -> ShowS
show :: EventMsg -> String
$cshow :: EventMsg -> String
showsPrec :: Int -> EventMsg -> ShowS
$cshowsPrec :: Int -> EventMsg -> ShowS
Show)

data SecurityMechanism
  = Null
  | Plain
  | Curve
  deriving (SecurityMechanism -> SecurityMechanism -> Bool
(SecurityMechanism -> SecurityMechanism -> Bool)
-> (SecurityMechanism -> SecurityMechanism -> Bool)
-> Eq SecurityMechanism
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SecurityMechanism -> SecurityMechanism -> Bool
$c/= :: SecurityMechanism -> SecurityMechanism -> Bool
== :: SecurityMechanism -> SecurityMechanism -> Bool
$c== :: SecurityMechanism -> SecurityMechanism -> Bool
Eq, Int -> SecurityMechanism -> ShowS
[SecurityMechanism] -> ShowS
SecurityMechanism -> String
(Int -> SecurityMechanism -> ShowS)
-> (SecurityMechanism -> String)
-> ([SecurityMechanism] -> ShowS)
-> Show SecurityMechanism
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SecurityMechanism] -> ShowS
$cshowList :: [SecurityMechanism] -> ShowS
show :: SecurityMechanism -> String
$cshow :: SecurityMechanism -> String
showsPrec :: Int -> SecurityMechanism -> ShowS
$cshowsPrec :: Int -> SecurityMechanism -> ShowS
Show)

data KeyFormat a where
  BinaryFormat :: KeyFormat Div4
  TextFormat   :: KeyFormat Div5

deriving instance Eq   (KeyFormat a)
deriving instance Show (KeyFormat a)

-- | A 0MQ context representation.
newtype Context = Context { Context -> ZMQCtx
_ctx :: ZMQCtx }

deriving instance Typeable Context

-- | A 0MQ Socket.
newtype Socket a = Socket
  { Socket a -> SocketRepr
_socketRepr :: SocketRepr }

data SocketRepr = SocketRepr
  { SocketRepr -> ZMQCtx
_socket   :: ZMQSocket
  , SocketRepr -> IORef Bool
_sockLive :: IORef Bool
  }

-- | Socket types.
class SocketType a where
    zmqSocketType :: a -> ZMQSocketType

class SocketLike s where
    toSocket :: s t -> Socket t

instance SocketLike Socket where
    toSocket :: Socket t -> Socket t
toSocket = Socket t -> Socket t
forall a. a -> a
id

-- A 0MQ Message representation.
newtype Message = Message { Message -> ZMQMsgPtr
msgPtr :: ZMQMsgPtr }

-- internal helpers:

onSocket :: String -> Socket a -> (ZMQSocket -> IO b) -> IO b
onSocket :: String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket _func :: String
_func (Socket (SocketRepr sock :: ZMQCtx
sock _state :: IORef Bool
_state)) act :: ZMQCtx -> IO b
act = ZMQCtx -> IO b
act ZMQCtx
sock
{-# INLINE onSocket #-}

mkSocketRepr :: SocketType t => t -> Context -> IO SocketRepr
mkSocketRepr :: t -> Context -> IO SocketRepr
mkSocketRepr t :: t
t c :: Context
c = do
    let ty :: CInt
ty = ZMQSocketType -> CInt
typeVal (t -> ZMQSocketType
forall a. SocketType a => a -> ZMQSocketType
zmqSocketType t
t)
    ZMQCtx
s   <- String -> IO ZMQCtx -> IO ZMQCtx
forall a. String -> IO (Ptr a) -> IO (Ptr a)
throwIfNull "mkSocketRepr" (ZMQCtx -> CInt -> IO ZMQCtx
c_zmq_socket (Context -> ZMQCtx
_ctx Context
c) CInt
ty)
    IORef Bool
ref <- Bool -> IO (IORef Bool)
forall a. a -> IO (IORef a)
newIORef Bool
True
    IORef Bool -> IO () -> IO ()
forall a. IORef a -> IO () -> IO ()
addFinalizer IORef Bool
ref (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        Bool
alive <- IORef Bool -> IO Bool
forall a. IORef a -> IO a
readIORef IORef Bool
ref
        Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
alive (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ ZMQCtx -> IO CInt
c_zmq_close ZMQCtx
s IO CInt -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    SocketRepr -> IO SocketRepr
forall (m :: * -> *) a. Monad m => a -> m a
return (ZMQCtx -> IORef Bool -> SocketRepr
SocketRepr ZMQCtx
s IORef Bool
ref)
  where
    addFinalizer :: IORef a -> IO () -> IO ()
addFinalizer r :: IORef a
r f :: IO ()
f = IORef a -> IO () -> IO (Weak (IORef a))
forall a. IORef a -> IO () -> IO (Weak (IORef a))
mkWeakIORef IORef a
r IO ()
f IO (Weak (IORef a)) -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

closeSock :: SocketRepr -> IO ()
closeSock :: SocketRepr -> IO ()
closeSock (SocketRepr s :: ZMQCtx
s status :: IORef Bool
status) = do
  Bool
alive <- IORef Bool -> (Bool -> (Bool, Bool)) -> IO Bool
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef IORef Bool
status (\b :: Bool
b -> (Bool
False, Bool
b))
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
alive (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1_ "close" (IO CInt -> IO ()) -> (ZMQCtx -> IO CInt) -> ZMQCtx -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ZMQCtx -> IO CInt
c_zmq_close (ZMQCtx -> IO ()) -> ZMQCtx -> IO ()
forall a b. (a -> b) -> a -> b
$ ZMQCtx
s

messageOf :: SB.ByteString -> IO Message
messageOf :: ByteString -> IO Message
messageOf b :: ByteString
b = ByteString -> (CStringLen -> IO Message) -> IO Message
forall a. ByteString -> (CStringLen -> IO a) -> IO a
UB.unsafeUseAsCStringLen ByteString
b ((CStringLen -> IO Message) -> IO Message)
-> (CStringLen -> IO Message) -> IO Message
forall a b. (a -> b) -> a -> b
$ \(cstr :: Ptr CChar
cstr, len :: Int
len) -> do
    Message
msg <- Size -> IO Message
messageInitSize (Int -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
    Ptr CChar
data_ptr <- ZMQMsgPtr -> IO (Ptr CChar)
forall a. ZMQMsgPtr -> IO (Ptr a)
c_zmq_msg_data (Message -> ZMQMsgPtr
msgPtr Message
msg)
    Ptr CChar -> Ptr CChar -> Int -> IO ()
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes Ptr CChar
data_ptr Ptr CChar
cstr Int
len
    Message -> IO Message
forall (m :: * -> *) a. Monad m => a -> m a
return Message
msg

messageOfLazy :: LB.ByteString -> IO Message
messageOfLazy :: ByteString -> IO Message
messageOfLazy lbs :: ByteString
lbs = do
    Message
msg <- Size -> IO Message
messageInitSize (Int64 -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
len)
    Ptr Any
data_ptr <- ZMQMsgPtr -> IO (Ptr Any)
forall a. ZMQMsgPtr -> IO (Ptr a)
c_zmq_msg_data (Message -> ZMQMsgPtr
msgPtr Message
msg)
    let fn :: Int -> ByteString -> IO Int
fn offset :: Int
offset bs :: ByteString
bs = ByteString -> (CStringLen -> IO Int) -> IO Int
forall a. ByteString -> (CStringLen -> IO a) -> IO a
UB.unsafeUseAsCStringLen ByteString
bs ((CStringLen -> IO Int) -> IO Int)
-> (CStringLen -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \(cstr :: Ptr CChar
cstr, str_len :: Int
str_len) -> do
        Ptr CChar -> Ptr CChar -> Int -> IO ()
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes (Ptr Any
data_ptr Ptr Any -> Int -> Ptr CChar
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
offset) Ptr CChar
cstr Int
str_len
        Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
str_len)
    (Int -> ByteString -> IO Int) -> Int -> [ByteString] -> IO ()
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m ()
foldM_ Int -> ByteString -> IO Int
fn 0 (ByteString -> [ByteString]
LB.toChunks ByteString
lbs)
    Message -> IO Message
forall (m :: * -> *) a. Monad m => a -> m a
return Message
msg
 where
    len :: Int64
len = ByteString -> Int64
LB.length ByteString
lbs

messageClose :: Message -> IO ()
messageClose :: Message -> IO ()
messageClose (Message ptr :: ZMQMsgPtr
ptr) = do
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1_ "messageClose" (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ ZMQMsgPtr -> IO CInt
c_zmq_msg_close ZMQMsgPtr
ptr
    ZMQMsgPtr -> IO ()
forall a. Ptr a -> IO ()
free ZMQMsgPtr
ptr

messageFree :: Message -> IO ()
messageFree :: Message -> IO ()
messageFree (Message ptr :: ZMQMsgPtr
ptr) = ZMQMsgPtr -> IO ()
forall a. Ptr a -> IO ()
free ZMQMsgPtr
ptr

messageInit :: IO Message
messageInit :: IO Message
messageInit = do
    ZMQMsgPtr
ptr <- ZMQMsg -> IO ZMQMsgPtr
forall a. Storable a => a -> IO (Ptr a)
new (ZMQCtx -> ZMQMsg
ZMQMsg ZMQCtx
forall a. Ptr a
nullPtr)
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1_ "messageInit" (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ ZMQMsgPtr -> IO CInt
c_zmq_msg_init ZMQMsgPtr
ptr
    Message -> IO Message
forall (m :: * -> *) a. Monad m => a -> m a
return (ZMQMsgPtr -> Message
Message ZMQMsgPtr
ptr)

messageInitSize :: Size -> IO Message
messageInitSize :: Size -> IO Message
messageInitSize s :: Size
s = do
    ZMQMsgPtr
ptr <- ZMQMsg -> IO ZMQMsgPtr
forall a. Storable a => a -> IO (Ptr a)
new (ZMQCtx -> ZMQMsg
ZMQMsg ZMQCtx
forall a. Ptr a
nullPtr)
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1_ "messageInitSize" (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$
        ZMQMsgPtr -> CSize -> IO CInt
c_zmq_msg_init_size ZMQMsgPtr
ptr (Size -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Size
s)
    Message -> IO Message
forall (m :: * -> *) a. Monad m => a -> m a
return (ZMQMsgPtr -> Message
Message ZMQMsgPtr
ptr)

setIntOpt :: (Storable b, Integral b) => Socket a -> ZMQOption -> b -> IO ()
setIntOpt :: Socket a -> ZMQOption -> b -> IO ()
setIntOpt sock :: Socket a
sock (ZMQOption o :: CInt
o) i :: b
i = String -> Socket a -> (ZMQCtx -> IO ()) -> IO ()
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket "setIntOpt" Socket a
sock ((ZMQCtx -> IO ()) -> IO ()) -> (ZMQCtx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \s :: ZMQCtx
s ->
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ "setIntOpt" (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ b -> (Ptr b -> IO CInt) -> IO CInt
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with b
i ((Ptr b -> IO CInt) -> IO CInt) -> (Ptr b -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr b
ptr ->
        ZMQCtx -> CInt -> ZMQCtx -> CSize -> IO CInt
c_zmq_setsockopt ZMQCtx
s (CInt -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
o)
                           (Ptr b -> ZMQCtx
forall a b. Ptr a -> Ptr b
castPtr Ptr b
ptr)
                           (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CSize) -> (b -> Int) -> b -> CSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Int
forall a. Storable a => a -> Int
sizeOf (b -> CSize) -> b -> CSize
forall a b. (a -> b) -> a -> b
$ b
i)

setCStrOpt :: ZMQSocket -> ZMQOption -> CStringLen -> IO CInt
setCStrOpt :: ZMQCtx -> ZMQOption -> CStringLen -> IO CInt
setCStrOpt s :: ZMQCtx
s (ZMQOption o :: CInt
o) (cstr :: Ptr CChar
cstr, len :: Int
len) =
    ZMQCtx -> CInt -> ZMQCtx -> CSize -> IO CInt
c_zmq_setsockopt ZMQCtx
s (CInt -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
o) (Ptr CChar -> ZMQCtx
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
cstr) (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)

setByteStringOpt :: Socket a -> ZMQOption -> SB.ByteString -> IO ()
setByteStringOpt :: Socket a -> ZMQOption -> ByteString -> IO ()
setByteStringOpt sock :: Socket a
sock opt :: ZMQOption
opt str :: ByteString
str = String -> Socket a -> (ZMQCtx -> IO ()) -> IO ()
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket "setByteStringOpt" Socket a
sock ((ZMQCtx -> IO ()) -> IO ()) -> (ZMQCtx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \s :: ZMQCtx
s ->
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ "setByteStringOpt" (IO CInt -> IO ())
-> ((CStringLen -> IO CInt) -> IO CInt)
-> (CStringLen -> IO CInt)
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> (CStringLen -> IO CInt) -> IO CInt
forall a. ByteString -> (CStringLen -> IO a) -> IO a
UB.unsafeUseAsCStringLen ByteString
str ((CStringLen -> IO CInt) -> IO ())
-> (CStringLen -> IO CInt) -> IO ()
forall a b. (a -> b) -> a -> b
$ ZMQCtx -> ZMQOption -> CStringLen -> IO CInt
setCStrOpt ZMQCtx
s ZMQOption
opt

setStrOpt :: Socket a -> ZMQOption -> String -> IO ()
setStrOpt :: Socket a -> ZMQOption -> String -> IO ()
setStrOpt sock :: Socket a
sock opt :: ZMQOption
opt str :: String
str = String -> Socket a -> (ZMQCtx -> IO ()) -> IO ()
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket "setStrOpt" Socket a
sock ((ZMQCtx -> IO ()) -> IO ()) -> (ZMQCtx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \s :: ZMQCtx
s ->
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ "setStrOpt" (IO CInt -> IO ())
-> ((CStringLen -> IO CInt) -> IO CInt)
-> (CStringLen -> IO CInt)
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> (CStringLen -> IO CInt) -> IO CInt
forall a. String -> (CStringLen -> IO a) -> IO a
withCStringLen String
str ((CStringLen -> IO CInt) -> IO ())
-> (CStringLen -> IO CInt) -> IO ()
forall a b. (a -> b) -> a -> b
$ ZMQCtx -> ZMQOption -> CStringLen -> IO CInt
setCStrOpt ZMQCtx
s ZMQOption
opt

getIntOpt :: (Storable b, Integral b) => Socket a -> ZMQOption -> b -> IO b
getIntOpt :: Socket a -> ZMQOption -> b -> IO b
getIntOpt sock :: Socket a
sock (ZMQOption o :: CInt
o) i :: b
i = String -> Socket a -> (ZMQCtx -> IO b) -> IO b
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket "getIntOpt" Socket a
sock ((ZMQCtx -> IO b) -> IO b) -> (ZMQCtx -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \s :: ZMQCtx
s ->
    b -> (Ptr b -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with b
i                         ((Ptr b -> IO b) -> IO b) -> (Ptr b -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \iptr :: Ptr b
iptr ->
    CSize -> (Ptr CSize -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CSize) -> Int -> CSize
forall a b. (a -> b) -> a -> b
$ b -> Int
forall a. Storable a => a -> Int
sizeOf b
i) ((Ptr CSize -> IO b) -> IO b) -> (Ptr CSize -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \jptr :: Ptr CSize
jptr -> do
        String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ "getIntOpt" (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$
            ZMQCtx -> CInt -> ZMQCtx -> Ptr CSize -> IO CInt
c_zmq_getsockopt ZMQCtx
s (CInt -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
o) (Ptr b -> ZMQCtx
forall a b. Ptr a -> Ptr b
castPtr Ptr b
iptr) Ptr CSize
jptr
        Ptr b -> IO b
forall a. Storable a => Ptr a -> IO a
peek Ptr b
iptr

getCStrOpt :: (CStringLen -> IO s) -> Socket a -> ZMQOption -> IO s
getCStrOpt :: (CStringLen -> IO s) -> Socket a -> ZMQOption -> IO s
getCStrOpt peekA :: CStringLen -> IO s
peekA sock :: Socket a
sock (ZMQOption o :: CInt
o) = String -> Socket a -> (ZMQCtx -> IO s) -> IO s
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket "getCStrOpt" Socket a
sock ((ZMQCtx -> IO s) -> IO s) -> (ZMQCtx -> IO s) -> IO s
forall a b. (a -> b) -> a -> b
$ \s :: ZMQCtx
s ->
    CSize -> (Ptr CSize -> IO s) -> IO s
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with 256        ((Ptr CSize -> IO s) -> IO s) -> (Ptr CSize -> IO s) -> IO s
forall a b. (a -> b) -> a -> b
$ \nptr :: Ptr CSize
nptr ->
    Int -> (Ptr CChar -> IO s) -> IO s
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes 256 ((Ptr CChar -> IO s) -> IO s) -> (Ptr CChar -> IO s) -> IO s
forall a b. (a -> b) -> a -> b
$ \bptr :: Ptr CChar
bptr -> do
        String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ "getCStrOpt" (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$
            ZMQCtx -> CInt -> ZMQCtx -> Ptr CSize -> IO CInt
c_zmq_getsockopt ZMQCtx
s (CInt -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
o) (Ptr CChar -> ZMQCtx
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
bptr) Ptr CSize
nptr
        Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
nptr IO CSize -> (CSize -> IO s) -> IO s
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \len :: CSize
len -> CStringLen -> IO s
peekA (Ptr CChar
bptr, CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
len)

getStrOpt :: Socket a -> ZMQOption -> IO String
getStrOpt :: Socket a -> ZMQOption -> IO String
getStrOpt = (CStringLen -> IO String) -> Socket a -> ZMQOption -> IO String
forall s a. (CStringLen -> IO s) -> Socket a -> ZMQOption -> IO s
getCStrOpt (Ptr CChar -> IO String
peekCString (Ptr CChar -> IO String)
-> (CStringLen -> Ptr CChar) -> CStringLen -> IO String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CStringLen -> Ptr CChar
forall a b. (a, b) -> a
fst)

getByteStringOpt :: Socket a -> ZMQOption -> IO SB.ByteString
getByteStringOpt :: Socket a -> ZMQOption -> IO ByteString
getByteStringOpt = (CStringLen -> IO ByteString)
-> Socket a -> ZMQOption -> IO ByteString
forall s a. (CStringLen -> IO s) -> Socket a -> ZMQOption -> IO s
getCStrOpt (Ptr CChar -> IO ByteString
SB.packCString (Ptr CChar -> IO ByteString)
-> (CStringLen -> Ptr CChar) -> CStringLen -> IO ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CStringLen -> Ptr CChar
forall a b. (a, b) -> a
fst)

getBytesOpt :: Socket a -> ZMQOption -> IO SB.ByteString
getBytesOpt :: Socket a -> ZMQOption -> IO ByteString
getBytesOpt = (CStringLen -> IO ByteString)
-> Socket a -> ZMQOption -> IO ByteString
forall s a. (CStringLen -> IO s) -> Socket a -> ZMQOption -> IO s
getCStrOpt CStringLen -> IO ByteString
SB.packCStringLen

getInt32Option :: ZMQOption -> Socket a -> IO Int
getInt32Option :: ZMQOption -> Socket a -> IO Int
getInt32Option o :: ZMQOption
o s :: Socket a
s = CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Socket a -> ZMQOption -> CInt -> IO CInt
forall b a.
(Storable b, Integral b) =>
Socket a -> ZMQOption -> b -> IO b
getIntOpt Socket a
s ZMQOption
o (0 :: CInt)

setInt32OptFromRestricted :: Integral i => ZMQOption -> Restricted r i -> Socket b -> IO ()
setInt32OptFromRestricted :: ZMQOption -> Restricted r i -> Socket b -> IO ()
setInt32OptFromRestricted o :: ZMQOption
o x :: Restricted r i
x s :: Socket b
s = Socket b -> ZMQOption -> CInt -> IO ()
forall b a.
(Storable b, Integral b) =>
Socket a -> ZMQOption -> b -> IO ()
setIntOpt Socket b
s ZMQOption
o ((i -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (i -> CInt) -> (Restricted r i -> i) -> Restricted r i -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Restricted r i -> i
forall r v. Restricted r v -> v
rvalue (Restricted r i -> CInt) -> Restricted r i -> CInt
forall a b. (a -> b) -> a -> b
$ Restricted r i
x) :: CInt)

ctxIntOption :: Integral i => String -> ZMQCtxOption -> Context -> IO i
ctxIntOption :: String -> ZMQCtxOption -> Context -> IO i
ctxIntOption name :: String
name opt :: ZMQCtxOption
opt ctx :: Context
ctx = CInt -> i
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> i) -> IO CInt -> IO i
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    (String -> IO CInt -> IO CInt
forall a. (Eq a, Num a) => String -> IO a -> IO a
throwIfMinus1 String
name (IO CInt -> IO CInt) -> IO CInt -> IO CInt
forall a b. (a -> b) -> a -> b
$ ZMQCtx -> CInt -> IO CInt
c_zmq_ctx_get (Context -> ZMQCtx
_ctx Context
ctx) (ZMQCtxOption -> CInt
ctxOptVal ZMQCtxOption
opt))

setCtxIntOption :: Integral i => String -> ZMQCtxOption -> i -> Context -> IO ()
setCtxIntOption :: String -> ZMQCtxOption -> i -> Context -> IO ()
setCtxIntOption name :: String
name opt :: ZMQCtxOption
opt val :: i
val ctx :: Context
ctx = String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1_ String
name (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$
    ZMQCtx -> CInt -> CInt -> IO CInt
c_zmq_ctx_set (Context -> ZMQCtx
_ctx Context
ctx) (ZMQCtxOption -> CInt
ctxOptVal ZMQCtxOption
opt) (i -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
val)

z85Encode :: (MonadIO m) => Restricted Div4 SB.ByteString -> m SB.ByteString
z85Encode :: Restricted Div4 ByteString -> m ByteString
z85Encode b :: Restricted Div4 ByteString
b = IO ByteString -> m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> m ByteString) -> IO ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> (CStringLen -> IO ByteString) -> IO ByteString
forall a. ByteString -> (CStringLen -> IO a) -> IO a
UB.unsafeUseAsCStringLen (Restricted Div4 ByteString -> ByteString
forall r v. Restricted r v -> v
rvalue Restricted Div4 ByteString
b) ((CStringLen -> IO ByteString) -> IO ByteString)
-> (CStringLen -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \(c :: Ptr CChar
c, s :: Int
s) ->
    Int -> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes ((Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
* 5) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) ((Ptr CChar -> IO ByteString) -> IO ByteString)
-> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \w :: Ptr CChar
w -> do
        IO (Ptr CChar) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Ptr CChar) -> IO ())
-> (IO (Ptr CChar) -> IO (Ptr CChar)) -> IO (Ptr CChar) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO (Ptr CChar) -> IO (Ptr CChar)
forall a. String -> IO (Ptr a) -> IO (Ptr a)
throwIfNull "z85Encode" (IO (Ptr CChar) -> IO ()) -> IO (Ptr CChar) -> IO ()
forall a b. (a -> b) -> a -> b
$
            Ptr CChar -> Ptr Word8 -> CSize -> IO (Ptr CChar)
c_zmq_z85_encode Ptr CChar
w (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
c) (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
s)
        Ptr CChar -> IO ByteString
SB.packCString Ptr CChar
w

z85Decode :: (MonadIO m) => Restricted Div5 SB.ByteString -> m SB.ByteString
z85Decode :: Restricted Div5 ByteString -> m ByteString
z85Decode b :: Restricted Div5 ByteString
b = IO ByteString -> m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> m ByteString) -> IO ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> (CStringLen -> IO ByteString) -> IO ByteString
forall a. ByteString -> (CStringLen -> IO a) -> IO a
SB.useAsCStringLen (Restricted Div5 ByteString -> ByteString
forall r v. Restricted r v -> v
rvalue Restricted Div5 ByteString
b) ((CStringLen -> IO ByteString) -> IO ByteString)
-> (CStringLen -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \(c :: Ptr CChar
c, s :: Int
s) -> do
    let size :: Int
size = (Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
* 4) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 5
    Int -> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
size ((Ptr CChar -> IO ByteString) -> IO ByteString)
-> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \w :: Ptr CChar
w -> do
        IO (Ptr Word8) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Ptr Word8) -> IO ())
-> (IO (Ptr Word8) -> IO (Ptr Word8)) -> IO (Ptr Word8) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO (Ptr Word8) -> IO (Ptr Word8)
forall a. String -> IO (Ptr a) -> IO (Ptr a)
throwIfNull "z85Decode" (IO (Ptr Word8) -> IO ()) -> IO (Ptr Word8) -> IO ()
forall a b. (a -> b) -> a -> b
$
            Ptr Word8 -> Ptr CChar -> IO (Ptr Word8)
c_zmq_z85_decode (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
w) (Ptr CChar -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
c)
        CStringLen -> IO ByteString
SB.packCStringLen (Ptr CChar
w, Int
size)

getKey :: KeyFormat f -> Socket a -> ZMQOption -> IO SB.ByteString
getKey :: KeyFormat f -> Socket a -> ZMQOption -> IO ByteString
getKey kf :: KeyFormat f
kf sock :: Socket a
sock (ZMQOption o :: CInt
o) = String -> Socket a -> (ZMQCtx -> IO ByteString) -> IO ByteString
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket "getKey" Socket a
sock ((ZMQCtx -> IO ByteString) -> IO ByteString)
-> (ZMQCtx -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \s :: ZMQCtx
s -> do
    let len :: Int
len = case KeyFormat f
kf of
            BinaryFormat -> 32
            TextFormat   -> 41
    Int -> (Ptr Int -> IO ByteString) -> IO ByteString
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Int
len ((Ptr Int -> IO ByteString) -> IO ByteString)
-> (Ptr Int -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \lenptr :: Ptr Int
lenptr -> Int -> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
len ((Ptr CChar -> IO ByteString) -> IO ByteString)
-> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \w :: Ptr CChar
w -> do
        String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ "getKey" (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$
            ZMQCtx -> CInt -> ZMQCtx -> Ptr CSize -> IO CInt
c_zmq_getsockopt ZMQCtx
s (CInt -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
o) (Ptr CChar -> ZMQCtx
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
w) (Ptr Int -> Ptr CSize
forall a b. Ptr a -> Ptr b
castPtr Ptr Int
lenptr)
        Ptr CChar -> IO ByteString
SB.packCString Ptr CChar
w

toZMQFlag :: Flag -> ZMQFlag
toZMQFlag :: Flag -> ZMQFlag
toZMQFlag DontWait = ZMQFlag
dontWait
toZMQFlag SendMore = ZMQFlag
sndMore

combineFlags :: [Flag] -> CInt
combineFlags :: [Flag] -> CInt
combineFlags = CInt -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> CInt) -> ([Flag] -> CInt) -> [Flag] -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CInt] -> CInt
forall i. (Integral i, Bits i) => [i] -> i
combine ([CInt] -> CInt) -> ([Flag] -> [CInt]) -> [Flag] -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Flag -> CInt) -> [Flag] -> [CInt]
forall a b. (a -> b) -> [a] -> [b]
map (ZMQFlag -> CInt
flagVal (ZMQFlag -> CInt) -> (Flag -> ZMQFlag) -> Flag -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Flag -> ZMQFlag
toZMQFlag)

combine :: (Integral i, Bits i) => [i] -> i
combine :: [i] -> i
combine = (i -> i -> i) -> i -> [i] -> i
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr i -> i -> i
forall a. Bits a => a -> a -> a
(.|.) 0

bool2cint :: Bool -> CInt
bool2cint :: Bool -> CInt
bool2cint True  = 1
bool2cint False = 0

toSwitch :: (Show a, Integral a) => String -> a -> Switch
toSwitch :: String -> a -> Switch
toSwitch _ (-1) = Switch
Default
toSwitch _   0  = Switch
Off
toSwitch _   1  = Switch
On
toSwitch m :: String
m   n :: a
n  = String -> Switch
forall a. HasCallStack => String -> a
error (String -> Switch) -> String -> Switch
forall a b. (a -> b) -> a -> b
$ String
m String -> ShowS
forall a. [a] -> [a] -> [a]
++ ": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
n

fromSwitch :: Integral a => Switch -> a
fromSwitch :: Switch -> a
fromSwitch Default = -1
fromSwitch Off     = 0
fromSwitch On      = 1

toZMQEventType :: EventType -> ZMQEventType
toZMQEventType :: EventType -> ZMQEventType
toZMQEventType AllEvents           = ZMQEventType
allEvents
toZMQEventType ConnectedEvent      = ZMQEventType
connected
toZMQEventType ConnectDelayedEvent = ZMQEventType
connectDelayed
toZMQEventType ConnectRetriedEvent = ZMQEventType
connectRetried
toZMQEventType ListeningEvent      = ZMQEventType
listening
toZMQEventType BindFailedEvent     = ZMQEventType
bindFailed
toZMQEventType AcceptedEvent       = ZMQEventType
accepted
toZMQEventType AcceptFailedEvent   = ZMQEventType
acceptFailed
toZMQEventType ClosedEvent         = ZMQEventType
closed
toZMQEventType CloseFailedEvent    = ZMQEventType
closeFailed
toZMQEventType DisconnectedEvent   = ZMQEventType
disconnected
toZMQEventType MonitorStoppedEvent = ZMQEventType
monitorStopped

toMechanism :: SecurityMechanism -> ZMQSecMechanism
toMechanism :: SecurityMechanism -> ZMQSecMechanism
toMechanism Null  = ZMQSecMechanism
secNull
toMechanism Plain = ZMQSecMechanism
secPlain
toMechanism Curve = ZMQSecMechanism
secCurve

fromMechanism :: String -> Int -> SecurityMechanism
fromMechanism :: String -> Int -> SecurityMechanism
fromMechanism s :: String
s m :: Int
m
    | Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQSecMechanism -> Int
secMechanism ZMQSecMechanism
secNull  = SecurityMechanism
Null
    | Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQSecMechanism -> Int
secMechanism ZMQSecMechanism
secPlain = SecurityMechanism
Plain
    | Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQSecMechanism -> Int
secMechanism ZMQSecMechanism
secCurve = SecurityMechanism
Curve
    | Bool
otherwise                  = String -> SecurityMechanism
forall a. HasCallStack => String -> a
error (String -> SecurityMechanism) -> String -> SecurityMechanism
forall a b. (a -> b) -> a -> b
$ String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ ": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
m

events2cint :: [EventType] -> CInt
events2cint :: [EventType] -> CInt
events2cint = Word16 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> CInt) -> ([EventType] -> Word16) -> [EventType] -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EventType -> Word16 -> Word16) -> Word16 -> [EventType] -> Word16
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
(.|.) (Word16 -> Word16 -> Word16)
-> (EventType -> Word16) -> EventType -> Word16 -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ZMQEventType -> Word16
eventTypeVal (ZMQEventType -> Word16)
-> (EventType -> ZMQEventType) -> EventType -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EventType -> ZMQEventType
toZMQEventType) 0

eventMessage :: SB.ByteString -> ZMQEvent -> EventMsg
eventMessage :: ByteString -> ZMQEvent -> EventMsg
eventMessage str :: ByteString
str (ZMQEvent e :: ZMQEventType
e v :: Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
connected      = ByteString -> Fd -> EventMsg
Connected      ByteString
str (CInt -> Fd
Fd (CInt -> Fd) -> (Int32 -> CInt) -> Int32 -> Fd
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Fd) -> Int32 -> Fd
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
connectDelayed = ByteString -> EventMsg
ConnectDelayed ByteString
str
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
connectRetried = ByteString -> Int -> EventMsg
ConnectRetried ByteString
str (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
listening      = ByteString -> Fd -> EventMsg
Listening      ByteString
str (CInt -> Fd
Fd (CInt -> Fd) -> (Int32 -> CInt) -> Int32 -> Fd
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Fd) -> Int32 -> Fd
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
bindFailed     = ByteString -> Int -> EventMsg
BindFailed     ByteString
str (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
accepted       = ByteString -> Fd -> EventMsg
Accepted       ByteString
str (CInt -> Fd
Fd (CInt -> Fd) -> (Int32 -> CInt) -> Int32 -> Fd
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Fd) -> Int32 -> Fd
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
acceptFailed   = ByteString -> Int -> EventMsg
AcceptFailed   ByteString
str (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
closed         = ByteString -> Fd -> EventMsg
Closed         ByteString
str (CInt -> Fd
Fd (CInt -> Fd) -> (Int32 -> CInt) -> Int32 -> Fd
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Fd) -> Int32 -> Fd
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
closeFailed    = ByteString -> Int -> EventMsg
CloseFailed    ByteString
str (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
disconnected   = ByteString -> Fd -> EventMsg
Disconnected   ByteString
str (Int32 -> Fd
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Fd) -> Int32 -> Fd
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | ZMQEventType
e ZMQEventType -> ZMQEventType -> Bool
forall a. Eq a => a -> a -> Bool
== ZMQEventType
monitorStopped = ByteString -> Int -> EventMsg
MonitorStopped ByteString
str (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Int32 -> Int
forall a b. (a -> b) -> a -> b
$ Int32
v)
    | Bool
otherwise           = String -> EventMsg
forall a. HasCallStack => String -> a
error (String -> EventMsg) -> String -> EventMsg
forall a b. (a -> b) -> a -> b
$ "unknown event type: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ZMQEventType -> String
forall a. Show a => a -> String
show ZMQEventType
e