reset method

Future<void> reset(
  1. int errorCode
)

Abruptly terminates the stream by sending a RESET_STREAM frame.

Implementation

Future<void> reset(int errorCode) async {
  if (!_connected) return;

  if (remoteId != null && remoteHost != null && remotePort != null) {
    // Use a local variable to avoid multiple null checks
    final socket = _socket;
    if (socket == null) {
      ////print('[UDXStream ${this.id}.reset] Socket is null, cannot send reset frame.');
      // Continue with local close even if we can't send the reset frame
    } else {
      final resetPacket = UDXPacket(
        destinationCid: _socket!.cids.remoteCid,
        sourceCid: _socket!.cids.localCid,
        destinationStreamId: remoteId!,
        sourceStreamId: id,
        sequence: packetManager.nextSequence,
        frames: [ResetStreamFrame(errorCode: errorCode)],
      );
      try {
        // This is a "fire-and-forget" send. We don't wait for an ACK.
        socket.send(resetPacket.toBytes());
      } catch (e) {
        // Ignore errors during reset, as the stream is being torn down.
      }
    }
  }

  // Immediately transition to a closed state locally.
  await _close(isReset: true);
}