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
Attributes
[R] io The wrapped IO object.
Public Class methods
new(io)

Create a new MessageChannel by wrapping the given IO object.

    # File lib/passenger/message_channel.rb, line 77
77:         def initialize(io)
78:                 @io = io
79:         end
Public Instance methods
close()

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

     # File lib/passenger/message_channel.rb, line 210
210:         def close
211:                 @io.close
212:         end
fileno()

Return the file descriptor of the underlying IO object.

     # File lib/passenger/message_channel.rb, line 204
204:         def fileno
205:                 return @io.fileno
206:         end
read()

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/passenger/message_channel.rb, line 87
 87:         def read
 88:                 buffer = ''
 89:                 while buffer.size < HEADER_SIZE
 90:                         buffer << @io.readpartial(HEADER_SIZE - buffer.size)
 91:                 end
 92:                 
 93:                 chunk_size = buffer.unpack('n')[0]
 94:                 buffer = ''
 95:                 while buffer.size < chunk_size
 96:                         buffer << @io.readpartial(chunk_size - buffer.size)
 97:                 end
 98:                 
 99:                 message = []
100:                 offset = 0
101:                 delimiter_pos = buffer.index(DELIMITER, offset)
102:                 while !delimiter_pos.nil?
103:                         if delimiter_pos == 0
104:                                 message << ""
105:                         else
106:                                 message << buffer[offset .. delimiter_pos - 1]
107:                         end
108:                         offset = delimiter_pos + 1
109:                         delimiter_pos = buffer.index(DELIMITER, offset)
110:                 end
111:                 return message
112:         rescue Errno::ECONNRESET
113:                 return nil
114:         rescue EOFError
115:                 return nil
116:         end
read_scalar(max_size = nil)

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/passenger/message_channel.rb, line 127
127:         def read_scalar(max_size = nil)
128:                 buffer = ''
129:                 temp = ''
130:                 while buffer.size < 4
131:                         buffer << @io.readpartial(4 - buffer.size, temp)
132:                 end
133:                 size = buffer.unpack('N')[0]
134:                 if size == 0
135:                         return ''
136:                 else
137:                         if !max_size.nil? && size > max_size
138:                                 raise SecurityError, "Scalar message size (#{size}) " <<
139:                                         "exceeds maximum allowed size (#{max_size})."
140:                         end
141:                         buffer = ''
142:                         while buffer.size < size
143:                                 buffer << @io.readpartial(size - buffer.size, temp)
144:                         end
145:                         return buffer
146:                 end
147:         rescue Errno::ECONNRESET
148:                 return nil
149:         rescue EOFError
150:                 return nil
151:         end
recv_io()

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/passenger/message_channel.rb, line 189
189:         def recv_io
190:                 return @io.recv_io
191:         end
send_io(io)

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/passenger/message_channel.rb, line 199
199:         def send_io(io)
200:                 @io.send_io(io)
201:         end
write(name, *args)

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/passenger/message_channel.rb, line 160
160:         def write(name, *args)
161:                 check_argument(name)
162:                 args.each do |arg|
163:                         check_argument(arg)
164:                 end
165:                 
166:                 message = "#{name}#{DELIMITER}"
167:                 args.each do |arg|
168:                         message << arg.to_s << DELIMITER
169:                 end
170:                 @io.write([message.size].pack('n') << message)
171:                 @io.flush
172:         end
write_scalar(data)

Send a scalar message over the underlying IO object.

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

     # File lib/passenger/message_channel.rb, line 178
178:         def write_scalar(data)
179:                 @io.write([data.size].pack('N') << data)
180:                 @io.flush
181:         end