Longbridge.Protocol.Header (longbridge v0.1.0)

Copy Markdown View Source

Binary packet header for the Longbridge v1 protocol.

The header layout differs by packet type:

Request (11 bytes):

type:4|ver:1|gzip:1|resv:2 | cmd_code:8 | request_id:32 | timeout:16 | body_len:24

Response (10 bytes):

type:4|ver:1|gzip:1|resv:2 | cmd_code:8 | request_id:32 | status_code:8 | body_len:24

Push (5 bytes):

type:4|ver:1|gzip:1|resv:2 | cmd_code:8 | body_len:24

After the body, optional nonce (8 bytes) and signature (16 bytes) when verify is enabled.

Summary

Functions

Packs a header struct into its binary representation. Returns iodata.

Unpacks binary data into a header struct. Returns {:ok, header, remaining_binary} or {:error, reason}.

Extract optional nonce and signature from data following the body. Only called when verify is true. Returns {nonce, signature, remaining}.

Types

packet_type()

@type packet_type() :: :request | :response | :push

t()

@type t() :: %Longbridge.Protocol.Header{
  body_length: non_neg_integer(),
  cmd_code: non_neg_integer(),
  gzip: boolean(),
  nonce: non_neg_integer() | nil,
  request_id: non_neg_integer() | nil,
  signature: binary() | nil,
  status_code: non_neg_integer() | nil,
  timeout: non_neg_integer() | nil,
  type: packet_type(),
  verify: boolean()
}

Functions

pack(h)

@spec pack(t()) :: [binary(), ...]

Packs a header struct into its binary representation. Returns iodata.

unpack(data)

@spec unpack(binary()) :: {:ok, t(), binary()} | {:error, term()}

Unpacks binary data into a header struct. Returns {:ok, header, remaining_binary} or {:error, reason}.

unpack_auth_tail(data)

Extract optional nonce and signature from data following the body. Only called when verify is true. Returns {nonce, signature, remaining}.