Module: Clacky::Channel::Adapters::Feishu::WSClient::ProtoFrame

Defined in:
lib/clacky/server/channel/adapters/feishu/ws_client.rb

Overview

Minimal protobuf encoder/decoder for pbbp2.Frame. Fields: 1=SeqID(uint64), 2=LogID(uint64), 3=service(int32), 4=method(int32), 5=headers(repeated msg1=key,2=value), 8=payload(bytes)

Class Method Summary collapse

Class Method Details

.decode(buf) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 320

def self.decode(buf)
  buf = buf.b
  pos = 0
  result = { headers: {}, payload: "".b }

  while pos < buf.bytesize
    tag_byte, pos = read_varint(buf, pos)
    field_number = tag_byte >> 3
    wire_type = tag_byte & 0x7

    case wire_type
    when 0 # varint
      val, pos = read_varint(buf, pos)
      case field_number
      when 1 then result[:seq_id] = val
      when 2 then result[:log_id] = val
      when 3 then result[:service] = val
      when 4 then result[:method] = val
      end
    when 2 # length-delimited
      len, pos = read_varint(buf, pos)
      bytes = buf.byteslice(pos, len)
      pos += len
      case field_number
      when 5 # header entry
        k, v = decode_header(bytes)
        result[:headers][k] = v if k
      when 7 then result[:payload_type] = bytes.force_encoding("UTF-8")
      when 8 then result[:payload] = bytes
      end
    else
      break # unknown wire type, stop parsing
    end
  end

  result
end

.decode_header(buf) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 358

def self.decode_header(buf)
  buf = buf.b
  pos = 0
  key = nil
  val = nil
  while pos < buf.bytesize
    tag_byte, pos = read_varint(buf, pos)
    field_number = tag_byte >> 3
    wire_type = tag_byte & 0x7
    if wire_type == 2
      len, pos = read_varint(buf, pos)
      bytes = buf.byteslice(pos, len)
      pos += len
      case field_number
      when 1 then key = bytes.force_encoding("UTF-8")
      when 2 then val = bytes.force_encoding("UTF-8")
      end
    else
      break
    end
  end
  [key, val]
end

.encode(frame) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 303

def self.encode(frame)
  buf = "".b
  buf << encode_varint(1, frame[:seq_id] || 0)
  buf << encode_varint(2, frame[:log_id] || 0)
  buf << encode_varint(3, frame[:service] || 0)
  buf << encode_varint(4, frame[:method] || 0)
  (frame[:headers] || {}).each do |k, v|
    header_bytes = encode_string(1, k.to_s) + encode_string(2, v.to_s)
    buf << encode_length_delimited(5, header_bytes)
  end
  if frame[:payload]
    payload_bytes = frame[:payload].respond_to?(:b) ? frame[:payload].b : frame[:payload].to_s.b
    buf << encode_length_delimited(8, payload_bytes)
  end
  buf
end

.encode_length_delimited(field_number, bytes) ⇒ Object



418
419
420
421
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 418

def self.encode_length_delimited(field_number, bytes)
  tag = (field_number << 3) | 2  # wire type 2
  encode_raw_varint(tag) + encode_raw_varint(bytes.bytesize) + bytes
end

.encode_raw_varint(value) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 401

def self.encode_raw_varint(value)
  bytes = "".b
  loop do
    byte = value & 0x7F
    value >>= 7
    byte |= 0x80 if value > 0
    bytes << byte
    break if value == 0
  end
  bytes
end

.encode_string(field_number, str) ⇒ Object



413
414
415
416
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 413

def self.encode_string(field_number, str)
  bytes = str.encode("UTF-8").b
  encode_length_delimited(field_number, bytes)
end

.encode_varint(field_number, value) ⇒ Object



396
397
398
399
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 396

def self.encode_varint(field_number, value)
  tag = (field_number << 3) | 0  # wire type 0
  encode_raw_varint(tag) + encode_raw_varint(value)
end

.read_varint(buf, pos) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 382

def self.read_varint(buf, pos)
  result = 0
  shift = 0
  loop do
    byte = buf.getbyte(pos)
    raise "unexpected end of buffer at pos #{pos}" if byte.nil?
    pos += 1
    result |= (byte & 0x7F) << shift
    break unless byte & 0x80 != 0
    shift += 7
  end
  [result, pos]
end