11import ./ bindings
2- import std/ [strformat]
2+ import std/ [strformat, logging ]
33
44# Unofficial easier-for-Nim API
55
1313
1414 ZConnectionImpl * {.pure , final .} = object
1515 # # A Zmq connection. Since ``ZContext`` and ``ZSocket`` are pointers, it is highly recommended to **not** copy ``ZConnection``.
16- context* : ZContext # # Zmq context from C-bindings.
16+ context* : ZContext # # Zmq context from C-bindings.
1717 socket* : ZSocket # # Zmq socket from C-bindings.
1818 ownctx: bool # Boolean indicating if the connection owns the Zmq context
1919 alive: bool # Boolean indicating if the connection has been closed
@@ -32,16 +32,39 @@ proc zmqError*() {.noinline, noreturn.} =
3232 e.msg = & " Error: { e.error} . " & $ strerror (e.error)
3333 raise e
3434
35+ var shouldLogEagainError = false
36+
37+ proc enableLogEagain * () =
38+ # # Enable logging EAGAIN error in ZMQ calls
39+ shouldLogEagainError = true
40+
41+ proc disableLogEagain * () =
42+ # # Disable logging EAGAIN error in ZMQ calls
43+ shouldLogEagainError = false
44+
3545proc zmqErrorExceptEAGAIN () =
3646 var e: ref ZmqError
3747 new (e)
3848 e.error = errno ()
49+ let errmsg = $ strerror (e.error)
3950 if e.error == ZMQ_EAGAIN :
40- discard
51+ if shouldLogEagainError:
52+ if logging.getHandlers ().len () > 0 :
53+ warn (errmsg)
54+ else :
55+ echo (errmsg)
56+ else :
57+ discard
4158 else :
42- e.msg = & " Error: { e.error} . " & $ strerror (e.error)
59+ e.msg = & " Error: { e.error} . " & errmsg
4360 raise e
4461
62+ template defaultFlag () : ZSendRecvOptions =
63+ when defined (defaultFlagDontWait):
64+ DONTWAIT
65+ else :
66+ NOFLAGS
67+
4568#[
4669# Context related proc
4770]#
@@ -135,7 +158,6 @@ proc getsockopt*[T: SomeOrdinal|string](c: ZConnection, option: ZSockOptions): T
135158 # # Check http://api.zeromq.org/4-2:zmq-setsockopt
136159 getsockopt [T](c.socket, option)
137160
138-
139161#[
140162 Destructor
141163]#
@@ -305,7 +327,7 @@ proc close*(c: ZConnection, linger: int = 500) =
305327
306328# Send / Receive
307329# Send with ZSocket type
308- proc send * (s: ZSocket , msg: string , flags: ZSendRecvOptions = NOFLAGS ) =
330+ proc send * (s: ZSocket , msg: string , flags: ZSendRecvOptions = defaultFlag () ) =
309331 # # Sends a message through the socket.
310332 var m: ZMsg
311333 if msg_init (m, msg.len) != 0 :
@@ -330,7 +352,7 @@ proc sendAll*(s: ZSocket, msg: varargs[string]) =
330352 inc (i)
331353 s.send (msg[i])
332354
333- proc send * (c: ZConnection , msg: string , flags: ZSendRecvOptions = NOFLAGS ) =
355+ proc send * (c: ZConnection , msg: string , flags: ZSendRecvOptions = defaultFlag () ) =
334356 # # Sends a message over the connection.
335357 send (c.socket, msg, flags)
336358
@@ -339,7 +361,7 @@ proc sendAll*(c: ZConnection, msg: varargs[string]) =
339361 sendAll (c.socket, msg)
340362
341363# receive with ZSocket type
342- proc receiveImpl (s: ZSocket , flags: ZSendRecvOptions = NOFLAGS ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
364+ proc receiveImpl (s: ZSocket , flags: ZSendRecvOptions = defaultFlag () ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
343365 result .moreAvailable = false
344366 result .msgAvailable = false
345367
@@ -364,7 +386,7 @@ proc receiveImpl(s: ZSocket, flags: ZSendRecvOptions = NOFLAGS): tuple[msgAvaila
364386 if msg_close (m) != 0 :
365387 zmqError ()
366388
367- proc waitForReceive * (s: ZSocket , timeout: int = - 2 , flags: ZSendRecvOptions = NOFLAGS ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
389+ proc waitForReceive * (s: ZSocket , timeout: int = - 2 , flags: ZSendRecvOptions = defaultFlag () ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
368390 # # Set RCVTIMEO for the socket and wait until a message is available.
369391 # # This function is blocking.
370392 # #
@@ -392,7 +414,7 @@ proc waitForReceive*(s: ZSocket, timeout: int = -2, flags: ZSendRecvOptions = NO
392414 if shouldUpdateTimeout:
393415 s.setsockopt (RCVTIMEO , curtimeout.cint )
394416
395- proc tryReceive * (s: ZSocket , flags: ZSendRecvOptions = NOFLAGS ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
417+ proc tryReceive * (s: ZSocket , flags: ZSendRecvOptions = defaultFlag () ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
396418 # # Receives a message from a socket in a non-blocking way.
397419 # #
398420 # # Indicate whether a message was received or EAGAIN occured by ``msgAvailable``
@@ -406,13 +428,13 @@ proc tryReceive*(s: ZSocket, flags: ZSendRecvOptions = NOFLAGS): tuple[msgAvaila
406428 if (status and ZMQ_POLLIN ) != 0 :
407429 result = receiveImpl (s, flags)
408430
409- proc receive * (s: ZSocket , flags: ZSendRecvOptions = NOFLAGS ): string =
431+ proc receive * (s: ZSocket , flags: ZSendRecvOptions = defaultFlag () ): string =
410432 # # Receive a message on socket.
411433 #
412434 # # Return an empty string on EAGAIN
413435 receiveImpl (s, flags).msg
414436
415- proc receiveAll * (s: ZSocket , flags: ZSendRecvOptions = NOFLAGS ): seq [string ] =
437+ proc receiveAll * (s: ZSocket , flags: ZSendRecvOptions = defaultFlag () ): seq [string ] =
416438 # # Receive all parts of a message
417439 # #
418440 # # If EAGAIN occurs without any data being received, it will be an empty seq
@@ -425,7 +447,7 @@ proc receiveAll*(s: ZSocket, flags: ZSendRecvOptions = NOFLAGS): seq[string] =
425447 else :
426448 expectMessage = false
427449
428- proc waitForReceive * (c: ZConnection , timeout: int = - 1 , flags: ZSendRecvOptions = NOFLAGS ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
450+ proc waitForReceive * (c: ZConnection , timeout: int = - 1 , flags: ZSendRecvOptions = defaultFlag () ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
429451 # # Set RCVTIMEO for the socket and wait until a message is available.
430452 # # This function is blocking.
431453 # #
@@ -438,19 +460,19 @@ proc waitForReceive*(c: ZConnection, timeout: int = -1, flags: ZSendRecvOptions
438460 # # Indicate if more parts are needed to be received by ``moreAvailable``
439461 waitForReceive (c.socket, timeout, flags)
440462
441- proc tryReceive * (c: ZConnection , flags: ZSendRecvOptions = NOFLAGS ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
463+ proc tryReceive * (c: ZConnection , flags: ZSendRecvOptions = defaultFlag () ): tuple [msgAvailable: bool , moreAvailable: bool , msg: string ] =
442464 # # Receives a message from a socket in a non-blocking way.
443465 # #
444466 # # Indicate whether a message was received or EAGAIN occured by ``msgAvailable``
445467 # #
446468 # # Indicate if more parts are needed to be received by ``moreAvailable``
447469 tryReceive (c.socket, flags)
448470
449- proc receive * (c: ZConnection , flags: ZSendRecvOptions = NOFLAGS ): string =
471+ proc receive * (c: ZConnection , flags: ZSendRecvOptions = defaultFlag () ): string =
450472 # # Receive data over the connection
451473 receive (c.socket, flags)
452474
453- proc receiveAll * (c: ZConnection , flags: ZSendRecvOptions = NOFLAGS ): seq [string ] =
475+ proc receiveAll * (c: ZConnection , flags: ZSendRecvOptions = defaultFlag () ): seq [string ] =
454476 # # Receive all parts of a message
455477 # #
456478 # # If EAGAIN occurs without any data being received, it will be an empty seq
0 commit comments