lib/0mq/socket.rb in 0mq-0.3.0 vs lib/0mq/socket.rb in 0mq-0.4.0

- old
+ new

@@ -13,10 +13,11 @@ attr_reader :context # The socket's ZeroMQ socket type (e.g. ZMQ::ROUTER). attr_reader :type def initialize(type, opts={}) + @closed = false @context = opts.fetch :context, ZMQ::DefaultContext @type = type @pointer = LibZMQ.zmq_socket @context.pointer, @type ZMQ.error_check true if @pointer.null? @@ -24,20 +25,32 @@ ObjectSpace.define_finalizer self, self.class.finalizer(@socket, Process.pid) end + # Show a useful inspect output + def inspect + "#<#{self.class}:#{type_sym}:#{object_id.to_s(16)}>" + end + # Close the socket def close + @closed = true + if @pointer ObjectSpace.undefine_finalizer self @temp_buffers.clear if @temp_buffers rc = LibZMQ.zmq_close @pointer ZMQ.error_check true if rc==-1 @pointer = nil end + end + + # Returns true if the socket is closed. + def closed? + @closed end # Create a safe finalizer for the socket pointer to close on GC of the object def self.finalizer(pointer, pid) Proc.new { LibZMQ.zmq_close pointer if Process.pid == pid }