Created
May 7, 2010 15:59
-
-
Save chuckremes/393627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module LibZMQ | |
extend FFI::Library | |
LINUX = ["libzmq.so", "/usr/local/lib/libzmq.so", "/opt/local/lib/libzmq.so"] | |
OSX = ["libzmq.dylib", "/usr/local/lib/libzmq.dylib", "/opt/local/lib/libzmq.dylib"] | |
WINDOWS = [] | |
ffi_lib(LINUX + OSX + WINDOWS) | |
# Message api | |
class Msg_t < FFI::Struct | |
layout :content, :pointer, | |
:flags, :uint8, | |
:vsm_size, :uint8, | |
:vsm_data, [:uint8, 30] | |
end # class Msg_t | |
attach_function :zmq_msg_init, [:pointer], :int | |
attach_function :zmq_msg_init_size, [:pointer, :size_t], :int | |
callback :message_deallocator, [:pointer, :pointer], :void | |
attach_function :zmq_msg_init_data, [:pointer, :pointer, :size_t, :message_deallocator, :pointer], :int | |
attach_function :zmq_msg_close, [:pointer], :int | |
attach_function :zmq_msg_data, [:pointer], :pointer | |
attach_function :zmq_msg_size, [:pointer], :size_t | |
attach_function :zmq_msg_copy, [:pointer, :pointer], :int | |
attach_function :zmq_msg_move, [:pointer, :pointer], :int | |
# Called by zmq_close | |
MessageDeallocator = Proc.new do |data_ptr, hint_ptr| | |
#puts "msg_callback: freed data [#{data_ptr}]" | |
data_ptr.free | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Message | |
include ZMQ::Util | |
def initialize message = nil | |
@struct = LibZMQ::Msg_t.new | |
if message | |
data = FFI::MemoryPointer.from_string message.to_s | |
result_code = LibZMQ.zmq_msg_init_size @struct, message.size | |
error_check ZMQ_MSG_INIT_SIZE_STR, result_code | |
# assign a memory deallocation callback function | |
result_code = LibZMQ.zmq_msg_init_data @struct, data, message.size, LibZMQ::MessageDeallocator, nil | |
error_check ZMQ_MSG_INIT_DATA_STR, result_code | |
else | |
result_code = LibZMQ.zmq_msg_init @struct | |
error_check ZMQ_MSG_INIT_STR, result_code | |
end | |
end | |
def address | |
@struct | |
end | |
def size | |
LibZMQ.zmq_msg_size @struct | |
end | |
def data | |
data_ptr.read_string(size) | |
end | |
def close | |
result_code = LibZMQ.zmq_msg_close @struct | |
error_check ZMQ_MSG_CLOSE_STR, result_code | |
@struct = nil | |
end | |
private | |
def data_ptr | |
LibZMQ.zmq_msg_data @struct | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment