HelPHP

Frame
in package

RFC 6455 frame codec: the byte layer, with no socket and no state.

One TCP read is not one frame and one frame is not one message: a message of 10 kB arrives in as many reads as the kernel decides, and a client may split it in fragments. So decode() consumes one frame from a caller-owned buffer and leaves the remainder in place; the caller loops, and reassembles fragments (Connection does, because it also has to answer the control frames interleaved between them).

Client frames must be masked, server frames must not (RFC 6455 §5.1): the $require_mask argument is what a client-side reader flips when it decodes what the server sends back.

Every violation throws a Protocol_exception carrying the close code to send; nothing here closes a socket, writes a log or knows about Config.

Tags
see

Connection, Handshake

Table of Contents

Constants

BINARY  : mixed = 0x2
Binary data frame.
CLOSE  : mixed = 0x8
Connection close control frame.
CONTINUATION  : mixed = 0x0
Data frame continuing the message opened by the previous frame.
PING  : mixed = 0x9
Heartbeat request; the peer answers with PONG.
PONG  : mixed = 0xa
Heartbeat answer.
TEXT  : mixed = 0x1
UTF-8 text data frame.

Methods

close_code()  : int
Reads the two-byte status code of a CLOSE payload.
close_reason()  : string
Reads the reason of a CLOSE payload.
decode()  : array<string|int, mixed>|null
Reads one frame from the head of $buffer.
encode()  : string
Builds one frame, unmasked: this is the server-to-client direction.
encode_close()  : string
Builds a close control frame. An empty payload is legal and means "no status"; a code always comes with its 2-byte big-endian form.
encode_masked()  : string
Builds a masked client frame. A client must mask what it sends (RFC 6455 §5.3); the mask is random unless one is given, which is what the tests need to replay the sample frame of the specification.

Constants

BINARY

Binary data frame.

public mixed BINARY = 0x2

CLOSE

Connection close control frame.

public mixed CLOSE = 0x8

CONTINUATION

Data frame continuing the message opened by the previous frame.

public mixed CONTINUATION = 0x0

PING

Heartbeat request; the peer answers with PONG.

public mixed PING = 0x9

PONG

Heartbeat answer.

public mixed PONG = 0xa

TEXT

UTF-8 text data frame.

public mixed TEXT = 0x1

Methods

close_code()

Reads the two-byte status code of a CLOSE payload.

public static close_code(string $payload) : int
Parameters
$payload : string

Close frame payload, with or without a reason.

Return values
int —

The code, or 0 when the peer sent none.

close_reason()

Reads the reason of a CLOSE payload.

public static close_reason(string $payload) : string
Parameters
$payload : string

Close frame payload.

Return values
string —

Reason, empty when absent.

decode()

Reads one frame from the head of $buffer.

public static decode(string &$buffer, int $max_payload[, bool $require_mask = true ]) : array<string|int, mixed>|null

Returns null while the frame is incomplete, and consumes exactly the bytes of the frame it returns — so a burst of two frames is two calls.

Parameters
$buffer : string

Read buffer, consumed by reference.

$max_payload : int

Largest payload accepted, in bytes; a bigger announced length is refused before buffering it.

$require_mask : bool = true

True for client frames, false when reading a server frame (the internal PHP client).

Tags
throws
Protocol_exception

1002 protocol violation, 1009 too large.

Return values
array<string|int, mixed>|null —

['fin'=>bool, 'opcode'=>int, 'payload'=>string]

encode()

Builds one frame, unmasked: this is the server-to-client direction.

public static encode(string $payload[, int $opcode = self::TEXT ][, bool $fin = true ]) : string

The two extended length forms are the RFC ones (16-bit for 126..65535, 64-bit above), and the 64-bit form is packed with 'J' because 'N' would silently truncate a length beyond 4 GB.

Parameters
$payload : string

Bytes to send.

$opcode : int = self::TEXT

One of the class constants.

$fin : bool = true

False to announce that a fragmented message follows.

Return values
string —

The frame bytes.

encode_close()

Builds a close control frame. An empty payload is legal and means "no status"; a code always comes with its 2-byte big-endian form.

public static encode_close([int $code = 1000 ][, string $reason = '' ]) : string
Parameters
$code : int = 1000

RFC 6455 close code, 0 for a bare close.

$reason : string = ''

Short UTF-8 reason, truncated to the 123 bytes a control frame leaves available.

Return values
string —

The frame bytes.

encode_masked()

Builds a masked client frame. A client must mask what it sends (RFC 6455 §5.3); the mask is random unless one is given, which is what the tests need to replay the sample frame of the specification.

public static encode_masked(string $payload[, int $opcode = self::TEXT ][, string $mask = '' ][, bool $fin = true ]) : string
Parameters
$payload : string

Bytes to send.

$opcode : int = self::TEXT

One of the class constants.

$mask : string = ''

Four bytes, or '' for a random mask.

$fin : bool = true

False to announce a fragmented message.

Return values
string —

The frame bytes.

On this page

Search results