{-# 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
$c== :: Flag -> Flag -> Bool
== :: Flag -> Flag -> Bool
$c/= :: Flag -> Flag -> Bool
/= :: 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
$ccompare :: Flag -> Flag -> Ordering
compare :: Flag -> Flag -> Ordering
$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
>= :: Flag -> Flag -> Bool
$cmax :: Flag -> Flag -> Flag
max :: Flag -> Flag -> Flag
$cmin :: Flag -> Flag -> Flag
min :: Flag -> Flag -> 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
$cshowsPrec :: Int -> Flag -> ShowS
showsPrec :: Int -> Flag -> ShowS
$cshow :: Flag -> String
show :: Flag -> String
$cshowList :: [Flag] -> ShowS
showList :: [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
$c== :: Switch -> Switch -> Bool
== :: Switch -> Switch -> Bool
$c/= :: Switch -> Switch -> Bool
/= :: 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
$ccompare :: Switch -> Switch -> Ordering
compare :: Switch -> Switch -> Ordering
$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
>= :: Switch -> Switch -> Bool
$cmax :: Switch -> Switch -> Switch
max :: Switch -> Switch -> Switch
$cmin :: Switch -> Switch -> Switch
min :: Switch -> Switch -> 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
$cshowsPrec :: Int -> Switch -> ShowS
showsPrec :: Int -> Switch -> ShowS
$cshow :: Switch -> String
show :: Switch -> String
$cshowList :: [Switch] -> ShowS
showList :: [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
$c== :: EventType -> EventType -> Bool
== :: EventType -> EventType -> Bool
$c/= :: EventType -> EventType -> Bool
/= :: 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
$ccompare :: EventType -> EventType -> Ordering
compare :: EventType -> EventType -> Ordering
$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
>= :: EventType -> EventType -> Bool
$cmax :: EventType -> EventType -> EventType
max :: EventType -> EventType -> EventType
$cmin :: EventType -> EventType -> EventType
min :: EventType -> EventType -> 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
$cshowsPrec :: Int -> EventType -> ShowS
showsPrec :: Int -> EventType -> ShowS
$cshow :: EventType -> String
show :: EventType -> String
$cshowList :: [EventType] -> ShowS
showList :: [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
$c== :: EventMsg -> EventMsg -> Bool
== :: EventMsg -> EventMsg -> Bool
$c/= :: EventMsg -> EventMsg -> Bool
/= :: 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
$cshowsPrec :: Int -> EventMsg -> ShowS
showsPrec :: Int -> EventMsg -> ShowS
$cshow :: EventMsg -> String
show :: EventMsg -> String
$cshowList :: [EventMsg] -> ShowS
showList :: [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
$c== :: SecurityMechanism -> SecurityMechanism -> Bool
== :: SecurityMechanism -> SecurityMechanism -> Bool
$c/= :: SecurityMechanism -> SecurityMechanism -> Bool
/= :: 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
$cshowsPrec :: Int -> SecurityMechanism -> ShowS
showsPrec :: Int -> SecurityMechanism -> ShowS
$cshow :: SecurityMechanism -> String
show :: SecurityMechanism -> String
$cshowList :: [SecurityMechanism] -> ShowS
showList :: [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
  { forall a. 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 :: forall t. 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 :: forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket String
_func (Socket (SocketRepr ZMQCtx
sock IORef Bool
_state)) ZMQCtx -> IO b
act = ZMQCtx -> IO b
act ZMQCtx
sock
{-# INLINE onSocket #-}

mkSocketRepr :: SocketType t => t -> Context -> IO SocketRepr
mkSocketRepr :: forall t. SocketType t => t -> Context -> IO SocketRepr
mkSocketRepr t
t 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 String
"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 a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    SocketRepr -> IO SocketRepr
forall a. a -> IO a
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 IORef a
r 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 a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

closeSock :: SocketRepr -> IO ()
closeSock :: SocketRepr -> IO ()
closeSock (SocketRepr ZMQCtx
s 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 (\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_ String
"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 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
$ \(Ptr CChar
cstr, 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 a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Message
msg

messageOfLazy :: LB.ByteString -> IO Message
messageOfLazy :: ByteString -> IO Message
messageOfLazy 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 Int
offset 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
$ \(Ptr CChar
cstr, 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 a. a -> IO a
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 Int
0 (ByteString -> [ByteString]
LB.toChunks ByteString
lbs)
    Message -> IO Message
forall a. a -> IO a
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 ZMQMsgPtr
ptr) = do
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1_ String
"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 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_ String
"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 a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ZMQMsgPtr -> Message
Message ZMQMsgPtr
ptr)

messageInitSize :: Size -> IO Message
messageInitSize :: Size -> IO Message
messageInitSize 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_ String
"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 a. a -> IO a
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 :: forall b a.
(Storable b, Integral b) =>
Socket a -> ZMQOption -> b -> IO ()
setIntOpt Socket a
sock (ZMQOption CInt
o) b
i = String -> Socket a -> (ZMQCtx -> IO ()) -> IO ()
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket String
"setIntOpt" Socket a
sock ((ZMQCtx -> IO ()) -> IO ()) -> (ZMQCtx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ZMQCtx
s ->
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ String
"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 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 ZMQCtx
s (ZMQOption CInt
o) (Ptr CChar
cstr, 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 :: forall a. Socket a -> ZMQOption -> ByteString -> IO ()
setByteStringOpt Socket a
sock ZMQOption
opt ByteString
str = String -> Socket a -> (ZMQCtx -> IO ()) -> IO ()
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket String
"setByteStringOpt" Socket a
sock ((ZMQCtx -> IO ()) -> IO ()) -> (ZMQCtx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ZMQCtx
s ->
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ String
"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 :: forall a. Socket a -> ZMQOption -> String -> IO ()
setStrOpt Socket a
sock ZMQOption
opt String
str = String -> Socket a -> (ZMQCtx -> IO ()) -> IO ()
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket String
"setStrOpt" Socket a
sock ((ZMQCtx -> IO ()) -> IO ()) -> (ZMQCtx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ZMQCtx
s ->
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ String
"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 :: forall b a.
(Storable b, Integral b) =>
Socket a -> ZMQOption -> b -> IO b
getIntOpt Socket a
sock (ZMQOption CInt
o) b
i = String -> Socket a -> (ZMQCtx -> IO b) -> IO b
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket String
"getIntOpt" Socket a
sock ((ZMQCtx -> IO b) -> IO b) -> (ZMQCtx -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \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
$ \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
$ \Ptr CSize
jptr -> do
        String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ String
"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 :: forall s a. (CStringLen -> IO s) -> Socket a -> ZMQOption -> IO s
getCStrOpt CStringLen -> IO s
peekA Socket a
sock (ZMQOption CInt
o) = String -> Socket a -> (ZMQCtx -> IO s) -> IO s
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket String
"getCStrOpt" Socket a
sock ((ZMQCtx -> IO s) -> IO s) -> (ZMQCtx -> IO s) -> IO s
forall a b. (a -> b) -> a -> b
$ \ZMQCtx
s ->
    CSize -> (Ptr CSize -> IO s) -> IO s
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with CSize
256        ((Ptr CSize -> IO s) -> IO s) -> (Ptr CSize -> IO s) -> IO s
forall a b. (a -> b) -> a -> b
$ \Ptr CSize
nptr ->
    Int -> (Ptr CChar -> IO s) -> IO s
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
256 ((Ptr CChar -> IO s) -> IO s) -> (Ptr CChar -> IO s) -> IO s
forall a b. (a -> b) -> a -> b
$ \Ptr CChar
bptr -> do
        String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ String
"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 a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \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 :: forall a. 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 :: forall a. 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 :: forall a. 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 :: forall a. ZMQOption -> Socket a -> IO Int
getInt32Option ZMQOption
o 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 (CInt
0 :: CInt)

setInt32OptFromRestricted :: Integral i => ZMQOption -> Restricted r i -> Socket b -> IO ()
setInt32OptFromRestricted :: forall i r b.
Integral i =>
ZMQOption -> Restricted r i -> Socket b -> IO ()
setInt32OptFromRestricted ZMQOption
o Restricted r i
x 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 :: forall i. Integral i => String -> ZMQCtxOption -> Context -> IO i
ctxIntOption String
name ZMQCtxOption
opt 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 :: forall i.
Integral i =>
String -> ZMQCtxOption -> i -> Context -> IO ()
setCtxIntOption String
name ZMQCtxOption
opt i
val 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 :: forall (m :: * -> *).
MonadIO m =>
Restricted Div4 ByteString -> m ByteString
z85Encode Restricted Div4 ByteString
b = IO ByteString -> m ByteString
forall a. IO a -> m a
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
$ \(Ptr CChar
c, 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
* Int
5) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ((Ptr CChar -> IO ByteString) -> IO ByteString)
-> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \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 String
"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 :: forall (m :: * -> *).
MonadIO m =>
Restricted Div5 ByteString -> m ByteString
z85Decode Restricted Div5 ByteString
b = IO ByteString -> m ByteString
forall a. IO a -> m a
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
$ \(Ptr CChar
c, Int
s) -> do
    let size :: Int
size = (Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
4) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
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
$ \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 String
"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 :: forall f a. KeyFormat f -> Socket a -> ZMQOption -> IO ByteString
getKey KeyFormat f
kf Socket a
sock (ZMQOption CInt
o) = String -> Socket a -> (ZMQCtx -> IO ByteString) -> IO ByteString
forall a b. String -> Socket a -> (ZMQCtx -> IO b) -> IO b
onSocket String
"getKey" Socket a
sock ((ZMQCtx -> IO ByteString) -> IO ByteString)
-> (ZMQCtx -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \ZMQCtx
s -> do
    let len :: Int
len = case KeyFormat f
kf of
            KeyFormat f
BinaryFormat -> Int
32
            KeyFormat f
TextFormat   -> Int
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
$ \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
$ \Ptr CChar
w -> do
        String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwIfMinus1Retry_ String
"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 Flag
DontWait = ZMQFlag
dontWait
toZMQFlag Flag
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 :: forall i. (Integral i, Bits i) => [i] -> i
combine = (i -> i -> i) -> i -> [i] -> i
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr i -> i -> i
forall a. Bits a => a -> a -> a
(.|.) i
0

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

toSwitch :: (Show a, Integral a) => String -> a -> Switch
toSwitch :: forall a. (Show a, Integral a) => String -> a -> Switch
toSwitch String
_ (-1) = Switch
Default
toSwitch String
_   a
0  = Switch
Off
toSwitch String
_   a
1  = Switch
On
toSwitch String
m   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
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
n

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

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

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

fromMechanism :: String -> Int -> SecurityMechanism
fromMechanism :: String -> Int -> SecurityMechanism
fromMechanism String
s 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
": " 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 a b. (a -> b -> b) -> b -> [a] -> b
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) Word16
0

eventMessage :: SB.ByteString -> ZMQEvent -> EventMsg
eventMessage :: ByteString -> ZMQEvent -> EventMsg
eventMessage ByteString
str (ZMQEvent ZMQEventType
e 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
$ String
"unknown event type: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ZMQEventType -> String
forall a. Show a => a -> String
show ZMQEventType
e