Module: EventMachine::RTMP::IOHelpers
- Included in:
- Buffer, Connection, ConnectionDelegate
- Defined in:
- lib/em-rtmp/io_helpers.rb
Instance Method Summary collapse
-
#read_bitfield(*widths) ⇒ Object
Read a bit field and return the mapped results.
-
#read_double_be ⇒ Object
Read a double (big endian).
-
#read_int29 ⇒ Object
Read an int29.
- #read_safe(length) ⇒ Object
-
#read_uint16_be ⇒ Object
Read a unsigned 16-bit integer.
-
#read_uint24_be ⇒ Object
Read a unsigned 24-bit integer.
-
#read_uint32_be ⇒ Object
Read a unsigned 32-bit integer (big endian).
-
#read_uint32_le ⇒ Object
Read a unsigned 32-bit integer (little endian).
-
#read_uint8 ⇒ Object
Read a unsigned 8-bit integer.
-
#write_bitfield(*values_and_widths) ⇒ Object
Write a bit field to the stream.
-
#write_double_be(value) ⇒ Object
Write a double (big endian).
-
#write_int29(value) ⇒ Object
Write an int29.
-
#write_uint16_be(value) ⇒ Object
Write an unsigned 16-bit integer.
-
#write_uint24_be(value) ⇒ Object
Write an unsigned 24-bit integer.
-
#write_uint32_be(value) ⇒ Object
Write an unsigned 32-bit integer (big endian).
-
#write_uint32_le(value) ⇒ Object
Write an unsigned 32-bit integer (big endian).
-
#write_uint8(value) ⇒ Object
Write an unsigned 8-bit integer.
Instance Method Details
#read_bitfield(*widths) ⇒ Object
Read a bit field and return the mapped results
widths - Array of integers representing the size of the fields
Returns the value for each field read
154 155 156 157 158 159 |
# File 'lib/em-rtmp/io_helpers.rb', line 154 def read_bitfield(*widths) byte = read_uint8 shifts_and_masks(widths).map{ |shift, mask| (byte >> shift) & mask } end |
#read_double_be ⇒ Object
Read a double (big endian)
Returns the result of the read
88 89 90 |
# File 'lib/em-rtmp/io_helpers.rb', line 88 def read_double_be read_safe(8).unpack('G')[0] end |
#read_int29 ⇒ Object
Read an int29
Returns the result of the stream operation
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/em-rtmp/io_helpers.rb', line 104 def read_int29 count = 1 result = 0 byte = read_uint8 while (byte & 0x80 != 0) && count < 4 do result <<= 7 result |= (byte & 0x7f) byte = read_uint8 count += 1 end if count < 4 result <<= 7 result |= byte else result <<= 8 result |= byte end result end |
#read_safe(length) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/em-rtmp/io_helpers.rb', line 175 def read_safe(length) raise ArgumentError, "cannot read nothing: #{length}" unless length && length >= 1 if value = read(length) return value else Logger.error "unable to read from socket, closing connection" close_connection "" end end |
#read_uint16_be ⇒ Object
Read a unsigned 16-bit integer
Returns the result of the read
24 25 26 |
# File 'lib/em-rtmp/io_helpers.rb', line 24 def read_uint16_be read_safe(2).unpack('n')[0] end |
#read_uint24_be ⇒ Object
Read a unsigned 24-bit integer
Returns the result of the read
40 41 42 |
# File 'lib/em-rtmp/io_helpers.rb', line 40 def read_uint24_be ("\x00" + read_safe(3)).unpack('N')[0] end |
#read_uint32_be ⇒ Object
Read a unsigned 32-bit integer (big endian)
Returns the result of the read
56 57 58 |
# File 'lib/em-rtmp/io_helpers.rb', line 56 def read_uint32_be read_safe(4).unpack('N')[0] end |
#read_uint32_le ⇒ Object
Read a unsigned 32-bit integer (little endian)
Returns the result of the read
63 64 65 |
# File 'lib/em-rtmp/io_helpers.rb', line 63 def read_uint32_le read_safe(4).unpack('V')[0] end |
#read_uint8 ⇒ Object
Read a unsigned 8-bit integer
Returns the result of the read
8 9 10 |
# File 'lib/em-rtmp/io_helpers.rb', line 8 def read_uint8 read_safe(1).unpack('C')[0] end |
#write_bitfield(*values_and_widths) ⇒ Object
Write a bit field to the stream
values_and_widths - An array of arrays, each containing two values:
[0] - value to be written
[1] - width of value
Returns the value for each field read
168 169 170 171 172 173 |
# File 'lib/em-rtmp/io_helpers.rb', line 168 def write_bitfield(*values_and_widths) sm = shifts_and_masks(values_and_widths.map{ |_,w| w }) write_uint8 values_and_widths.zip(sm).inject(0){ |byte, ((value, width), (shift, mask))| byte | ((value & mask) << shift) } end |
#write_double_be(value) ⇒ Object
Write a double (big endian)
value - Value to write
Returns the result of the stream operation
97 98 99 |
# File 'lib/em-rtmp/io_helpers.rb', line 97 def write_double_be(value) write [value].pack('G') end |
#write_int29(value) ⇒ Object
Write an int29
value - Value to write
Returns the result of the stream operation
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/em-rtmp/io_helpers.rb', line 132 def write_int29(value) value = value & 0x1fffffff if(value < 0x80) result = [value].pack('c') write result elsif(value < 0x4000) result = [value >> 7 & 0x7f | 0x80].pack('c') + [value & 0x7f].pack('c') write result elsif(value < 0x200000) result = [value >> 14 & 0x7f | 0x80].pack('c') + [value >> 7 & 0x7f | 0x80].pack('c') + [value & 0x7f].pack('c') write result else result = [value >> 22 & 0x7f | 0x80].pack('c') + [value >> 15 & 0x7f | 0x80].pack('c') + [value >> 8 & 0x7f | 0x80].pack('c') + [value & 0xff].pack('c') write result end end |
#write_uint16_be(value) ⇒ Object
Write an unsigned 16-bit integer
value - Value to write
Returns the result of the stream operation
33 34 35 |
# File 'lib/em-rtmp/io_helpers.rb', line 33 def write_uint16_be(value) write [value].pack('n') end |
#write_uint24_be(value) ⇒ Object
Write an unsigned 24-bit integer
value - Value to write
Returns the result of the stream operation
49 50 51 |
# File 'lib/em-rtmp/io_helpers.rb', line 49 def write_uint24_be(value) write [value].pack('N')[1,3] end |
#write_uint32_be(value) ⇒ Object
Write an unsigned 32-bit integer (big endian)
value - Value to write
Returns the result of the stream operation
72 73 74 |
# File 'lib/em-rtmp/io_helpers.rb', line 72 def write_uint32_be(value) write [value].pack('N') end |
#write_uint32_le(value) ⇒ Object
Write an unsigned 32-bit integer (big endian)
value - Value to write
Returns the result of the stream operation
81 82 83 |
# File 'lib/em-rtmp/io_helpers.rb', line 81 def write_uint32_le(value) write [value].pack('V') end |
#write_uint8(value) ⇒ Object
Write an unsigned 8-bit integer
value - Value to write
Returns the result of the stream operation
17 18 19 |
# File 'lib/em-rtmp/io_helpers.rb', line 17 def write_uint8(value) write [value].pack('C') end |