Module: Mycel::Framing
- Defined in:
- lib/mycel.rb
Overview
Layer 1: Framing — length-prefixed chunks (up to 1,073,741,823 bytes)
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.read(io) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mycel.rb', line 83 def self.read(io) raise EOFError unless (t1 = io.read(1)) if ((w = ((t2 = t1.unpack('C')[0]) & 0xC0) >> 6) == 0) t3 = 0 else raise EOFError unless (t1 = io.read(w)) t3 = t1.rjust(4, "\x00").unpack('N')[0] end io.read(t3 | (t2 & 0x3F) << (w * 8)) end |
.write(io, msg) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mycel.rb', line 94 def self.write(io, msg) begin msg = msg.to_s.force_encoding("ASCII-8BIT") rescue NoMethodError msg = msg.to_s end raise(ArgumentError) if (l = msg.length) >= 0xC0000000 w = ((l & 0x3FFFC000 == 0) ? ((l & 0x00003FC0 == 0) ? 0 : 1) : ((l & 0x3FC00000 == 0) ? 2 : 3)) h = Array.new w.downto(0) do |i| h.push 0xff & (l >> (i * 8)) end h[0] = h[0] | w << 6 io.print h.pack('C*'), msg end |