Telegram-Android/TMessagesProj/jni/tgnet/Request.cpp

98 lines
2.8 KiB
C++
Raw Normal View History

2015-09-24 22:52:02 +02:00
/*
2018-07-30 04:07:02 +02:00
* This is the source code of tgnet library v. 1.1
2015-09-24 22:52:02 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2018-07-30 04:07:02 +02:00
* Copyright Nikolai Kudashov, 2015-2018.
2015-09-24 22:52:02 +02:00
*/
#include <algorithm>
#include "Request.h"
#include "TLObject.h"
#include "MTProtoScheme.h"
#include "ConnectionsManager.h"
2018-07-30 04:07:02 +02:00
#include "Datacenter.h"
#include "Connection.h"
2015-09-24 22:52:02 +02:00
2018-07-30 04:07:02 +02:00
Request::Request(int32_t instance, int32_t token, ConnectionType type, uint32_t flags, uint32_t datacenter, onCompleteFunc completeFunc, onQuickAckFunc quickAckFunc, onWriteToSocketFunc writeToSocketFunc) {
2015-09-24 22:52:02 +02:00
requestToken = token;
connectionType = type;
requestFlags = flags;
datacenterId = datacenter;
onCompleteRequestCallback = completeFunc;
onQuickAckCallback = quickAckFunc;
2017-07-08 18:32:04 +02:00
onWriteToSocketCallback = writeToSocketFunc;
2017-03-31 01:58:05 +02:00
dataType = (uint8_t) (requestFlags >> 24);
2018-07-30 04:07:02 +02:00
instanceNum = instance;
2015-09-24 22:52:02 +02:00
}
Request::~Request() {
#ifdef ANDROID
if (ptr1 != nullptr) {
2018-07-30 04:07:02 +02:00
jniEnv[instanceNum]->DeleteGlobalRef(ptr1);
2015-09-24 22:52:02 +02:00
ptr1 = nullptr;
}
if (ptr2 != nullptr) {
2018-07-30 04:07:02 +02:00
jniEnv[instanceNum]->DeleteGlobalRef(ptr2);
2015-09-24 22:52:02 +02:00
ptr2 = nullptr;
}
2017-07-08 18:32:04 +02:00
if (ptr3 != nullptr) {
2018-07-30 04:07:02 +02:00
jniEnv[instanceNum]->DeleteGlobalRef(ptr3);
2017-07-08 18:32:04 +02:00
ptr3 = nullptr;
}
2015-09-24 22:52:02 +02:00
#endif
}
void Request::addRespondMessageId(int64_t id) {
respondsToMessageIds.push_back(messageId);
}
bool Request::respondsToMessageId(int64_t id) {
return messageId == id || std::find(respondsToMessageIds.begin(), respondsToMessageIds.end(), id) != respondsToMessageIds.end();
}
void Request::clear(bool time) {
messageId = 0;
messageSeqNo = 0;
connectionToken = 0;
if (time) {
startTime = 0;
minStartTime = 0;
}
}
2021-03-19 11:25:58 +01:00
void Request::onComplete(TLObject *result, TL_error *error, int32_t networkType, int64_t responseTime) {
if (onCompleteRequestCallback != nullptr && (result != nullptr || error != nullptr)) {
2021-03-19 11:25:58 +01:00
onCompleteRequestCallback(result, error, networkType, responseTime);
2015-09-24 22:52:02 +02:00
}
}
2017-07-08 18:32:04 +02:00
void Request::onWriteToSocket() {
if (onWriteToSocketCallback != nullptr) {
onWriteToSocketCallback();
}
}
2018-07-30 04:07:02 +02:00
bool Request::hasInitFlag() {
return isInitRequest || isInitMediaRequest;
}
bool Request::isMediaRequest() {
return Connection::isMediaConnectionType(connectionType);
}
bool Request::needInitRequest(Datacenter *datacenter, uint32_t currentVersion) {
bool media = PFS_ENABLED && datacenter != nullptr && isMediaRequest() && datacenter->hasMediaAddress();
return !media && datacenter->lastInitVersion != currentVersion || media && datacenter->lastInitMediaVersion != currentVersion;
}
2015-09-24 22:52:02 +02:00
void Request::onQuickAck() {
if (onQuickAckCallback != nullptr) {
onQuickAckCallback();
}
}
TLObject *Request::getRpcRequest() {
return rpcRequest.get();
}