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

One TCP connection to one FCast receiver, protocol v3 (docs/tech.md, "FCast"; design.md, section 1). More...

Inheritance diagram for de.yaacc.fcast.protocol.FCastConnection:
Inheritance graph
Collaboration diagram for de.yaacc.fcast.protocol.FCastConnection:
Collaboration graph

Classes

enum  CloseReason
 Why a connection ended. More...
 
class  Config
 The times and sizes, injectable so tests do not wait.
 
interface  Listener
 Receives what the connection learns, in order, on one thread. More...
 

Public Member Functions

 FCastConnection (InetSocketAddress target, Listener listener, Config config, MonotonicClock clock, String senderName, String appName, String appVersion)
 
void connect () throws IOException
 Connects and starts the threads; the connection is usable when this returns.
 
InetSocketAddress getTarget ()
 The receiver's address.
 
int getPeerVersion ()
 The version the receiver announced (1 or more), 0 while it has not said.
 
int getNegotiatedVersion ()
 The version this connection speaks: the smaller of the receiver's and FCastMessages#MAX_VERSION; 3 until the receiver said.
 
FCastPin.Source getPinSource ()
 Where the pinned key came from, or null if the connection is not secured.
 
boolean isSecure ()
 True if this connection runs inside TLS 1.3 (protocol v4, pinned key).
 
boolean isClosed ()
 
boolean send (FCastOpcode opcode, String body) throws FCastProtocolException
 Queues a message for the receiver.
 
boolean sendV4 (byte[] message) throws FCastProtocolException
 Queues a message that only version 4 has (tracks, subtitles).
 
void close ()
 Closes the connection; the listener gets onClosed(CLOSED_BY_US).
 
boolean awaitTermination (long millis) throws InterruptedException
 For tests and for an orderly exit: waits until all threads of this connection ended.
 

Static Public Attributes

static final int MIN_SDK_FOR_V4 = 29
 The first Android API level with TLS 1.3 in SSLContext (no TLS provider library is added, D3).
 

Detailed Description

One TCP connection to one FCast receiver, protocol v3 (docs/tech.md, "FCast"; design.md, section 1).

Handshake. connect() connects (with a timeout) and sends Version 3 at once without waiting for the receiver's (both reference receivers expect the sender's first). The receiver's Version is only an upper bound: the connection speaks the smaller number (getNegotiatedVersion()); a receiver of version 4 is spoken to in 3, and this class never sends Version 4 (the desktop receiver would switch to TLS). Our own Initial follows the receiver's Version (or initialWaitMillis without one) and only if the connection speaks 3: a receiver of version 1 or 2 never gets an opcode it does not have (task 2.4). Until the receiver's Version is known the connection assumes 3.

Threads. One reader thread (it answers a Ping with a Pong before anything else, so a slow listener can not make the receiver drop us), one writer thread with a bounded queue (a receiver that stops reading can not make memory grow: the queue is full, the connection is closed with WRITE_OVERFLOW; a single write that blocks longer than writeTimeoutMillis closes it with WRITE_TIMEOUT), one dispatcher thread that calls the Listener in order (bounded as well: FLOOD), and one watchdog thread (heartbeat, write timeout, the late Initial). All are daemon threads and end when the connection is closed (awaitTermination).

Version 1. A receiver of version 1 has no Ping, so it is never pinged and silence does not end the connection (a failing socket does).

Errors. A framing error (size 0, over 32000 bytes, truncated) ends the connection; a body that does not parse is dropped and logged, the connection goes on; an opcode that is not known is skipped.

Constructor & Destructor Documentation

◆ FCastConnection()

de.yaacc.fcast.protocol.FCastConnection.FCastConnection ( InetSocketAddress target,
Listener listener,
Config config,
MonotonicClock clock,
String senderName,
String appName,
String appVersion )
Parameters
targetthe receiver's address
listenerreceives the messages
configtimes and sizes
clockthe time source (monotonic)
senderNamethe name sent in Initial (displayName)
appNamesent in Initial
appVersionsent in Initial

Member Function Documentation

◆ awaitTermination()

boolean de.yaacc.fcast.protocol.FCastConnection.awaitTermination ( long millis) throws InterruptedException

For tests and for an orderly exit: waits until all threads of this connection ended.

Returns
true if they did within the time

◆ close()

void de.yaacc.fcast.protocol.FCastConnection.close ( )

Closes the connection; the listener gets onClosed(CLOSED_BY_US).

Safe to call again.

◆ connect()

void de.yaacc.fcast.protocol.FCastConnection.connect ( ) throws IOException

Connects and starts the threads; the connection is usable when this returns.

Blocking: not for the main thread.

Exceptions
IOExceptionif the receiver can not be reached within connectTimeoutMillis

Referenced by de.yaacc.fcast.protocol.FCastSession.connectorFor().

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

◆ send()

boolean de.yaacc.fcast.protocol.FCastConnection.send ( FCastOpcode opcode,
String body ) throws FCastProtocolException

Queues a message for the receiver.

Never blocks.

Parameters
opcodea command opcode (Play, Pause, Resume, Stop, Seek, SetVolume, SetSpeed, SetPlaylistItem, event subscriptions) of the negotiated version
bodythe JSON text or null
Returns
false if the connection is closed (or the queue overflowed and the connection was closed by this call)
Exceptions
FCastProtocolExceptionUNSUPPORTED if the receiver's version does not have the opcode (say a SetSpeed to a version 1 receiver), TOO_LARGE if the message does not fit into a packet
Here is the call graph for this function:

◆ sendV4()

boolean de.yaacc.fcast.protocol.FCastConnection.sendV4 ( byte[] message) throws FCastProtocolException

Queues a message that only version 4 has (tracks, subtitles).

Never blocks.

Parameters
messagea serialized Packet made by the v4 writer
Returns
false if the connection is closed
Exceptions
FCastProtocolExceptionUNSUPPORTED on a connection that is not version 4
Here is the call graph for this function:

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