lib/ffi-rzmq/socket.rb in ffi-rzmq-0.9.6 vs lib/ffi-rzmq/socket.rb in ffi-rzmq-0.9.7
- old
+ new
@@ -261,19 +261,11 @@
#
# With a -1 return code, the user must check ZMQ.errno to determine the
# cause.
#
def send_strings parts, flags = 0
- return -1 if !parts || parts.empty?
- flags = NonBlocking if dontwait?(flags)
-
- parts[0..-2].each do |part|
- rc = send_string part, (flags | ZMQ::SNDMORE)
- return rc unless Util.resultcode_ok?(rc)
- end
-
- send_string parts[-1], flags
+ send_multiple(parts, flags, :send_string)
end
# Send a sequence of messages as a multipart message out of the +parts+
# passed in for transmission. Every element of +parts+ should be
# a Message (or subclass).
@@ -287,19 +279,11 @@
#
# With a -1 return code, the user must check ZMQ.errno to determine the
# cause.
#
def sendmsgs parts, flags = 0
- return -1 if !parts || parts.empty?
- flags = NonBlocking if dontwait?(flags)
-
- parts[0..-2].each do |part|
- rc = sendmsg part, (flags | ZMQ::SNDMORE)
- return rc unless Util.resultcode_ok?(rc)
- end
-
- sendmsg parts[-1], flags
+ send_multiple(parts, flags, :sendmsg)
end
# Sends a message. This will automatically close the +message+ for both successful
# and failed sends.
#
@@ -442,10 +426,27 @@
rc
end
private
+
+ def send_multiple(parts, flags, method_name)
+ if !parts || parts.empty?
+ -1
+ else
+ flags = NonBlocking if dontwait?(flags)
+ rc = nil
+
+ parts[0..-2].each do |part|
+ rc = send(method_name, part, (flags | ZMQ::SNDMORE))
+ break unless Util.resultcode_ok?(rc)
+ end
+
+ Util.resultcode_ok?(rc) ? send(method_name, parts[-1], flags) : rc
+ end
+
+ end
def __getsockopt__ name, array
# a small optimization so we only have to determine the option
# type a single time; gives approx 5% speedup to do it this way.
option_type = @option_lookup[name]
@@ -471,43 +472,23 @@
# for subsequent calls. This is a big perf win for calling RCVMORE which happens quite often.
# Cannot save the buffer for the IDENTITY.
def sockopt_buffers option_type
if 1 == option_type
# int64_t or uint64_t
- unless @longlong_cache
- length = FFI::MemoryPointer.new :size_t
- length.write_int 8
- @longlong_cache = [FFI::MemoryPointer.new(:int64), length]
- end
+ @longlong_cache ||= alloc_pointer(:int64, 8)
- @longlong_cache
-
elsif 0 == option_type
# int, 0mq assumes int is 4-bytes
- unless @int_cache
- length = FFI::MemoryPointer.new :size_t
- length.write_int 4
- @int_cache = [FFI::MemoryPointer.new(:int32), length]
- end
+ @int_cache ||= alloc_pointer(:int32, 4)
- @int_cache
-
elsif 2 == option_type
- length = FFI::MemoryPointer.new :size_t
- # could be a string of up to 255 bytes
- length.write_int 255
- [FFI::MemoryPointer.new(255), length]
+ # could be a string of up to 255 bytes, so allocate for worst case
+ alloc_pointer(255, 255)
else
# uh oh, someone passed in an unknown option; use a slop buffer
- unless @int_cache
- length = FFI::MemoryPointer.new :size_t
- length.write_int 4
- @int_cache = [FFI::MemoryPointer.new(:int32), length]
- end
-
- @int_cache
+ @int_cache ||= alloc_pointer(:int32, 4)
end
end
def populate_option_lookup
# integer options
@@ -527,9 +508,15 @@
def dontwait?(flags)
(NonBlocking & flags) == NonBlocking
end
alias :noblock? :dontwait?
+
+ def alloc_pointer(kind, length)
+ pointer = FFI::MemoryPointer.new :size_t
+ pointer.write_int(length)
+ [FFI::MemoryPointer.new(kind), pointer]
+ end
end # module CommonSocketBehavior
module IdentitySupport