class UnionStationHooks::MessageChannel
This class allows reading and writing structured messages over I/O channels. This is the Ruby implementation of Passenger's src/cxx_supportlib/Utils/MessageIO.h; see that file for more information.
@private
Constants
- DELIMITER
- DELIMITER_NAME
- HEADER_SIZE
- UINT16_PACK_FORMAT
- UINT32_PACK_FORMAT
Attributes
The wrapped IO object.
Public Class Methods
Create a new MessageChannel by wrapping the given IO object.
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/message_channel.rb, line 45 def initialize(io = nil) @io = io # Make it binary just in case. @io.binmode if @io end
Public Instance Methods
Read an array message from the underlying file descriptor. Returns the array message as an array, or nil when end-of-stream has been reached.
Might raise SystemCallError, IOError or SocketError when something goes wrong.
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/message_channel.rb, line 60 def read buffer = new_buffer if !@io.read(HEADER_SIZE, buffer) return nil end while buffer.size < HEADER_SIZE tmp = @io.read(HEADER_SIZE - buffer.size) if tmp.empty? return nil else buffer << tmp end end chunk_size = buffer.unpack(UINT16_PACK_FORMAT)[0] if !@io.read(chunk_size, buffer) return nil end while buffer.size < chunk_size tmp = @io.read(chunk_size - buffer.size) if tmp.empty? return nil else buffer << tmp end end message = [] offset = 0 delimiter_pos = buffer.index(DELIMITER, offset) while !delimiter_pos.nil? if delimiter_pos == 0 message << '' else message << buffer[offset..delimiter_pos - 1] end offset = delimiter_pos + 1 delimiter_pos = buffer.index(DELIMITER, offset) end message rescue Errno::ECONNRESET nil end
Send an array message, which consists of the given elements, over the underlying file descriptor. name is the first element in the message, and args are the other elements. These arguments will internally be converted to strings by calling to_s().
Might raise SystemCallError, IOError or SocketError when something goes wrong.
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/message_channel.rb, line 114 def write(name, *args) check_argument(name) args.each do |arg| check_argument(arg) end message = "#{name}#{DELIMITER}" args.each do |arg| message << arg.to_s << DELIMITER end @io.write([message.size].pack('n') << message) @io.flush end
Send a scalar message over the underlying IO object.
Might raise SystemCallError, IOError or SocketError when something goes wrong.
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/message_channel.rb, line 132 def write_scalar(data) @io.write([data.size].pack('N') << data) @io.flush end
Private Instance Methods
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/message_channel.rb, line 139 def check_argument(arg) if arg.to_s.index(DELIMITER) raise ArgumentError, "Message name and arguments may not contain #{DELIMITER_NAME}" end end
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/message_channel.rb, line 147 def new_buffer ByteString.new end