Hello,

Attached is a patch implementing ZMQ_EVENTS handler to zmq_getsockopt for use in combination with ZMQ_FD based on sustrik's zeromq2:master branch.

This patch is submitted under the MIT license.

regards,

chris
diff --git a/doc/zmq_getsockopt.txt b/doc/zmq_getsockopt.txt
index 0e4cfb5..f6aee20 100644
--- a/doc/zmq_getsockopt.txt
+++ b/doc/zmq_getsockopt.txt
@@ -199,6 +199,22 @@ Default value:: 0
 Applicable socket types:: all


+ZMQ_EVENTS: Retrieve pending socket events
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The 'ZMQ_EVENTS' option shall retrieve pending IN/OUT status for the specified
+'socket'.  A value of zero means that no events are pending. A value of
+'ZMQ_POLLIN' means at least one message may be received from the socket without
+blocking. A value of 'ZMQ_POLLOUT' means at least one message may be sent to
+the socket without blocking. If both IN and OUT events are pending the value
+will be 'ZMQ_POLLOUT|ZMQ_POLLIN'.
+
+[horizontal]
+Option value type:: uint32_t
+Option value unit:: bytes
+Default value:: 0
+Applicable socket types:: all
+
+
 RETURN VALUE
 ------------
 The _zmq_getsockopt()_ function shall return zero if successful. Otherwise it
diff --git a/include/zmq.h b/include/zmq.h
index 22f4438..bf62d49 100644
--- a/include/zmq.h
+++ b/include/zmq.h
@@ -175,6 +175,7 @@ ZMQ_EXPORT int zmq_term (void *context);
 #define ZMQ_RCVBUF 12
 #define ZMQ_RCVMORE 13
 #define ZMQ_FD 14
+#define ZMQ_EVENTS 15

 /*  Send/recv options.                                                        
*/
 #define ZMQ_NOBLOCK 1
diff --git a/src/socket_base.cpp b/src/socket_base.cpp
index 26a5680..901aca8 100644
--- a/src/socket_base.cpp
+++ b/src/socket_base.cpp
@@ -181,6 +181,21 @@ int zmq::socket_base_t::getsockopt (int option_, void 
*optval_,
         return 0;
     }

+    if (option_ == ZMQ_EVENTS) {
+        if (*optvallen_ < sizeof (uint32_t)) {
+            errno = EINVAL;
+            return -1;
+        }
+        process_commands(false, false);
+        *((uint32_t*) optval_) = 0;
+        if (has_out ())
+            *((uint32_t*) optval_) |= ZMQ_POLLOUT;
+        if (has_in ())
+            *((uint32_t*) optval_) |= ZMQ_POLLIN;
+        *optvallen_ = sizeof (uint32_t);
+        return 0;
+    }
+
     return options.getsockopt (option_, optval_, optvallen_);
 }
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to