add method

  1. @override
Future<void> add(
  1. Uint8List data
)
override

Adds a data event to the sink.

Must not be called on a closed sink.

Implementation

@override
Future<void> add(Uint8List data) async {
  if (!_connected) throw StateError('UDXStream ($id): Stream is not connected');
  if (_socket == null) throw StateError('UDXStream ($id): Stream is not connected to a socket');

  // DEBUG: Log window states at the beginning of add
  if (_socket != null) {
    // Use a local variable to avoid multiple null checks
    final socket = _socket;
    if (socket == null) {
      ////print('[UDXStream ${this.id}.add] Socket became null after initial check, cannot proceed.');
      throw StateError('UDXStream ($id): Socket became null during operation');
    }
    ////print('[UDXStream ${this.id} (${this.remoteHost}:${this.remotePort} rID:${this.remoteId}).add ENTRY] DEBUG: Initial available conn window: ${socket.getAvailableConnectionSendWindow()}, remoteStreamWindow: $_remoteReceiveWindow, cwnd: $cwnd, inflight: $inflight, data.length: ${data.length}');
  }
  if (remoteId == null || remoteHost == null || remotePort == null) {
    throw StateError('UDXStream ($id): Remote peer details not set');
  }
  if (data.isEmpty) {
    return;
  }

  // Fragment the data according to MTU size if needed
  final fragments = _fragmentData(data);

  for (final fragment in fragments) {
    await _sendFragment(fragment);
  }

  emit('send', data);
}