YAACC - UPNP Client and Server
Loading...
Searching...
No Matches
de.yaacc.snapcast.protocol.SnapcastConnection Class Reference

The phone as a client of one snapserver: the TCP connection of the binary protocol (default port 1704). More...

Inheritance diagram for de.yaacc.snapcast.protocol.SnapcastConnection:
Inheritance graph
Collaboration diagram for de.yaacc.snapcast.protocol.SnapcastConnection:
Collaboration graph

Classes

class  Config
 
interface  Listener
 Receives what happens, on the listener executor. More...
 
class  Loss
 A typed reason and, for the user, a short text of ours or the server's (display text only, never logged).
 
enum  State
 The state of the connection. More...
 

Public Member Functions

 SnapcastConnection (SnapcastTransport transport, HelloMessage hello, Config config, MicroClock clock, RetryScheduler scheduler, Executor listenerExecutor)
 
void addListener (Listener listener)
 
void removeListener (Listener listener)
 
State getState ()
 
ClockOffsetFilter getOffsetFilter ()
 The clock estimate of this connection; the same object for the life of the connection (reset on every attempt).
 
long getDroppedChunks ()
 Chunks dropped because the listener was too slow (the bounds on the waiting chunks).
 
long getDroppedBeforeHeader ()
 Chunks that arrived before the codec header and were not delivered.
 
long getUnsolicitedTime ()
 Time answers that did not belong to a request of ours.
 
long getRejectedTimeSamples ()
 Time answers whose network time was longer than our own clock measured.
 
ConnectionStats stats ()
 
void start ()
 Starts connecting; does nothing unless the connection was not started yet.
 
void retry ()
 Connects now when the connection waits for a retry or gave up (the failure count starts again).
 
void close ()
 Ends the connection for good.
 
boolean sendClientInfo (int volume, boolean muted)
 Tells the server the volume of this client (0 to 100) and whether it is muted.
 

Static Public Member Functions

static SnapcastConnection forAddress (InetAddress address, int port, HelloMessage hello)
 A connection to this address and port (default port {@value DEFAULT_PORT}) with the default settings and own threads.
 

Static Public Attributes

static final int DEFAULT_PORT = 1704
 The default port of the stream protocol.
 

Detailed Description

The phone as a client of one snapserver: the TCP connection of the binary protocol (default port 1704).

Whom it talks to. To the endpoint of the SnapcastTransport it was made with, which the caller (the user or the discovery gate) chose. Nothing in a message can name another host or port, and nothing is followed. No credentials are sent (the Hello has no Auth); a server that wants some answers with an Error 401 or 403 and is given up with the reason LossReason#UNAUTHORIZED.

Flow. After the connect the Hello goes out, the server answers with the settings and the codec header, and chunks follow. A chunk that arrives before the codec header is dropped (counted), not delivered. When the settings arrive the time sync starts: a burst of timeSyncBurstCount (default 16, the size of the filter's window) Time requests timeSyncBurstIntervalMs (100 ms) apart, then one every timeSyncIntervalMs (1 s, the interval of the reference client); the answers feed the ClockOffsetFilter. A message must be complete messageDeadlineMs (10 s) after its first byte, the settings must be there handshakeTimeoutMs (10 s) after the Hello (the header may be missing for good: a server with no stream sends none, and the connection stays until the stream comes), and a connection that delivers nothing at all for idleTimeoutMs (10 s) is dropped.

Threads. One reader thread per connection attempt reads and decodes, and decides when a Time request is due; one writer thread is the only code that writes to the socket (so a server that stops reading holds nobody: a full queue of 32 ends the connection). The reader counts the frames of the server and drops the connection above maxFramesPerSecond (2000) of the injected clock. Neither thread calls a listener. Listeners run on a serial executor (an own thread unless the caller gives one), in the order of the stream. What waits for a slow listener is bounded for every kind of event: chunks in count and bytes (a chunk beyond the bounds is dropped and counted), server settings and offset updates keep only the latest value, and codec headers (never replaced or dropped silently, the order to the chunks matters) are limited to maxPendingHeaders (8); more waiting than that is a flood and ends the connection as PROTOCOL_ERROR. The outage events onLost and onGaveUp are limited by the reconnect rules (one report per outage, five failures in a row). A listener that throws is skipped for that call.

Reconnect. As in the control client (SnapcastRpc): a lost connection is retried after 1 s, 2 s, 4 s ... at most 30 s; after 5 failures in a row it stops (State#GAVE_UP) until retry(); a connection that held for stableConnectionMs (10 s) after a complete handshake starts the count again. Listener#onLost is called once per outage (not again for every failed retry; a complete handshake ends the outage) and Listener#onGaveUp once when the retries stop. A permanent reason (authorization, a codec that is not on the allow-list) stops at once, because a retry would meet the same answer.

close() ends the thread and the socket, also while the thread is blocked in a read, and nothing is reported afterwards.

Constructor & Destructor Documentation

◆ SnapcastConnection()

de.yaacc.snapcast.protocol.SnapcastConnection.SnapcastConnection ( SnapcastTransport transport,
HelloMessage hello,
Config config,
MicroClock clock,
RetryScheduler scheduler,
Executor listenerExecutor )
Parameters
transportopens the connection to the one endpoint the caller chose (never one from the server)
hellowhat to tell the server about this client
configthe limits and timings; copied
clockthe client clock of the time sync; null: the monotonic clock
schedulerruns the reconnect timers; null: an own single-thread scheduler
listenerExecutorruns the listeners, one task at a time in order; null: an own thread

Referenced by de.yaacc.snapcast.protocol.SnapcastConnection.forAddress().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ close()

void de.yaacc.snapcast.protocol.SnapcastConnection.close ( )

Ends the connection for good.

Not for the main thread: it waits up to 2 s for the reader thread, which can be held that long by a connect that is in progress.

◆ forAddress()

static SnapcastConnection de.yaacc.snapcast.protocol.SnapcastConnection.forAddress ( InetAddress address,
int port,
HelloMessage hello )
static

A connection to this address and port (default port {@value DEFAULT_PORT}) with the default settings and own threads.

Pass an address that is already resolved (the one the control connection uses, or one resolved once by the caller), never a name: a name is looked up again at every reconnect, which can change its answer and holds a thread in the resolver that close() can not end. The wildcard, multicast addresses and bad ports are refused with an IllegalArgumentException.

Here is the call graph for this function:

◆ retry()

void de.yaacc.snapcast.protocol.SnapcastConnection.retry ( )

Connects now when the connection waits for a retry or gave up (the failure count starts again).

Do not call it in an automatic loop: it removes the ceiling of the reconnects.

◆ sendClientInfo()

boolean de.yaacc.snapcast.protocol.SnapcastConnection.sendClientInfo ( int volume,
boolean muted )

Tells the server the volume of this client (0 to 100) and whether it is muted.

It never blocks: the message is handed to the writer thread of the connection, and when an earlier one has not been written yet it is replaced (only the latest values matter).

Returns
false when there is no connection or the writer's queue is full
Here is the call graph for this function:

The documentation for this class was generated from the following file: