Class PhusionPassenger::MessageChannel
In: lib/phusion_passenger/message_channel.rb
Parent: Object

This class provides convenience methods for:

  • sending and receiving raw data over an IO channel.
  • sending and receiving messages over an IO channel.
  • file descriptor (IO object) passing over a Unix socket.

All of these methods use exceptions for error reporting.

There are two kinds of messages:

Array messages
These are just a list of strings, and the message itself has a specific length. The contained strings may not contain NUL characters (’\0‘). Note that an array message must have at least one element.
Scalar messages
These are byte strings which may contain arbitrary binary data. Scalar messages also have a specific length.

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.

Methods

close   closed?   fileno   new   read   read_hash   read_scalar   recv_io   send_io   write   write_scalar  

Classes and Modules

Class PhusionPassenger::MessageChannel::InvalidHashError

Attributes

io  [RW]  The wrapped IO object.

Public Class methods

Create a new MessageChannel by wrapping the given IO object.

Public Instance methods

Close the underlying IO stream. Might raise SystemCallError or IOError when something goes wrong.

Checks whether the underlying IO stream is closed.

Return the file descriptor of the underlying IO object.

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.

Read an array message from the underlying file descriptor and return the result as a hash instead of an array. This assumes that the array message has an even number of elements. Returns nil when end-of-stream has been reached.

Might raise SystemCallError, IOError or SocketError when something goes wrong.

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 buffer argument specifies a buffer in which read_scalar stores the read data. It is good practice to reuse existing buffers in order to minimize stress on the garbage collector.

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.

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.

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.

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.

Send a scalar message over the underlying IO object.

Might raise SystemCallError, IOError or SocketError when something goes wrong.

[Validate]