Class: Ruflet::WireCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/ruflet/server/wire_codec.rb

Defined Under Namespace

Classes: ByteReader

Class Method Summary collapse

Class Method Details

.pack(value) ⇒ Object



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
55
56
57
58
59
# File 'lib/ruflet/server/wire_codec.rb', line 30

def pack(value)
  case value
  when Ruflet::Protocol::DateTimeValue
    pack_extension(1, value)
  when Ruflet::Protocol::TimeOfDayValue
    pack_extension(2, value)
  when Ruflet::Protocol::DurationValue
    pack_extension(3, value)
  when NilClass
    "\xc0".b
  when TrueClass
    "\xc3".b
  when FalseClass
    "\xc2".b
  when Integer
    pack_integer(value)
  when Float
    "\xcb".b + [value].pack("G")
  when String
    binary_string?(value) ? pack_binary(value) : pack_string(value)
  when Symbol
    pack_string(value.to_s)
  when Array
    pack_array(value)
  when Hash
    pack_map(value)
  else
    pack_string(value.to_s)
  end
end

.trace(direction, bytes) ⇒ Object

Transport tracing is opt-in. Even compact summaries decode every frame and write to stdout, so enabling them by default would distort the renderer performance they are intended to diagnose.

RUFLET_PROTOCOL_TRACE=summary  compact frame metadata
RUFLET_PROTOCOL_TRACE=1        exact bytes and decoded value
RUFLET_PROTOCOL_TRACE=0        disabled (default)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruflet/server/wire_codec.rb', line 13

def trace(direction, bytes)
  mode = ENV.fetch("RUFLET_PROTOCOL_TRACE", "0")
  return if mode == "0"

  raw = bytes.to_s.b
  decoded = unpack(raw)
  action = decoded.is_a?(Array) ? decoded[0] : nil
  prefix = "[RUFLET_PROTOCOL] t=#{format("%.6f", Process.clock_gettime(Process::CLOCK_MONOTONIC))} #{direction} bytes=#{raw.bytesize} action=#{action.inspect}"
  if mode == "1" || mode == "full"
    puts "#{prefix} hex=#{raw.unpack("H*").first} data=#{decoded.inspect}"
  else
    puts "#{prefix} #{trace_summary(action, decoded.is_a?(Array) ? decoded[1] : nil)}".rstrip
  end
rescue StandardError => error
  puts "[RUFLET_PROTOCOL] t=#{format("%.6f", Process.clock_gettime(Process::CLOCK_MONOTONIC))} #{direction} bytes=#{raw&.bytesize || 0} decode_error=#{error.class}: #{error.message}"
end

.unpack(bytes) ⇒ Object



61
62
63
64
# File 'lib/ruflet/server/wire_codec.rb', line 61

def unpack(bytes)
  reader = ByteReader.new(bytes)
  read_value(reader)
end