Class: MTProto::Transport::AbridgedPacketCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/transport/abridged_packet_codec.rb

Constant Summary collapse

TAG =
"\xef".b
OBFUSCATE_TAG =
"\xef\xef\xef\xef".b

Instance Method Summary collapse

Instance Method Details

#decode_packet(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mtproto/transport/abridged_packet_codec.rb', line 21

def decode_packet(data)
  length = data.unpack1('C')
  offset = 1

  if length >= 127
    length = (data[1, 3] + "\x00").unpack1('L<')
    offset = 4
  end

  actual_length = length << 2
  data[offset, actual_length]
end

#encode_packet(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mtproto/transport/abridged_packet_codec.rb', line 9

def encode_packet(data)
  length = data.bytesize >> 2

  length_prefix = if length < 127
    [length].pack('C')
  else
    "\x7f".b + [length].pack('L<')[0, 3]
  end

  length_prefix + data
end