Connection
in package
One connected client: its socket, its read buffer, its write queue and what the handshake told us about it.
This is where the bytes become messages. It owns the two pieces of state the frame codec cannot: the reassembly of a fragmented message, and the queue of outgoing bytes. Both are per connection, which is what makes a slow client harmless: writing never blocks, it appends, and the server flushes what the socket accepts.
Control frames are answered here, as the RFC requires, even in the middle of a fragmented message: a PING gets its PONG queued immediately, a PONG refreshes the heartbeat, a CLOSE is echoed with the same code and marks the connection finished once the echo is out.
The identity fields ($oid, $topics) are left to the bridge of the caller: this class knows nothing about helPHP users.
Tags
Table of Contents
Properties
- $buffer : string
- $close_code : int
- $close_reason : string
- $closed : bool
- $closing : bool
- $headers : array<string, string>
- $id : int
- $ip : string
- $last_activity : int
- $last_pong : int
- $oid : string
- $origin : string
- $out : string
- $path : string
- $socket : resource
- $tokens : float
- $tokens_at : float
- $ua : string
- $user : array<string|int, mixed>
- $user_checked : int
Methods
- __construct() : mixed
- allow() : bool
- Spends one message from the rate budget of this connection.
- close() : void
- Queues a close frame and marks the connection as closing.
- feed() : void
- Adds bytes read from the socket to the read buffer.
- flush() : bool
- Writes as much of the queue as the socket accepts, without blocking.
- is_finished() : bool
- messages() : array<int, array{opcode: int, payload: string}>
- Decodes every complete frame of the read buffer and returns the messages.
- queue() : void
- Queues raw bytes for the socket.
- send() : void
- Queues a text or binary message.
- wants_write() : bool
Properties
$buffer
public
string
$buffer
= ''
Bytes received and not yet decoded as a full frame.
$close_code
public
int
$close_code
= 0
Close code sent or received, 0 while open.
$close_reason
public
string
$close_reason
= ''
Close reason, for the log and the onClose callback.
$closed
public
bool
$closed
= false
True once the peer close frame has been answered and flushed.
$closing
public
bool
$closing
= false
True once a close frame has been queued.
$headers
public
array<string, string>
$headers
= array()
Handshake headers, lowercased names.
$id
public
int
$id
Connection number, unique for the life of the server.
$ip
public
string
$ip
= ''
Client address, as resolved by the caller.
$last_activity
public
int
$last_activity
= 0
Timestamp of the last bytes read.
$last_pong
public
int
$last_pong
= 0
Timestamp of the last PONG received.
$oid
public
string
$oid
= ''
OID of the client once it has authenticated, set by the bridge.
$origin
public
string
$origin
= ''
Origin header of the handshake, for the log.
$out
public
string
$out
= ''
Bytes waiting for the socket to accept them.
$path
public
string
$path
= ''
Request target of the handshake, query string included.
$socket
public
resource
$socket
The client stream.
$tokens
public
float
$tokens
= 0.0
Tokens left in the message budget of this connection.
$tokens_at
public
float
$tokens_at
= 0.0
Time the budget was last refilled, in seconds with microseconds.
$ua
public
string
$ua
= ''
Client user agent, needed to rebuild an OID check.
$user
public
array<string|int, mixed>
$user
= array('id' => null, 'login' => '', 'admin' => false, 'groups' => array())
Identity of the client, set by the bridge after a successful OID check: ['id'=>int|null, 'login'=>string, 'admin'=>bool, 'groups'=>int[]]. A connection that never authenticated keeps id null, whatever it asks for.
$user_checked
public
int
$user_checked
= 0
When the identity was last refreshed from the database, so a heartbeat does not reload it every time.
Methods
__construct()
public
__construct(int $id, resource $socket, array<string|int, mixed> $handshake, string $ip, int $max_payload) : mixed
Parameters
- $id : int
-
Connection number.
- $socket : resource
-
Client stream, non-blocking.
- $handshake : array<string|int, mixed>
-
Result of Handshake::verify().
- $ip : string
-
Client address resolved by the server.
- $max_payload : int
-
Largest message accepted, in bytes.
allow()
Spends one message from the rate budget of this connection.
public
allow(float $now, float $rate, float $burst) : bool
A token bucket, not a counter per second: a client that sends a burst of twenty messages at once is normal (a page posting several fields), a client that keeps sending two hundred per second is not. The bucket absorbs the first burst and refuses the rest, and it refills with time, so a greedy client recovers on its own instead of staying banned.
The clock is passed in: the caller already has a timestamp, and a test needs to move time without sleeping.
Parameters
- $now : float
-
Current time, in seconds with microseconds.
- $rate : float
-
Messages allowed per second.
- $burst : float
-
Size of the bucket.
Return values
bool —True when the message may be processed.
close()
Queues a close frame and marks the connection as closing.
public
close([int $code = 1000 ][, string $reason = '' ]) : void
Idempotent: a second call (peer close arriving after a timeout close) does not queue a second frame.
Parameters
- $code : int = 1000
-
RFC 6455 close code.
- $reason : string = ''
-
Short reason.
feed()
Adds bytes read from the socket to the read buffer.
public
feed(string $chunk) : void
Parameters
- $chunk : string
-
Bytes just received.
flush()
Writes as much of the queue as the socket accepts, without blocking.
public
flush() : bool
Return values
bool —True when the queue is empty.
is_finished()
public
is_finished() : bool
Return values
bool —True when the connection must be removed from the loop.
messages()
Decodes every complete frame of the read buffer and returns the messages.
public
messages() : array<int, array{opcode: int, payload: string}>
A frame is consumed by Frame::decode(), which leaves the rest of the buffer in place: this is what makes a burst of messages, or a single message split across reads, work without any assumption on the network.
Tags
Return values
array<int, array{opcode: int, payload: string}> —Complete messages, in order.
queue()
Queues raw bytes for the socket.
public
queue(string $bytes) : void
Parameters
- $bytes : string
-
Frame bytes.
send()
Queues a text or binary message.
public
send(string $payload[, int $opcode = Frame::TEXT ]) : void
Parameters
- $payload : string
-
Message body.
- $opcode : int = Frame::TEXT
-
Frame::TEXT or Frame::BINARY.
wants_write()
public
wants_write() : bool
Return values
bool —True when bytes are waiting to be written.