lib/ffi-rzmq/socket.rb in ffi-rzmq-0.7.2 vs lib/ffi-rzmq/socket.rb in ffi-rzmq-0.8.0

- old
+ new

@@ -17,11 +17,11 @@ # Allocates a socket of type +type+ for sending and receiving data. # # +type+ can be one of ZMQ::REQ, ZMQ::REP, ZMQ::PUB, # ZMQ::SUB, ZMQ::PAIR, ZMQ::PULL, ZMQ::PUSH, - # ZMQ::XREQ or ZMQ::XREP. + # ZMQ::DEALER or ZMQ::ROUTER. # # By default, this class uses ZMQ::Message for manual # memory management. For automatic garbage collection of received messages, # it is possible to override the :receiver_class to use ZMQ::ManagedMessage. # @@ -142,11 +142,11 @@ # SocketError:: See all of the possibilities in the docs for #SocketError. # def getsockopt option_name begin option_value = FFI::MemoryPointer.new :pointer - option_length = FFI::MemoryPointer.new :size_t + option_length = FFI::MemoryPointer.new(:size_t) rescue FFI::MemoryPointer.new(:ulong) unless [ RCVMORE, HWM, SWAP, AFFINITY, RATE, RECOVERY_IVL, MCAST_LOOP, IDENTITY, SNDBUF, RCVBUF, FD, EVENTS, LINGER, RECONNECT_IVL, BACKLOG, RECOVERY_IVL_MSEC ].include? option_name @@ -224,10 +224,11 @@ if @socket remove_finalizer result_code = LibZMQ.zmq_close @socket error_check ZMQ_CLOSE_STR, result_code @socket = nil + release_cache end end # Queues the message for transmission. Message is assumed to conform to the # same public API as #Message. @@ -240,18 +241,13 @@ # Returns true when the message was successfully enqueued. # Returns false under two conditions. # 1. The message could not be enqueued # 2. When +flags+ is set with ZMQ::NOBLOCK and the socket returned EAGAIN. # - # The application code is *not* responsible for handling the +message+ object - # lifecycle when #send returns successfully or it raises an exception. The - # #send method takes ownership of the +message+ and its associated buffers. - # Both successful and failed calls will release the +message+ data buffer. + # The application code is responsible for handling the +message+ object + # lifecycle when #send returns. # - # Again, once a +message+ object has been passed to this method, - # do not try to access its #data buffer anymore. The 0mq library now owns it. - # # Can raise two kinds of exceptions depending on the error. # ContextError:: Raised when a socket operation is attempted on a terminated # #Context. See #ContextError. # SocketError:: See all of the possibilities in the docs for #SocketError. # @@ -260,12 +256,10 @@ result_code = LibZMQ.zmq_send @socket, message.address, flags # when the flag isn't set, do a normal error check # when set, check to see if the message was successfully queued queued = flags != NOBLOCK ? error_check(ZMQ_SEND_STR, result_code) : error_check_nonblock(result_code) - ensure - message.close end # true if sent, false if failed/EAGAIN queued end @@ -279,15 +273,29 @@ # ContextError:: Raised when a socket operation is attempted on a terminated # #Context. See #ContextError. # SocketError:: See all of the possibilities in the docs for #SocketError. # def send_string message_string, flags = 0 - message = Message.new - message.copy_in_string message_string - result = send message, flags - result + message = Message.new message_string + result_code = send_and_close message, flags + + result_code end + + # Sends a message. This will automatically close the +message+ for both successful + # and failed sends. + # + # Raises the same exceptions as Socket#send + # + def send_and_close message, flags = 0 + begin + result_code = send message, flags + ensure + message.close + end + result_code + end # Dequeues a message from the underlying queue. By default, this is a blocking operation. # # +flags+ may take two values: # 0 (default) - blocking operation @@ -401,9 +409,13 @@ when MCAST_LOOP option_value ? 1 : 0 else option_value end + end + + def release_cache + @sockopt_cache.clear end # require a minimum of 0mq 2.1.0 to support socket finalizers; it contains important # fixes for sockets and threads so that a garbage collector thread can successfully # reap this resource without crashing