UDPSocket constructor

UDPSocket({
  1. required UDX udx,
  2. required UDXMultiplexer multiplexer,
  3. required InternetAddress remoteAddress,
  4. required int remotePort,
  5. required ConnectionCids cids,
  6. UdxMetricsObserver? metricsObserver,
  7. bool isServer = false,
  8. Clock? clock,
})

Creates a new UDX connection socket.

Implementation

UDPSocket({
  required this.udx,
  required this.multiplexer,
  required this.remoteAddress,
  required this.remotePort,
  required this.cids,
  this.metricsObserver,
  this.isServer = false,
  Clock? clock,
}) : clock = clock ?? const SystemClock() {
  _localConnectionMaxData = defaultInitialConnectionWindow;
  _remoteConnectionMaxData = defaultInitialConnectionWindow;
  _localMaxStreams = defaultMaxStreams;
  _mtu = MtuDiscovery(clock: this.clock);

  // Initialize connection-level packet management
  _packetManager = PacketManager(clock: this.clock);
  _congestionController = CongestionController(packetManager: _packetManager, clock: this.clock);
  _packetManager.congestionController = _congestionController;
  _lostQueue = LostQueue(
    packetManager: _packetManager,
    congestionController: _congestionController,
    clock: this.clock,
    send: _sendQuietly,
    maxDatagram: () => _mtu.current,
  );

  // A PTO probe re-sends the oldest outstanding packets under fresh
  // sequences, through this; their bytes are still in flight. Probe
  // timeouts in a row while streams send more than the base datagram
  // size are read as the path having stopped carrying that size (a
  // carrier's tunnel after a handover drops every full-size packet and
  // answers nothing): the size falls back to the base and the probe is
  // re-chunked to fit, the first packet that can get through.
  _packetManager.onRetransmit = (packet) => _sendQuietly(packet.toBytes());
  _congestionController.onProbe = () {
    if (_congestionController.ptoCount >= blackholePtos) _mtu.blackhole();
    _packetManager.sendProbe(maxDatagram: _mtu.current);
  };
  _packetManager.onPacketsLost = _onPacketsLost;

  // For client-initiated connections, address is validated by default
  // For server-side connections (receiving SYN), address must be validated
  _addressValidated = !isServer;

  // Notify observer that handshake is starting
  _handshakeStartTime = this.clock.now();
  metricsObserver?.onHandshakeStart(
    cids.localCid,
    cids.remoteCid,
    '${remoteAddress.address}:$remotePort',
  );

  _lastActivity = this.clock.now();
  _armIdleCheck();
}