Exemplo n.º 1
0
  private void processUpdate(long authId, byte[] content) {
    if (authId != currentAuthId) {
      return;
    }

    ProtoStruct protoStruct;
    try {
      protoStruct = ProtoSerializer.readUpdate(content);
    } catch (IOException e) {
      e.printStackTrace();
      Log.w(TAG, "Broken mt update");
      return;
    }

    int type = ((Push) protoStruct).updateType;
    byte[] body = ((Push) protoStruct).body;

    RpcScope updateBox;
    try {
      updateBox = new RpcParser().read(type, body);
    } catch (IOException e) {
      e.printStackTrace();
      Log.w(TAG, "Broken update box");
      return;
    }

    // Log.w(TAG, "Box: " + updateBox + "");

    callback.onUpdateReceived(updateBox);
  }
Exemplo n.º 2
0
  private void processResponse(long authId, long mid, byte[] content) {
    if (authId != currentAuthId) {
      return;
    }

    ProtoStruct protoStruct;
    try {
      protoStruct = ProtoSerializer.readRpcResponsePayload(content);
    } catch (IOException e) {
      e.printStackTrace();
      Log.w(TAG, "Broken response mid#" + mid);
      return;
    }

    // Log.w(TAG, protoStruct + " mid#" + mid);

    long rid;
    if (idMap.containsKey(mid)) {
      rid = idMap.get(mid);
    } else {
      return;
    }

    RequestHolder holder;
    if (requests.containsKey(rid)) {
      holder = requests.get(rid);
    } else {
      return;
    }

    if (protoStruct instanceof RpcOk) {
      RpcOk ok = (RpcOk) protoStruct;
      requests.remove(rid);
      if (holder.protoId != 0) {
        idMap.remove(holder.protoId);
      }

      Response response;
      try {
        response = (Response) new RpcParser().read(ok.responseType, ok.payload);
      } catch (IOException e) {
        e.printStackTrace();
        return;
      }

      Log.d(
          TAG,
          "<- response#"
              + holder.publicId
              + ": "
              + response
              + " in "
              + (Environment.getCurrentTime() - holder.requestTime)
              + " ms");

      holder.callback.onResult(response);
    } else if (protoStruct instanceof RpcError) {
      RpcError e = (RpcError) protoStruct;
      requests.remove(rid);
      if (holder.protoId != 0) {
        idMap.remove(holder.protoId);
      }

      Log.w(
          TAG,
          "<- error#"
              + holder.publicId
              + ": "
              + e.errorTag
              + " "
              + e.errorCode
              + " "
              + e.userMessage
              + " in "
              + (Environment.getCurrentTime() - holder.requestTime)
              + " ms");

      holder.callback.onError(
          new RpcException(e.errorTag, e.errorCode, e.userMessage, e.canTryAgain, e.relatedData));
    } else if (protoStruct instanceof RpcInternalError) {
      RpcInternalError e = ((RpcInternalError) protoStruct);
      Log.d(
          TAG,
          "<- internal_error#"
              + holder.publicId
              + " "
              + e.getTryAgainDelay()
              + " sec"
              + " in "
              + (Environment.getCurrentTime() - holder.requestTime)
              + " ms");
      if (e.isCanTryAgain()) {
        self().send(new ForceResend(rid), e.getTryAgainDelay() * 1000L);
      } else {
        requests.remove(rid);
        if (holder.protoId != 0) {
          idMap.remove(holder.protoId);
        }
        holder.callback.onError(new RpcInternalException());
      }
    } else if (protoStruct instanceof RpcFloodWait) {
      RpcFloodWait f = (RpcFloodWait) protoStruct;
      Log.d(
          TAG,
          "<- flood_wait#"
              + holder.publicId
              + " "
              + f.getDelay()
              + " sec"
              + " in "
              + (Environment.getCurrentTime() - holder.requestTime)
              + " ms");
      self().send(new ForceResend(rid), f.getDelay() * 1000L);
    } else {
      Log.d(
          TAG,
          "<- unknown_package#"
              + holder.publicId
              + " in "
              + (Environment.getCurrentTime() - holder.requestTime)
              + " ms");
    }
  }