Module: Protocol::SP::Codec::Greeting

Defined in:
lib/protocol/sp/codec/greeting.rb

Overview

SP/TCP greeting encode/decode.

The greeting is exactly 8 bytes (per nng ‘src/sp/transport/tcp/tcp.c`, `tcptran_pipe_nego_cb` and `tcptran_pipe_send_start`):

Offset  Bytes  Field
0       1      0x00
1       1      'S' (0x53)
2       1      'P' (0x50)
3       1      0x00
4-5     2      protocol id (u16, big-endian)
6-7     2      reserved (must be 0x00 0x00)

Constant Summary collapse

SIZE =
8
SIGNATURE =
"\x00SP\x00".b.freeze

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ Integer

Decodes an SP/TCP greeting.

Parameters:

  • data (String)

    8-byte binary greeting

Returns:

  • (Integer)

    peer protocol id

Raises:

  • (Error)

    on invalid greeting



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/protocol/sp/codec/greeting.rb', line 38

def self.decode(data)
  raise Error, "greeting too short (#{data.bytesize} bytes)" if data.bytesize < SIZE

  data = data.b
  unless data.byteslice(0, 4) == SIGNATURE
    raise Error, "invalid SP greeting signature"
  end
  unless data.getbyte(6) == 0 && data.getbyte(7) == 0
    raise Error, "invalid SP greeting reserved bytes"
  end

  data.byteslice(4, 2).unpack1("n")
end

.encode(protocol:) ⇒ String

Encodes an SP/TCP greeting.

Parameters:

  • protocol (Integer)

    our protocol id (e.g. Protocols::PUSH_V0)

Returns:

  • (String)

    8-byte binary greeting



28
29
30
# File 'lib/protocol/sp/codec/greeting.rb', line 28

def self.encode(protocol:)
  SIGNATURE + [protocol].pack("n") + "\x00\x00".b
end