Class: CZTop::Frame
- Inherits:
-
Object
- Object
- CZTop::Frame
- Extended by:
- HasFFIDelegate::ClassMethods
- Includes:
- HasFFIDelegate
- Defined in:
- lib/cztop/frame.rb
Overview
Dealing with frames (parts of a message) is pretty low-level. You'll probably not really need this functionality. It's only useful when you need to be able to receive and send single frames. Just use Message instead.
Represents a CZMQ::FFI::Zframe, a part of a message.
Constant Summary collapse
- FLAG_MORE =
1
- FLAG_REUSE =
2
- FLAG_DONTWAIT =
4
Instance Attribute Summary
Attributes included from HasFFIDelegate
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare to another frame.
-
#content ⇒ String
(also: #to_s)
Content as string (encoding = Encoding::BINARY).
-
#content=(new_content) ⇒ new_content
Sets new content of this Frame.
-
#dup ⇒ Frame
Duplicates a frame.
-
#empty? ⇒ Boolean
If this Frame has zero-sized content.
-
#group ⇒ String?
Gets the group (radio/dish pattern).
-
#group=(new_group) ⇒ new_group
Sets a new group (radio/dish pattern).
-
#initialize(content = nil) ⇒ Frame
constructor
Initialize a new Frame.
-
#more=(indicator) ⇒ indicator
Sets the MORE indicator.
-
#more? ⇒ Boolean
If the MORE indicator is set.
-
#routing_id ⇒ Integer
Gets the routing ID.
-
#routing_id=(new_routing_id) ⇒ new_routing_id
Sets a new routing ID.
- #send_to(destination, more: false, reuse: false, dontwait: false) ⇒ void
-
#size ⇒ Integer
Content length in bytes.
Methods included from HasFFIDelegate::ClassMethods
ffi_delegate, from_ffi_delegate
Methods included from HasFFIDelegate
#attach_ffi_delegate, #from_ffi_delegate, #raise_zmq_err, raise_zmq_err, #to_ptr
Constructor Details
#initialize(content = nil) ⇒ Frame
Initialize a new CZTop::Frame.
16 17 18 19 |
# File 'lib/cztop/frame.rb', line 16 def initialize(content = nil) attach_ffi_delegate(CZMQ::FFI::Zframe.new_empty) self.content = content if content end |
Class Method Details
.receive_from(source) ⇒ Frame
This is low-level. Consider just receiving a Message.
Receive CZTop::Frame from a Socket/Actor.
69 70 71 |
# File 'lib/cztop/frame.rb', line 69 def self.receive_from(source) from_ffi_delegate(CZMQ::FFI::Zframe.recv(source)) end |
Instance Method Details
#==(other) ⇒ Boolean
132 133 134 |
# File 'lib/cztop/frame.rb', line 132 def ==(other) ffi_delegate.eq(other.ffi_delegate) end |
#content ⇒ String Also known as: to_s
This string is always binary. Use String#force_encoding if needed.
Returns content as string (encoding = Encoding::BINARY).
75 76 77 |
# File 'lib/cztop/frame.rb', line 75 def content ffi_delegate.data.read_string(size) end |
#content=(new_content) ⇒ new_content
Sets new content of this CZTop::Frame.
88 89 90 91 92 93 |
# File 'lib/cztop/frame.rb', line 88 def content=(new_content) content_ptr = ::FFI::MemoryPointer.new(new_content.bytesize) content_ptr.write_bytes(new_content) ffi_delegate.reset(content_ptr, content_ptr.size) # NOTE: FFI::MemoryPointer will autorelease end |
#dup ⇒ Frame
Duplicates a frame.
97 98 99 |
# File 'lib/cztop/frame.rb', line 97 def dup from_ffi_delegate(ffi_delegate.dup) end |
#empty? ⇒ Boolean
Returns if this CZTop::Frame has zero-sized content.
81 82 83 |
# File 'lib/cztop/frame.rb', line 81 def empty? size.zero? end |
#group ⇒ String?
This is only set when the frame has been read from a Socket::DISH socket.
Gets the group (radio/dish pattern).
163 164 165 166 167 |
# File 'lib/cztop/frame.rb', line 163 def group group = ffi_delegate.group return nil if group.nil? || group.empty? group end |
#group=(new_group) ⇒ new_group
This is used when the frame is sent via a Socket::RADIO socket.
Sets a new group (radio/dish pattern).
175 176 177 178 |
# File 'lib/cztop/frame.rb', line 175 def group=(new_group) rc = ffi_delegate.set_group(new_group) raise_zmq_err("unable to set group to %p" % new_group) if rc == -1 end |
#more=(indicator) ⇒ indicator
This is NOT used when sending frame to socket.
Sets the MORE indicator.
113 114 115 |
# File 'lib/cztop/frame.rb', line 113 def more=(indicator) ffi_delegate.set_more(indicator ? 1 : 0) end |
#more? ⇒ Boolean
104 105 106 |
# File 'lib/cztop/frame.rb', line 104 def more? ffi_delegate.more == 1 end |
#routing_id ⇒ Integer
This only set when the frame has been read from a Socket::SERVER socket.
Gets the routing ID.
143 |
# File 'lib/cztop/frame.rb', line 143 ffi_delegate :routing_id |
#routing_id=(new_routing_id) ⇒ new_routing_id
This is used when the frame is sent via a Socket::CLIENT socket.
Sets a new routing ID.
151 152 153 154 155 156 |
# File 'lib/cztop/frame.rb', line 151 def routing_id=(new_routing_id) # need to raise manually, as FFI lacks this feature. # @see https://github.com/ffi/ffi/issues/473 raise RangeError if new_routing_id < 0 ffi_delegate.set_routing_id(new_routing_id) end |
#send_to(destination, more: false, reuse: false, dontwait: false) ⇒ void
If you don't specify reuse: true, do NOT use this CZTop::Frame anymore afterwards. Its native counterpart will have been destroyed.
This is low-level. Consider just sending a Message.
This method returns an undefined value.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cztop/frame.rb', line 41 def send_to(destination, more: false, reuse: false, dontwait: false) flags = 0 flags |= FLAG_MORE if more flags |= FLAG_REUSE if reuse flags |= FLAG_DONTWAIT if dontwait # remember pointer, in case the zframe_t won't be destroyed zframe_ptr = ffi_delegate.to_ptr ret = CZMQ::FFI::Zframe.send(ffi_delegate, destination, flags) if reuse || ret == -1 # zframe_t hasn't been destroyed yet: avoid memory leak. attach_ffi_delegate(CZMQ::FFI::Zframe.__new(zframe_ptr, true)) # OPTIMIZE: reuse existing Zframe object by redefining its finalizer end if ret == -1 if dontwait && FFI.errno == Errno::EAGAIN::Errno raise IO::EAGAINWaitWritable end raise_zmq_err end end |
#size ⇒ Integer
Returns content length in bytes.
137 |
# File 'lib/cztop/frame.rb', line 137 ffi_delegate :size |