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 76
76:         def initialize(io)
77:                 @io = io
78:         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 209
209:         def close
210:                 @io.close
211:         end
fileno()

Return the file descriptor of the underlying IO object.

     # File lib/passenger/message_channel.rb, line 203
203:         def fileno
204:                 return @io.fileno
205:         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 86
 86:         def read
 87:                 buffer = ''
 88:                 while buffer.size < HEADER_SIZE
 89:                         buffer << @io.readpartial(HEADER_SIZE - buffer.size)
 90:                 end
 91:                 
 92:                 chunk_size = buffer.unpack('n')[0]
 93:                 buffer = ''
 94:                 while buffer.size < chunk_size
 95:                         buffer << @io.readpartial(chunk_size - buffer.size)
 96:                 end
 97:                 
 98:                 message = []
 99:                 offset = 0
100:                 delimiter_pos = buffer.index(DELIMITER, offset)
101:                 while !delimiter_pos.nil?
102:                         if delimiter_pos == 0
103:                                 message << ""
104:                         else
105:                                 message << buffer[offset .. delimiter_pos - 1]
106:                         end
107:                         offset = delimiter_pos + 1
108:                         delimiter_pos = buffer.index(DELIMITER, offset)
109:                 end
110:                 return message
111:         rescue Errno::ECONNRESET
112:                 return nil
113:         rescue EOFError
114:                 return nil
115:         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 126
126:         def read_scalar(max_size = nil)
127:                 buffer = ''
128:                 temp = ''
129:                 while buffer.size < 4
130:                         buffer << @io.readpartial(4 - buffer.size, temp)
131:                 end
132:                 size = buffer.unpack('N')[0]
133:                 if size == 0
134:                         return ''
135:                 else
136:                         if !max_size.nil? && size > max_size
137:                                 raise SecurityError, "Scalar message size (#{size}) " <<
138:                                         "exceeds maximum allowed size (#{max_size})."
139:                         end
140:                         buffer = ''
141:                         while buffer.size < size
142:                                 buffer << @io.readpartial(size - buffer.size, temp)
143:                         end
144:                         return buffer
145:                 end
146:         rescue Errno::ECONNRESET
147:                 return nil
148:         rescue EOFError
149:                 return nil
150:         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 188
188:         def recv_io
189:                 return @io.recv_io
190:         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 198
198:         def send_io(io)
199:                 @io.send_io(io)
200:         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 159
159:         def write(name, *args)
160:                 check_argument(name)
161:                 args.each do |arg|
162:                         check_argument(arg)
163:                 end
164:                 
165:                 message = "#{name}#{DELIMITER}"
166:                 args.each do |arg|
167:                         message << arg.to_s << DELIMITER
168:                 end
169:                 @io.write([message.size].pack('n') << message)
170:                 @io.flush
171:         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 177
177:         def write_scalar(data)
178:                 @io.write([data.size].pack('N') << data)
179:                 @io.flush
180:         end