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   fileno   new   read   read_scalar   recv_io   send_io   write   write_scalar  

Attributes

io  [R]  The wrapped IO object.

Public Class methods

Create a new MessageChannel by wrapping the given IO object.

[Source]

    # File lib/phusion_passenger/message_channel.rb, line 82
82:         def initialize(io)
83:                 @io = io
84:         end

Public Instance methods

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

[Source]

     # File lib/phusion_passenger/message_channel.rb, line 216
216:         def close
217:                 @io.close
218:         end

Return the file descriptor of the underlying IO object.

[Source]

     # File lib/phusion_passenger/message_channel.rb, line 210
210:         def fileno
211:                 return @io.fileno
212:         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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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

Send a scalar message over the underlying IO object.

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

[Source]

     # File lib/phusion_passenger/message_channel.rb, line 184
184:         def write_scalar(data)
185:                 @io.write([data.size].pack('N') << data)
186:                 @io.flush
187:         end

[Validate]