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 msg{1=key,2=value}),
8=payload(bytes)
Class Method Summary collapse
- .decode(buf) ⇒ Object
- .decode_header(buf) ⇒ Object
- .encode(frame) ⇒ Object
- .encode_length_delimited(field_number, bytes) ⇒ Object
- .encode_raw_varint(value) ⇒ Object
- .encode_string(field_number, str) ⇒ Object
- .encode_varint(field_number, value) ⇒ Object
- .read_varint(buf, pos) ⇒ Object
Class Method Details
.decode(buf) ⇒ Object
314 315 316 317 318 319 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 |
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 314 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
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 352 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
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 297 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
412 413 414 415 |
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 412 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
395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 395 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
407 408 409 410 |
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 407 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
390 391 392 393 |
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 390 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
376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/clacky/server/channel/adapters/feishu/ws_client.rb', line 376 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 |