lib/0mq/socket.rb in 0mq-0.2.1 vs lib/0mq/socket.rb in 0mq-0.3.0
- old
+ new
@@ -1,71 +1,77 @@
require_relative 'socket/options'
module ZMQ
+ # See http://api.zeromq.org/4-0:zmq-socket
+ # Not thread safe.
class Socket
- attr_reader :ptr
+
+ # The FFI pointer to the socket.
+ attr_reader :pointer
+ # The socket's ZMQ::Context.
attr_reader :context
+ # The socket's ZeroMQ socket type (e.g. ZMQ::ROUTER).
attr_reader :type
def initialize(type, opts={})
@context = opts.fetch :context, ZMQ::DefaultContext
@type = type
- @ptr = LibZMQ.zmq_socket @context.ptr, @type
- ZMQ.error_check true if @ptr.null?
+ @pointer = LibZMQ.zmq_socket @context.pointer, @type
+ ZMQ.error_check true if @pointer.null?
@msgptr = FFI::MemoryPointer.new LibZMQ::Message.size, 1, false
ObjectSpace.define_finalizer self,
self.class.finalizer(@socket, Process.pid)
end
# Close the socket
def close
- if @ptr
+ if @pointer
ObjectSpace.undefine_finalizer self
@temp_buffers.clear if @temp_buffers
- rc = LibZMQ.zmq_close @ptr
+ rc = LibZMQ.zmq_close @pointer
ZMQ.error_check true if rc==-1
- @ptr = nil
+ @pointer = nil
end
end
- # Create a safe finalizer for the socket ptr to close on GC of the object
- def self.finalizer(ptr, pid)
- Proc.new { LibZMQ.zmq_close ptr if Process.pid == pid }
+ # 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 }
end
# Get the socket type name as a symbol
def type_sym
ZMQ::SocketTypeNameMap[type].to_sym
end
# Bind to an endpoint
def bind(endpoint)
- rc = LibZMQ.zmq_bind @ptr, endpoint
+ rc = LibZMQ.zmq_bind @pointer, endpoint
ZMQ.error_check true if rc==-1
end
# Connect to an endpoint
def connect(endpoint)
- rc = LibZMQ.zmq_connect @ptr, endpoint
+ rc = LibZMQ.zmq_connect @pointer, endpoint
ZMQ.error_check true if rc==-1
end
# Unbind from an endpoint
def unbind(endpoint)
- rc = LibZMQ.zmq_unbind @ptr, endpoint
+ rc = LibZMQ.zmq_unbind @pointer, endpoint
ZMQ.error_check true if rc==-1
end
- # Disconnect to an endpoint
+ # Disconnect from an endpoint
def disconnect(endpoint)
- rc = LibZMQ.zmq_disconnect @ptr, endpoint
+ rc = LibZMQ.zmq_disconnect @pointer, endpoint
ZMQ.error_check true if rc==-1
end
# Send a string to the socket
def send_string(string, flags = 0)
@@ -75,11 +81,11 @@
@msgbuf.write_string string, size
rc = LibZMQ.zmq_msg_init_data @msgptr, @msgbuf, size, LibC::Free, nil
ZMQ.error_check true if rc==-1
- rc = LibZMQ.zmq_sendmsg @ptr, @msgptr, flags
+ rc = LibZMQ.zmq_sendmsg @pointer, @msgptr, flags
ZMQ.error_check true if rc==-1
rc = LibZMQ.zmq_msg_close @msgptr
ZMQ.error_check true if rc==-1
end
@@ -87,11 +93,11 @@
# Receive a string from the socket
def recv_string(flags = 0)
rc = LibZMQ.zmq_msg_init @msgptr
ZMQ.error_check true if rc==-1
- rc = LibZMQ.zmq_recvmsg @ptr, @msgptr, flags
+ rc = LibZMQ.zmq_recvmsg @pointer, @msgptr, flags
ZMQ.error_check true if rc==-1
str = LibZMQ.zmq_msg_data(@msgptr)
.read_string(LibZMQ.zmq_msg_size(@msgptr))
@@ -161,11 +167,11 @@
valptr.send :"write_#{type}", value
end
value = valptr
end
- rc = LibZMQ.zmq_setsockopt @ptr, option, value, value.size
+ rc = LibZMQ.zmq_setsockopt @pointer, option, value, value.size
ZMQ.error_check true if rc==-1
value
end
@@ -174,19 +180,24 @@
type = @@option_types.fetch(option) \
{ raise ArgumentError, "Unknown option: #{option}" }
value, size = get_opt_pointers type
- rc = LibZMQ.zmq_getsockopt @ptr, option, value, size
+ rc = LibZMQ.zmq_getsockopt @pointer, option, value, size
ZMQ.error_check true if rc==-1
if type == :string
value.read_string(size.read_int-1)
elsif type == :bool
value.read_int == 1
else
value.send :"read_#{type}"
end
+ end
+
+ # Returns the socket's FFI pointer.
+ def to_ptr
+ @pointer
end
private
def get_opt_pointers(type)