PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/System/ZMQ4/Monadic.hs

http://github.com/twittner/zeromq-haskell
Haskell | 624 lines | 425 code | 134 blank | 65 comment | 1 complexity | b7ab9bc202c77cbde1d533cbbd437501 MD5 | raw file
  1. {-# LANGUAGE RankNTypes #-}
  2. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  3. {-# LANGUAGE MultiParamTypeClasses #-}
  4. {-# LANGUAGE TypeFamilies #-}
  5. {-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
  6. -- |
  7. -- Module : System.ZMQ4.Monadic
  8. -- Copyright : (c) 2013 Toralf Wittner
  9. -- License : MIT
  10. -- Maintainer : Toralf Wittner <tw@dtex.org>
  11. -- Stability : experimental
  12. -- Portability : non-portable
  13. --
  14. -- This modules exposes a monadic interface of 'System.ZMQ4'. Actions run
  15. -- inside a 'ZMQ' monad and 'Socket's are guaranteed not to leak outside
  16. -- their corresponding 'runZMQ' scope. Running 'ZMQ' computations
  17. -- asynchronously is directly supported through 'async'.
  18. module System.ZMQ4.Monadic
  19. ( -- * Type Definitions
  20. ZMQ
  21. , Socket
  22. , Z.Flag (..)
  23. , Z.Switch (..)
  24. , Z.Timeout
  25. , Z.Event (..)
  26. , Z.EventType (..)
  27. , Z.EventMsg (..)
  28. , Z.Poll (..)
  29. , Z.KeyFormat (..)
  30. , Z.SecurityMechanism (..)
  31. -- ** Socket type-classes
  32. , Z.SocketType
  33. , Z.Sender
  34. , Z.Receiver
  35. , Z.Subscriber
  36. , Z.SocketLike
  37. , Z.Conflatable
  38. , Z.SendProbe
  39. -- ** Socket Types
  40. , Z.Pair (..)
  41. , Z.Pub (..)
  42. , Z.Sub (..)
  43. , Z.XPub (..)
  44. , Z.XSub (..)
  45. , Z.Req (..)
  46. , Z.Rep (..)
  47. , Z.Dealer (..)
  48. , Z.Router (..)
  49. , Z.Pull (..)
  50. , Z.Push (..)
  51. , Z.Stream (..)
  52. -- * General Operations
  53. , version
  54. , runZMQ
  55. , async
  56. , socket
  57. -- * ZMQ Options (Read)
  58. , ioThreads
  59. , maxSockets
  60. -- * ZMQ Options (Write)
  61. , setIoThreads
  62. , setMaxSockets
  63. -- * Socket operations
  64. , close
  65. , bind
  66. , unbind
  67. , connect
  68. , disconnect
  69. , send
  70. , send'
  71. , sendMulti
  72. , receive
  73. , receiveMulti
  74. , subscribe
  75. , unsubscribe
  76. , proxy
  77. , monitor
  78. , socketMonitor
  79. , Z.poll
  80. -- * Socket Options (Read)
  81. , affinity
  82. , backlog
  83. , conflate
  84. , curvePublicKey
  85. , curveSecretKey
  86. , curveServerKey
  87. , delayAttachOnConnect
  88. , events
  89. , fileDescriptor
  90. , identity
  91. , immediate
  92. , ipv4Only
  93. , ipv6
  94. , lastEndpoint
  95. , linger
  96. , maxMessageSize
  97. , mcastHops
  98. , mechanism
  99. , moreToReceive
  100. , plainServer
  101. , plainPassword
  102. , plainUserName
  103. , rate
  104. , receiveBuffer
  105. , receiveHighWM
  106. , receiveTimeout
  107. , reconnectInterval
  108. , reconnectIntervalMax
  109. , recoveryInterval
  110. , sendBuffer
  111. , sendHighWM
  112. , sendTimeout
  113. , tcpKeepAlive
  114. , tcpKeepAliveCount
  115. , tcpKeepAliveIdle
  116. , tcpKeepAliveInterval
  117. , zapDomain
  118. -- * Socket Options (Write)
  119. , setAffinity
  120. , setBacklog
  121. , setConflate
  122. , setCurveServer
  123. , setCurvePublicKey
  124. , setCurveSecretKey
  125. , setCurveServerKey
  126. , setDelayAttachOnConnect
  127. , setIdentity
  128. , setImmediate
  129. , setIpv4Only
  130. , setIpv6
  131. , setLinger
  132. , setMaxMessageSize
  133. , setMcastHops
  134. , setPlainServer
  135. , setPlainPassword
  136. , setPlainUserName
  137. , setProbeRouter
  138. , setRate
  139. , setReceiveBuffer
  140. , setReceiveHighWM
  141. , setReceiveTimeout
  142. , setReconnectInterval
  143. , setReconnectIntervalMax
  144. , setRecoveryInterval
  145. , setReqCorrelate
  146. , setReqRelaxed
  147. , setRouterMandatory
  148. , setSendBuffer
  149. , setSendHighWM
  150. , setSendTimeout
  151. , setTcpAcceptFilter
  152. , setTcpKeepAlive
  153. , setTcpKeepAliveCount
  154. , setTcpKeepAliveIdle
  155. , setTcpKeepAliveInterval
  156. , setXPubVerbose
  157. -- * Error Handling
  158. , Z.ZMQError
  159. , Z.errno
  160. , Z.source
  161. , Z.message
  162. -- * Re-exports
  163. , Control.Monad.IO.Class.liftIO
  164. , Data.Restricted.restrict
  165. , Data.Restricted.toRestricted
  166. -- * Low-level Functions
  167. , waitRead
  168. , waitWrite
  169. , I.z85Encode
  170. , I.z85Decode
  171. , Z.curveKeyPair
  172. ) where
  173. import Control.Applicative
  174. import Control.Concurrent.Async (Async)
  175. import Control.Monad
  176. import Control.Monad.Base (MonadBase(..))
  177. import Control.Monad.Catch
  178. import Control.Monad.IO.Class
  179. import Control.Monad.Trans.Control (MonadBaseControl(..))
  180. import Control.Monad.Trans.Reader
  181. import Data.Int
  182. import Data.IORef
  183. import Data.List.NonEmpty (NonEmpty)
  184. import Data.Restricted
  185. import Data.Word
  186. import Data.ByteString (ByteString)
  187. import System.Posix.Types (Fd)
  188. import Prelude
  189. import qualified Control.Concurrent.Async as A
  190. import qualified Control.Exception as E
  191. import qualified Control.Monad.Catch as C
  192. import qualified Data.ByteString.Lazy as Lazy
  193. import qualified System.ZMQ4 as Z
  194. import qualified System.ZMQ4.Internal as I
  195. data ZMQEnv = ZMQEnv
  196. { _refcount :: !(IORef Word)
  197. , _context :: !Z.Context
  198. , _sockets :: !(IORef [I.SocketRepr])
  199. }
  200. -- | The ZMQ monad is modeled after 'Control.Monad.ST' and encapsulates
  201. -- a 'System.ZMQ4.Context'. It uses the uninstantiated type variable 'z' to
  202. -- distinguish different invoctions of 'runZMQ' and to prevent
  203. -- unintented use of 'Socket's outside their scope. Cf. the paper
  204. -- of John Launchbury and Simon Peyton Jones /Lazy Functional State Threads/.
  205. newtype ZMQ z a = ZMQ { _unzmq :: ReaderT ZMQEnv IO a }
  206. deriving (MonadBase IO)
  207. -- | The ZMQ socket, parameterised by 'SocketType' and belonging to
  208. -- a particular 'ZMQ' thread.
  209. newtype Socket z t = Socket { _unsocket :: Z.Socket t }
  210. instance I.SocketLike (Socket z) where
  211. toSocket = _unsocket
  212. instance Monad (ZMQ z) where
  213. return = ZMQ . return
  214. (ZMQ m) >>= f = ZMQ $ m >>= _unzmq . f
  215. instance MonadIO (ZMQ z) where
  216. liftIO m = ZMQ $! liftIO m
  217. instance MonadBaseControl IO (ZMQ z) where
  218. type StM (ZMQ z) a = a
  219. liftBaseWith = \f -> ZMQ $ liftBaseWith $ \q -> f (q . _unzmq)
  220. restoreM = ZMQ . restoreM
  221. instance MonadThrow (ZMQ z) where
  222. throwM = ZMQ . C.throwM
  223. instance MonadCatch (ZMQ z) where
  224. catch (ZMQ m) f = ZMQ $ m `C.catch` (_unzmq . f)
  225. instance MonadMask (ZMQ z) where
  226. mask a = ZMQ . ReaderT $ \env ->
  227. C.mask $ \restore ->
  228. let f :: forall r a . ZMQ r a -> ZMQ r a
  229. f (ZMQ (ReaderT b)) = ZMQ $ ReaderT (restore . b)
  230. in runReaderT (_unzmq (a $ f)) env
  231. uninterruptibleMask a = ZMQ . ReaderT $ \env ->
  232. C.uninterruptibleMask $ \restore ->
  233. let f :: forall r a . ZMQ r a -> ZMQ r a
  234. f (ZMQ (ReaderT b)) = ZMQ $ ReaderT (restore . b)
  235. in runReaderT (_unzmq (a $ f)) env
  236. instance Functor (ZMQ z) where
  237. fmap = liftM
  238. instance Applicative (ZMQ z) where
  239. pure = return
  240. (<*>) = ap
  241. -- | Return the value computed by the given 'ZMQ' monad. Rank-2
  242. -- polymorphism is used to prevent leaking of 'z'.
  243. -- An invocation of 'runZMQ' will internally create a 'System.ZMQ4.Context'
  244. -- and all actions are executed relative to this context. On finish the
  245. -- context will be disposed, but see 'async'.
  246. runZMQ :: MonadIO m => (forall z. ZMQ z a) -> m a
  247. runZMQ z = liftIO $ E.bracket make term (runReaderT (_unzmq z))
  248. where
  249. make = ZMQEnv <$> newIORef 1 <*> Z.context <*> newIORef []
  250. -- | Run the given 'ZMQ' computation asynchronously, i.e. this function
  251. -- runs the computation in a new thread using 'Control.Concurrent.Async.async'.
  252. -- /N.B./ reference counting is used to prolong the lifetime of the
  253. -- 'System.ZMQ.Context' encapsulated in 'ZMQ' as necessary, e.g.:
  254. --
  255. -- @
  256. -- runZMQ $ do
  257. -- s <- socket Pair
  258. -- async $ do
  259. -- liftIO (threadDelay 10000000)
  260. -- identity s >>= liftIO . print
  261. -- @
  262. --
  263. -- Here, 'runZMQ' will finish before the code section in 'async', but due to
  264. -- reference counting, the 'System.ZMQ4.Context' will only be disposed after
  265. -- 'async' finishes as well.
  266. async :: ZMQ z a -> ZMQ z (Async a)
  267. async z = ZMQ $ do
  268. e <- ask
  269. liftIO $ atomicModifyIORef (_refcount e) $ \n -> (succ n, ())
  270. liftIO . A.async $ (runReaderT (_unzmq z) e) `E.finally` term e
  271. ioThreads :: ZMQ z Word
  272. ioThreads = onContext Z.ioThreads
  273. setIoThreads :: Word -> ZMQ z ()
  274. setIoThreads = onContext . Z.setIoThreads
  275. maxSockets :: ZMQ z Word
  276. maxSockets = onContext Z.maxSockets
  277. setMaxSockets :: Word -> ZMQ z ()
  278. setMaxSockets = onContext . Z.setMaxSockets
  279. socket :: Z.SocketType t => t -> ZMQ z (Socket z t)
  280. socket t = ZMQ $ do
  281. c <- asks _context
  282. s <- asks _sockets
  283. x <- liftIO $ I.mkSocketRepr t c
  284. liftIO $ atomicModifyIORef s $ \ss -> (x:ss, ())
  285. return (Socket (I.Socket x))
  286. version :: ZMQ z (Int, Int, Int)
  287. version = liftIO $! Z.version
  288. -- * Socket operations
  289. close :: Socket z t -> ZMQ z ()
  290. close = liftIO . Z.close . _unsocket
  291. bind :: Socket z t -> String -> ZMQ z ()
  292. bind s = liftIO . Z.bind (_unsocket s)
  293. unbind :: Socket z t -> String -> ZMQ z ()
  294. unbind s = liftIO . Z.unbind (_unsocket s)
  295. connect :: Socket z t -> String -> ZMQ z ()
  296. connect s = liftIO . Z.connect (_unsocket s)
  297. disconnect :: Socket z t -> String -> ZMQ z ()
  298. disconnect s = liftIO . Z.disconnect (_unsocket s)
  299. send :: Z.Sender t => Socket z t -> [Z.Flag] -> ByteString -> ZMQ z ()
  300. send s f = liftIO . Z.send (_unsocket s) f
  301. send' :: Z.Sender t => Socket z t -> [Z.Flag] -> Lazy.ByteString -> ZMQ z ()
  302. send' s f = liftIO . Z.send' (_unsocket s) f
  303. sendMulti :: Z.Sender t => Socket z t -> NonEmpty ByteString -> ZMQ z ()
  304. sendMulti s = liftIO . Z.sendMulti (_unsocket s)
  305. receive :: Z.Receiver t => Socket z t -> ZMQ z ByteString
  306. receive = liftIO . Z.receive . _unsocket
  307. receiveMulti :: Z.Receiver t => Socket z t -> ZMQ z [ByteString]
  308. receiveMulti = liftIO . Z.receiveMulti . _unsocket
  309. subscribe :: Z.Subscriber t => Socket z t -> ByteString -> ZMQ z ()
  310. subscribe s = liftIO . Z.subscribe (_unsocket s)
  311. unsubscribe :: Z.Subscriber t => Socket z t -> ByteString -> ZMQ z ()
  312. unsubscribe s = liftIO . Z.unsubscribe (_unsocket s)
  313. proxy :: Socket z a -> Socket z b -> Maybe (Socket z c) -> ZMQ z ()
  314. proxy a b c = liftIO $ Z.proxy (_unsocket a) (_unsocket b) (_unsocket <$> c)
  315. monitor :: [Z.EventType] -> Socket z t -> ZMQ z (Bool -> IO (Maybe Z.EventMsg))
  316. monitor es s = onContext $ \ctx -> Z.monitor es ctx (_unsocket s)
  317. socketMonitor :: [Z.EventType] -> String -> Socket z t -> ZMQ z ()
  318. socketMonitor es addr s = liftIO $ Z.socketMonitor es addr (_unsocket s)
  319. -- * Socket Options (Read)
  320. affinity :: Socket z t -> ZMQ z Word64
  321. affinity = liftIO . Z.affinity . _unsocket
  322. backlog :: Socket z t -> ZMQ z Int
  323. backlog = liftIO . Z.backlog . _unsocket
  324. conflate :: Z.Conflatable t => Socket z t -> ZMQ z Bool
  325. conflate = liftIO . Z.conflate . _unsocket
  326. curvePublicKey :: Z.KeyFormat f -> Socket z t -> ZMQ z ByteString
  327. curvePublicKey f = liftIO . Z.curvePublicKey f . _unsocket
  328. curveSecretKey :: Z.KeyFormat f -> Socket z t -> ZMQ z ByteString
  329. curveSecretKey f = liftIO . Z.curveSecretKey f . _unsocket
  330. curveServerKey :: Z.KeyFormat f -> Socket z t -> ZMQ z ByteString
  331. curveServerKey f = liftIO . Z.curveServerKey f . _unsocket
  332. delayAttachOnConnect :: Socket z t -> ZMQ z Bool
  333. delayAttachOnConnect = liftIO . Z.delayAttachOnConnect . _unsocket
  334. {-# DEPRECATED delayAttachOnConnect "Use immediate" #-}
  335. events :: Socket z t -> ZMQ z [Z.Event]
  336. events = liftIO . Z.events . _unsocket
  337. fileDescriptor :: Socket z t -> ZMQ z Fd
  338. fileDescriptor = liftIO . Z.fileDescriptor . _unsocket
  339. identity :: Socket z t -> ZMQ z ByteString
  340. identity = liftIO . Z.identity . _unsocket
  341. immediate :: Socket z t -> ZMQ z Bool
  342. immediate = liftIO . Z.immediate . _unsocket
  343. ipv4Only :: Socket z t -> ZMQ z Bool
  344. ipv4Only = liftIO . Z.ipv4Only . _unsocket
  345. {-# DEPRECATED ipv4Only "Use ipv6" #-}
  346. ipv6 :: Socket z t -> ZMQ z Bool
  347. ipv6 = liftIO . Z.ipv6 . _unsocket
  348. lastEndpoint :: Socket z t -> ZMQ z String
  349. lastEndpoint = liftIO . Z.lastEndpoint . _unsocket
  350. linger :: Socket z t -> ZMQ z Int
  351. linger = liftIO . Z.linger . _unsocket
  352. maxMessageSize :: Socket z t -> ZMQ z Int64
  353. maxMessageSize = liftIO . Z.maxMessageSize . _unsocket
  354. mcastHops :: Socket z t -> ZMQ z Int
  355. mcastHops = liftIO . Z.mcastHops . _unsocket
  356. mechanism :: Socket z t -> ZMQ z Z.SecurityMechanism
  357. mechanism = liftIO . Z.mechanism . _unsocket
  358. moreToReceive :: Socket z t -> ZMQ z Bool
  359. moreToReceive = liftIO . Z.moreToReceive . _unsocket
  360. plainServer :: Socket z t -> ZMQ z Bool
  361. plainServer = liftIO . Z.plainServer . _unsocket
  362. plainPassword :: Socket z t -> ZMQ z ByteString
  363. plainPassword = liftIO . Z.plainPassword . _unsocket
  364. plainUserName :: Socket z t -> ZMQ z ByteString
  365. plainUserName = liftIO . Z.plainUserName . _unsocket
  366. rate :: Socket z t -> ZMQ z Int
  367. rate = liftIO . Z.rate . _unsocket
  368. receiveBuffer :: Socket z t -> ZMQ z Int
  369. receiveBuffer = liftIO . Z.receiveBuffer . _unsocket
  370. receiveHighWM :: Socket z t -> ZMQ z Int
  371. receiveHighWM = liftIO . Z.receiveHighWM . _unsocket
  372. receiveTimeout :: Socket z t -> ZMQ z Int
  373. receiveTimeout = liftIO . Z.receiveTimeout . _unsocket
  374. reconnectInterval :: Socket z t -> ZMQ z Int
  375. reconnectInterval = liftIO . Z.reconnectInterval . _unsocket
  376. reconnectIntervalMax :: Socket z t -> ZMQ z Int
  377. reconnectIntervalMax = liftIO . Z.reconnectIntervalMax . _unsocket
  378. recoveryInterval :: Socket z t -> ZMQ z Int
  379. recoveryInterval = liftIO . Z.recoveryInterval . _unsocket
  380. sendBuffer :: Socket z t -> ZMQ z Int
  381. sendBuffer = liftIO . Z.sendBuffer . _unsocket
  382. sendHighWM :: Socket z t -> ZMQ z Int
  383. sendHighWM = liftIO . Z.sendHighWM . _unsocket
  384. sendTimeout :: Socket z t -> ZMQ z Int
  385. sendTimeout = liftIO . Z.sendTimeout . _unsocket
  386. tcpKeepAlive :: Socket z t -> ZMQ z Z.Switch
  387. tcpKeepAlive = liftIO . Z.tcpKeepAlive . _unsocket
  388. tcpKeepAliveCount :: Socket z t -> ZMQ z Int
  389. tcpKeepAliveCount = liftIO . Z.tcpKeepAliveCount . _unsocket
  390. tcpKeepAliveIdle :: Socket z t -> ZMQ z Int
  391. tcpKeepAliveIdle = liftIO . Z.tcpKeepAliveIdle . _unsocket
  392. tcpKeepAliveInterval :: Socket z t -> ZMQ z Int
  393. tcpKeepAliveInterval = liftIO . Z.tcpKeepAliveInterval . _unsocket
  394. zapDomain :: Socket z t -> ZMQ z ByteString
  395. zapDomain = liftIO . Z.zapDomain . _unsocket
  396. -- * Socket Options (Write)
  397. setAffinity :: Word64 -> Socket z t -> ZMQ z ()
  398. setAffinity a = liftIO . Z.setAffinity a . _unsocket
  399. setBacklog :: Integral i => Restricted (N0, Int32) i -> Socket z t -> ZMQ z ()
  400. setBacklog b = liftIO . Z.setBacklog b . _unsocket
  401. setConflate :: Z.Conflatable t => Bool -> Socket z t -> ZMQ z ()
  402. setConflate i = liftIO . Z.setConflate i . _unsocket
  403. setCurvePublicKey :: Z.KeyFormat f -> Restricted f ByteString -> Socket z t -> ZMQ z ()
  404. setCurvePublicKey f k = liftIO . Z.setCurvePublicKey f k . _unsocket
  405. setCurveSecretKey :: Z.KeyFormat f -> Restricted f ByteString -> Socket z t -> ZMQ z ()
  406. setCurveSecretKey f k = liftIO . Z.setCurveSecretKey f k . _unsocket
  407. setCurveServer :: Bool -> Socket z t -> ZMQ z ()
  408. setCurveServer b = liftIO . Z.setCurveServer b . _unsocket
  409. setCurveServerKey :: Z.KeyFormat f -> Restricted f ByteString -> Socket z t -> ZMQ z ()
  410. setCurveServerKey f k = liftIO . Z.setCurveServerKey f k . _unsocket
  411. setDelayAttachOnConnect :: Bool -> Socket z t -> ZMQ z ()
  412. setDelayAttachOnConnect d = liftIO . Z.setDelayAttachOnConnect d . _unsocket
  413. {-# DEPRECATED setDelayAttachOnConnect "Use setImmediate" #-}
  414. setIdentity :: Restricted (N1, N254) ByteString -> Socket z t -> ZMQ z ()
  415. setIdentity i = liftIO . Z.setIdentity i . _unsocket
  416. setImmediate :: Bool -> Socket z t -> ZMQ z ()
  417. setImmediate i = liftIO . Z.setImmediate i . _unsocket
  418. setIpv4Only :: Bool -> Socket z t -> ZMQ z ()
  419. setIpv4Only i = liftIO . Z.setIpv4Only i . _unsocket
  420. {-# DEPRECATED setIpv4Only "Use setIpv6" #-}
  421. setIpv6 :: Bool -> Socket z t -> ZMQ z ()
  422. setIpv6 i = liftIO . Z.setIpv6 i . _unsocket
  423. setLinger :: Integral i => Restricted (Nneg1, Int32) i -> Socket z t -> ZMQ z ()
  424. setLinger l = liftIO . Z.setLinger l . _unsocket
  425. setMaxMessageSize :: Integral i => Restricted (Nneg1, Int64) i -> Socket z t -> ZMQ z ()
  426. setMaxMessageSize s = liftIO . Z.setMaxMessageSize s . _unsocket
  427. setMcastHops :: Integral i => Restricted (N1, Int32) i -> Socket z t -> ZMQ z ()
  428. setMcastHops k = liftIO . Z.setMcastHops k . _unsocket
  429. setPlainServer :: Bool -> Socket z t -> ZMQ z ()
  430. setPlainServer b = liftIO . Z.setPlainServer b . _unsocket
  431. setPlainPassword :: Restricted (N1, N254) ByteString -> Socket z t -> ZMQ z ()
  432. setPlainPassword s = liftIO . Z.setPlainPassword s . _unsocket
  433. setPlainUserName :: Restricted (N1, N254) ByteString -> Socket z t -> ZMQ z ()
  434. setPlainUserName s = liftIO . Z.setPlainUserName s . _unsocket
  435. setProbeRouter :: Z.SendProbe t => Bool -> Socket z t -> ZMQ z ()
  436. setProbeRouter b = liftIO . Z.setProbeRouter b . _unsocket
  437. setRate :: Integral i => Restricted (N1, Int32) i -> Socket z t -> ZMQ z ()
  438. setRate r = liftIO . Z.setRate r . _unsocket
  439. setReceiveBuffer :: Integral i => Restricted (N0, Int32) i -> Socket z t -> ZMQ z ()
  440. setReceiveBuffer k = liftIO . Z.setReceiveBuffer k . _unsocket
  441. setReceiveHighWM :: Integral i => Restricted (N0, Int32) i -> Socket z t -> ZMQ z ()
  442. setReceiveHighWM k = liftIO . Z.setReceiveHighWM k . _unsocket
  443. setReceiveTimeout :: Integral i => Restricted (Nneg1, Int32) i -> Socket z t -> ZMQ z ()
  444. setReceiveTimeout t = liftIO . Z.setReceiveTimeout t . _unsocket
  445. setReconnectInterval :: Integral i => Restricted (N0, Int32) i -> Socket z t -> ZMQ z ()
  446. setReconnectInterval i = liftIO . Z.setReconnectInterval i . _unsocket
  447. setReconnectIntervalMax :: Integral i => Restricted (N0, Int32) i -> Socket z t -> ZMQ z ()
  448. setReconnectIntervalMax i = liftIO . Z.setReconnectIntervalMax i . _unsocket
  449. setRecoveryInterval :: Integral i => Restricted (N0, Int32) i -> Socket z t -> ZMQ z ()
  450. setRecoveryInterval i = liftIO . Z.setRecoveryInterval i . _unsocket
  451. setReqCorrelate :: Bool -> Socket z Z.Req -> ZMQ z ()
  452. setReqCorrelate b = liftIO . Z.setReqCorrelate b . _unsocket
  453. setReqRelaxed :: Bool -> Socket z Z.Req -> ZMQ z ()
  454. setReqRelaxed b = liftIO . Z.setReqRelaxed b . _unsocket
  455. setRouterMandatory :: Bool -> Socket z Z.Router -> ZMQ z ()
  456. setRouterMandatory b = liftIO . Z.setRouterMandatory b . _unsocket
  457. setSendBuffer :: Integral i => Restricted (N0, Int32) i -> Socket z t -> ZMQ z ()
  458. setSendBuffer i = liftIO . Z.setSendBuffer i . _unsocket
  459. setSendHighWM :: Integral i => Restricted (N0, Int32) i -> Socket z t -> ZMQ z ()
  460. setSendHighWM i = liftIO . Z.setSendHighWM i . _unsocket
  461. setSendTimeout :: Integral i => Restricted (Nneg1, Int32) i -> Socket z t -> ZMQ z ()
  462. setSendTimeout i = liftIO . Z.setSendTimeout i . _unsocket
  463. setTcpAcceptFilter :: Maybe ByteString -> Socket z t -> ZMQ z ()
  464. setTcpAcceptFilter s = liftIO . Z.setTcpAcceptFilter s . _unsocket
  465. setTcpKeepAlive :: Z.Switch -> Socket z t -> ZMQ z ()
  466. setTcpKeepAlive s = liftIO . Z.setTcpKeepAlive s . _unsocket
  467. setTcpKeepAliveCount :: Integral i => Restricted (Nneg1, Int32) i -> Socket z t -> ZMQ z ()
  468. setTcpKeepAliveCount c = liftIO . Z.setTcpKeepAliveCount c . _unsocket
  469. setTcpKeepAliveIdle :: Integral i => Restricted (Nneg1, Int32) i -> Socket z t -> ZMQ z ()
  470. setTcpKeepAliveIdle i = liftIO . Z.setTcpKeepAliveIdle i . _unsocket
  471. setTcpKeepAliveInterval :: Integral i => Restricted (Nneg1, Int32) i -> Socket z t -> ZMQ z ()
  472. setTcpKeepAliveInterval i = liftIO . Z.setTcpKeepAliveInterval i . _unsocket
  473. setXPubVerbose :: Bool -> Socket z Z.XPub -> ZMQ z ()
  474. setXPubVerbose b = liftIO . Z.setXPubVerbose b . _unsocket
  475. -- * Low Level Functions
  476. waitRead :: Socket z t -> ZMQ z ()
  477. waitRead = liftIO . Z.waitRead . _unsocket
  478. waitWrite :: Socket z t -> ZMQ z ()
  479. waitWrite = liftIO . Z.waitWrite . _unsocket
  480. -- * Internal
  481. onContext :: (Z.Context -> IO a) -> ZMQ z a
  482. onContext f = ZMQ $! asks _context >>= liftIO . f
  483. term :: ZMQEnv -> IO ()
  484. term env = do
  485. n <- atomicModifyIORef (_refcount env) $ \n -> (pred n, n)
  486. when (n == 1) $ do
  487. readIORef (_sockets env) >>= mapM_ close'
  488. Z.term (_context env)
  489. where
  490. close' s = I.closeSock s `E.catch` (\e -> print (e :: E.SomeException))