executeTransaction method

Future<CallResult> executeTransaction(
  1. UdpAddress? remoteConnectionPoint,
  2. int sessionId,
  3. Uint8List data
)

Implementation

Future<CallResult> executeTransaction(
    UdpAddress? remoteConnectionPoint, int sessionId, Uint8List data) async {
  int localTransactionId = nextTransactionId;
  nextTransactionId++;

  Transaction tr = Transaction(0x10, localAddress(), remoteAddress,
      localTransactionId, sessionId, 0, data.length, data);
  outgoingTransactions[tr.transactionId] = tr;

  List<Uint8List> frames = [];

  int offset = 0;
  int blockSize = 524;
  while (offset < tr.data.length) {
    int currentBlockSize = blockSize;
    int restDataLen = tr.data.length - offset;
    if (restDataLen < currentBlockSize) {
      currentBlockSize = restDataLen;
    }

    var blockTransaction = Transaction(
        tr.frameType,
        tr.srcAddress,
        tr.destAddress,
        tr.transactionId,
        tr.sessionId,
        offset,
        tr.totalSize,
        tr.data.sublist(offset, offset + currentBlockSize));

    Uint8List blockFrame = Uint8List.fromList(blockTransaction.serialize());
    frames.add(blockFrame);
    offset += currentBlockSize;
  }
  sendFrame(remoteConnectionPoint, frames, peer);

  for (int i = 0; i < 100; i++) {
    if (tr.complete) {
      outgoingTransactions.remove(localTransactionId);
      if (tr.error.isNotEmpty) {
        return CallResult.createError(tr.error);
      }

      CallResult result = CallResult();
      result.data = tr.result;
      return result;
    }
    await Future.delayed(const Duration(milliseconds: 10));
  }

  outgoingTransactions.remove(localTransactionId);
  resetConnectionPoint();
  return CallResult.createError("transaction timeout");
}