UDXPacket.fromBytes constructor

UDXPacket.fromBytes(
  1. Uint8List bytes
)

Creates a UDX packet from raw bytes. Header is 28 bytes. 0-7: destinationConnectionId 8-15: sourceConnectionId 16-19: sequence 20-23: destinationStreamId 24-27: sourceStreamId

Implementation

factory UDXPacket.fromBytes(Uint8List bytes) {
  const headerLength = 28;
  if (bytes.length < headerLength) {
    throw ArgumentError(
        'Byte array too short for UDXPacket. Minimum $headerLength bytes required, got ${bytes.length}');
  }
  final view =
      ByteData.view(bytes.buffer, bytes.offsetInBytes, bytes.lengthInBytes);

  final destCid = ConnectionId.fromUint8List(
      bytes.sublist(0, ConnectionId.cidLength));
  final srcCid = ConnectionId.fromUint8List(
      bytes.sublist(ConnectionId.cidLength, ConnectionId.cidLength * 2));
  final sequence = view.getUint32(16, Endian.big);
  final destinationStreamId = view.getUint32(20, Endian.big);
  final sourceStreamId = view.getUint32(24, Endian.big);

  final frames = <Frame>[];
  int offset = headerLength;
  while (offset < bytes.length) {
    final frame = Frame.fromBytes(view, offset);
    frames.add(frame);
    offset += frame.length;
  }

  return UDXPacket(
    destinationCid: destCid,
    sourceCid: srcCid,
    destinationStreamId: destinationStreamId,
    sourceStreamId: sourceStreamId,
    sequence: sequence,
    frames: frames,
  );
}