call method

Future<CallResult> call(
  1. String function,
  2. Uint8List data
)

Implementation

Future<CallResult> call(String function, Uint8List data) async {
  await checkInternetConnectionPoint();

  if (lanConnectionPoint1 == null) {
    if (lanConnectionPoint1 == null) {
      checkLANConnectionPoint();
    }
  }

  // Waiting for network
  for (int i = 0; i < 100; i++) {
    if (peer.network != null) {
      break;
    }
    await Future.delayed(const Duration(milliseconds: 10));
  }
  await checkInternetConnectionPoint();

  // Waiting for public key
  for (int i = 0; i < 100; i++) {
    if (remotePublicKey != null) {
      break;
    }
    await Future.delayed(const Duration(milliseconds: 10));
  }

  if (peer.network == null && lanConnectionPoint1 == null) {
    // No route
    return CallResult.createError("connecting to xchg network");
  }

  if (sessionId == 0) {
    print("auth start");
    String authRes = await auth(lanConnectionPoint1);
    if (authRes.isNotEmpty) {
      CallResult result = CallResult();
      result.error = "auth error:" + authRes;
      sessionId = 0;
      aesKey = Uint8List(0);
      return result;
    }
    print("auth ok");
  }

  CallResult result =
      await regularCall(lanConnectionPoint1, function, data, aesKey);
  return result;
}