lib/celluloid/zmq/sockets.rb in celluloid-zmq-0.9.0 vs lib/celluloid/zmq/sockets.rb in celluloid-zmq-0.10.0

- old
+ new

@@ -1,31 +1,36 @@ module Celluloid module ZMQ + attr_reader :linger + class Socket # Create a new socket def initialize(type) @socket = Celluloid::ZMQ.context.socket ::ZMQ.const_get(type.to_s.upcase) + @linger = 0 end # Connect to the given 0MQ address # Address should be in the form: tcp://1.2.3.4:5678/ def connect(addr) - puts "zomg connecting" unless ::ZMQ::Util.resultcode_ok? @socket.connect addr raise IOError, "error connecting to #{addr}: #{::ZMQ::Util.error_string}" end true end + def linger=(value) + @linger = value || -1 + + unless ::ZMQ::Util.resultcode_ok? @socket.setsockopt(::ZMQ::LINGER, value) + raise IOError, "couldn't set linger: #{::ZMQ::Util.error_string}" + end + end + # Bind to the given 0MQ address # Address should be in the form: tcp://1.2.3.4:5678/ def bind(addr) - unless ::ZMQ::Util.resultcode_ok? @socket.setsockopt(::ZMQ::LINGER, 0) - @socket.close - raise IOError, "couldn't set ZMQ::LINGER: #{::ZMQ::Util.error_string}" - end - unless ::ZMQ::Util.resultcode_ok? @socket.bind(addr) raise IOError, "couldn't bind to #{addr}: #{::ZMQ::Util.error_string}" end end @@ -47,9 +52,20 @@ alias_method :inspect, :to_s end # Readable 0MQ sockets have a read method module ReadableSocket + # always set LINGER on readable sockets + def bind(addr) + self.linger = @linger + super(addr) + end + + def connect(addr) + self.linger = @linger + super(addr) + end + # Read a message from the socket def read(buffer = '') Celluloid.current_actor.wait_readable(@socket) if evented? unless ::ZMQ::Util.resultcode_ok? @socket.recv_string buffer