Skip to content

Commit e1d9c9c

Browse files
erts: Document packet byte order
Document three-byte headers and explicit big, little, and native tuple forms for open_port and decode_packet. Keep scalar packet widths as the backward-compatible big-endian shorthand.
1 parent b5c16d0 commit e1d9c9c

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

erts/preloaded/src/erlang.erl

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,10 +1814,15 @@ returned.
18141814
- **`raw | 0`** - No packet handling is done. The entire binary is returned
18151815
unless it is empty.
18161816

1817-
- **`1 | 2 | 4`** - Packets consist of a header specifying the number of bytes
1818-
in the packet, followed by that number of bytes. The length of the header can
1819-
be one, two, or four bytes; the order of the bytes is big-endian. The header
1820-
is stripped off when the packet is returned.
1817+
- **`1 | 2 | 3 | 4`** - Packets consist of a header specifying the number of
1818+
bytes in the packet, followed by that number of bytes. The length of the
1819+
header can be one, two, three, or four bytes; the order of the bytes is
1820+
big-endian. The header is stripped off when the packet is returned.
1821+
1822+
- **`{N, Endian}`** - Uses the same framing with a two-, three-, or four-byte
1823+
header, in the byte order specified by `Endian`, which can be `big`, `little`,
1824+
or `native`. `{N, big}` is equivalent to `N`; `native` uses the native byte
1825+
order of the runtime system.
18211826

18221827
- **`line`** - A packet is a line-terminated by a delimiter byte, default is the
18231828
latin-1 newline character. The delimiter byte is included in the returned
@@ -1890,8 +1895,10 @@ Options:
18901895
{ok, Packet, Rest} |
18911896
{more, Length} |
18921897
{error, Reason} when
1893-
Type :: 'raw' | 0 | 1 | 2 | 4 | 'asn1' | 'cdr' | 'sunrm' | 'fcgi'
1894-
| 'tpkt' | 'line' | 'http' | 'http_bin' | 'httph' | 'httph_bin',
1898+
Type :: 'raw' | 0 | 1 | 2 | 3 | 4
1899+
| {N :: 2 | 3 | 4, Endian :: big | little | native}
1900+
| 'asn1' | 'cdr' | 'sunrm' | 'fcgi' | 'tpkt' | 'line'
1901+
| 'http' | 'http_bin' | 'httph' | 'httph_bin',
18951902
Bin :: binary(),
18961903
Options :: [Opt],
18971904
Opt :: {packet_size, non_neg_integer()}
@@ -7751,9 +7758,15 @@ encoding. For details, see the module `m:file`, the function
77517758
follows:
77527759
77537760
- **`{packet, N}`** - Messages are preceded by their length, sent in `N` bytes,
7754-
with the most significant byte first. The valid values for `N` are 1, 2,
7761+
with the most significant byte first. The valid values for `N` are 1, 2, 3,
77557762
and 4.
77567763
7764+
- **`{packet, {N, Endian}}`** - Uses the same framing with a two-, three-, or
7765+
four-byte header, in the byte order specified by `Endian`, which can be
7766+
`big`, `little`, or `native`. This setting applies to messages in both
7767+
directions. `{packet, {N, big}}` is equivalent to `{packet, N}`; `native`
7768+
uses the native byte order of the runtime system.
7769+
77577770
- **`stream`** - Output messages are sent without packet lengths. A user-defined
77587771
protocol must be used between the Erlang process and the external object.
77597772
@@ -7768,7 +7781,8 @@ follows:
77687781
following a newline sequence, the last line is also delivered with `Flag` set
77697782
to `noeol`. Otherwise lines are delivered with `Flag` set to `eol`.
77707783
7771-
The `{packet, N}` and `{line, L}` settings are mutually exclusive.
7784+
The `{packet, N}` and `{packet, {N, Endian}}` settings are mutually exclusive
7785+
with `{line, L}`.
77727786
77737787
- **`{cd, Dir}`** - Only valid for `{spawn, Command}` and
77747788
`{spawn_executable, FileName}`. The external program starts using `Dir` as its
@@ -7966,7 +7980,9 @@ by passing command-line flag [`+Q`](erl_cmd.md#max_ports) to [erl](erl_cmd.md).
79667980
{spawn_executable, FileName :: file:name_all() } |
79677981
{fd, In :: non_neg_integer(), Out :: non_neg_integer()},
79687982
PortSettings :: [Opt],
7969-
Opt :: {packet, N :: 1 | 2 | 4}
7983+
Opt :: {packet, N :: 1 | 2 | 3 | 4}
7984+
| {packet, {N :: 2 | 3 | 4,
7985+
Endian :: big | little | native}}
79707986
| stream
79717987
| {line, L :: non_neg_integer()}
79727988
| {cd, Dir :: string() | binary()}

system/doc/reference_manual/ports.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ is found, that driver is started.
7676

7777
`PortSettings` is a list of settings (options) for the port. The list typically
7878
contains at least a tuple `{packet,N}`, which specifies that data sent between
79-
the port and the external program are preceded by an N-byte length indicator.
80-
Valid values for N are 1, 2, or 4. If binaries are to be used instead of lists
81-
of bytes, the option `binary` must be included.
79+
the port and the external program are preceded by an N-byte length indicator in
80+
big-endian byte order. Valid values for N are 1, 2, 3, or 4. For two-, three-,
81+
and four-byte headers, `{packet,{N,Endian}}` can be used to select `big`,
82+
`little`, or the platform-native byte order. If binaries are to be used instead
83+
of lists of bytes, the option `binary` must be included.
8284

8385
The port owner `Pid` can communicate with the port `Port` by sending and
8486
receiving messages. (In fact, any process can send messages to the port, but

0 commit comments

Comments
 (0)