Module: Arsenicum::Core::IOHelper
Constant Summary collapse
- TYPE_INT =
"\xa0".force_encoding(Encoding::BINARY).freeze
- TYPE_STRING =
"\xfc".force_encoding(Encoding::BINARY).freeze
Instance Method Summary collapse
Instance Method Details
#read_message(io, encoding: Encoding::UTF_8) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/arsenicum/core/io_helper.rb', line 29 def (io, encoding: Encoding::UTF_8) bytes_for_length = read_from io, 4 length = bin2int(bytes_for_length) return [] if length == 0 bytes = read_from(io, length) ptr = 0 result = [] while ptr < bytes.length type_byte = bytes[ptr] ptr += 1 case type_byte when TYPE_INT result << bin2int(bytes[ptr...ptr + 4]) ptr += 4 when TYPE_STRING length = bin2int(bytes[ptr...ptr + 4]) ptr += 4 next result << '' if length == 0 result << bytes[ptr...ptr + length].force_encoding(encoding) ptr += length end end result end |
#write_message(io, *items) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/arsenicum/core/io_helper.rb', line 5 def (io, *items) buffer = StringIO.new buffer.set_encoding Encoding::BINARY buffer.seek 4# length of integer. items.each do |item| case item when Fixnum buffer.write TYPE_INT buffer.write [item].pack('N') when String, Symbol item = item.to_s.force_encoding Encoding::BINARY buffer.write TYPE_STRING length = item.length buffer.write int2bin(length) buffer.write item end end buffer.seek 0 buffer.write int2bin(buffer.length - 4) io.write buffer.string end |