Class | PhusionPassenger::MessageChannel |
In: |
lib/phusion_passenger/message_channel.rb
|
Parent: | Object |
This class provides convenience methods for:
All of these methods use exceptions for error reporting.
There are two kinds of messages:
The protocol is designed to be low overhead, easy to implement and easy to parse.
MessageChannel is to be wrapped around an IO object. For example:
a, b = IO.pipe channel1 = MessageChannel.new(a) channel2 = MessageChannel.new(b) # Send an array message. channel2.write("hello", "world !!") channel1.read # => ["hello", "world !!"] # Send a scalar message. channel2.write_scalar("some long string which can contain arbitrary binary data") channel1.read_scalar
The life time of a MessageChannel is independent from that of the wrapped IO object. If a MessageChannel object is destroyed, the underlying IO object is not automatically closed. Call close() if you want to close the underlying IO object.
Note: Be careful with mixing the sending/receiving of array messages, scalar messages and IO objects. If you send a collection of any of these in a specific order, then the receiving side must receive them in the exact some order. So suppose you first send a message, then an IO object, then a scalar, then the receiving side must first receive a message, then an IO object, then a scalar. If the receiving side does things in the wrong order then bad things will happen.
io | [R] | The wrapped IO object. |
Create a new MessageChannel by wrapping the given IO object.
# File lib/phusion_passenger/message_channel.rb, line 82 82: def initialize(io) 83: @io = io 84: end
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 lib/phusion_passenger/message_channel.rb, line 92 92: def read 93: buffer = '' 94: while buffer.size < HEADER_SIZE 95: buffer << @io.readpartial(HEADER_SIZE - buffer.size) 96: end 97: 98: chunk_size = buffer.unpack('n')[0] 99: buffer = '' 100: while buffer.size < chunk_size 101: buffer << @io.readpartial(chunk_size - buffer.size) 102: end 103: 104: message = [] 105: offset = 0 106: delimiter_pos = buffer.index(DELIMITER, offset) 107: while !delimiter_pos.nil? 108: if delimiter_pos == 0 109: message << "" 110: else 111: message << buffer[offset .. delimiter_pos - 1] 112: end 113: offset = delimiter_pos + 1 114: delimiter_pos = buffer.index(DELIMITER, offset) 115: end 116: return message 117: rescue Errno::ECONNRESET 118: return nil 119: rescue EOFError 120: return nil 121: end
Read a scalar message from the underlying IO object. Returns the read message, or nil on end-of-stream.
Might raise SystemCallError, IOError or SocketError when something goes wrong.
The max_size argument allows one to specify the maximum allowed size for the scalar message. If the received scalar message‘s size is larger than max_size, then a SecurityError will be raised.
# File lib/phusion_passenger/message_channel.rb, line 132 132: def read_scalar(max_size = nil) 133: buffer = '' 134: temp = '' 135: while buffer.size < 4 136: buffer << @io.readpartial(4 - buffer.size, temp) 137: end 138: size = buffer.unpack('N')[0] 139: if size == 0 140: return '' 141: else 142: if !max_size.nil? && size > max_size 143: raise SecurityError, "Scalar message size (#{size}) " << 144: "exceeds maximum allowed size (#{max_size})." 145: end 146: buffer = '' 147: while buffer.size < size 148: temp = '' # JRuby doesn't clear the buffer. TODO: remove this when JRuby has been fixed. 149: buffer << @io.readpartial(size - buffer.size, temp) 150: end 151: return buffer 152: end 153: rescue Errno::ECONNRESET 154: return nil 155: rescue EOFError 156: return nil 157: end
Receive an IO object (a file descriptor) from the channel. The other side must have sent an IO object by calling send_io(). Note that this only works on Unix sockets.
Might raise SystemCallError, IOError or SocketError when something goes wrong.
# File lib/phusion_passenger/message_channel.rb, line 195 195: def recv_io 196: return @io.recv_io 197: end
Send an IO object (a file descriptor) over the channel. The other side must receive the IO object by calling recv_io(). Note that this only works on Unix sockets.
Might raise SystemCallError, IOError or SocketError when something goes wrong.
# File lib/phusion_passenger/message_channel.rb, line 205 205: def send_io(io) 206: @io.send_io(io) 207: 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 lib/phusion_passenger/message_channel.rb, line 166 166: def write(name, *args) 167: check_argument(name) 168: args.each do |arg| 169: check_argument(arg) 170: end 171: 172: message = "#{name}#{DELIMITER}" 173: args.each do |arg| 174: message << arg.to_s << DELIMITER 175: end 176: @io.write([message.size].pack('n') << message) 177: @io.flush 178: end