2015-09-24 20:52:02 +00:00
|
|
|
/*
|
2018-07-30 02:07:02 +00:00
|
|
|
* This is the source code of tgnet library v. 1.1
|
2015-09-24 20:52:02 +00: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 02:07:02 +00:00
|
|
|
* Copyright Nikolai Kudashov, 2015-2018.
|
2015-09-24 20:52:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include "Request.h"
|
|
|
|
#include "TLObject.h"
|
|
|
|
#include "MTProtoScheme.h"
|
|
|
|
#include "ConnectionsManager.h"
|
2018-07-30 02:07:02 +00:00
|
|
|
#include "Datacenter.h"
|
|
|
|
#include "Connection.h"
|
2023-03-24 11:38:14 +00:00
|
|
|
#include "FileLog.h"
|
2015-09-24 20:52:02 +00:00
|
|
|
|
2024-03-08 14:32:16 +00:00
|
|
|
Request::Request(int32_t instance, int32_t token, ConnectionType type, uint32_t flags, uint32_t datacenter, onCompleteFunc completeFunc, onQuickAckFunc quickAckFunc, onWriteToSocketFunc writeToSocketFunc, onRequestClearFunc onClearFunc) {
|
2015-09-24 20:52:02 +00:00
|
|
|
requestToken = token;
|
|
|
|
connectionType = type;
|
|
|
|
requestFlags = flags;
|
|
|
|
datacenterId = datacenter;
|
|
|
|
onCompleteRequestCallback = completeFunc;
|
|
|
|
onQuickAckCallback = quickAckFunc;
|
2017-07-08 16:32:04 +00:00
|
|
|
onWriteToSocketCallback = writeToSocketFunc;
|
2024-03-08 14:32:16 +00:00
|
|
|
onRequestClearCallback = onClearFunc;
|
2017-03-30 23:58:05 +00:00
|
|
|
dataType = (uint8_t) (requestFlags >> 24);
|
2018-07-30 02:07:02 +00:00
|
|
|
instanceNum = instance;
|
2015-09-24 20:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Request::~Request() {
|
2024-03-08 14:32:16 +00:00
|
|
|
if (!completedSent && !disableClearCallback && onRequestClearCallback != nullptr) {
|
|
|
|
onRequestClearCallback();
|
2015-09-24 20:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 01:16:18 +00:00
|
|
|
void Request::onComplete(TLObject *result, TL_error *error, int32_t networkType, int64_t responseTime, int64_t requestMsgId) {
|
2016-01-11 17:19:48 +00:00
|
|
|
if (onCompleteRequestCallback != nullptr && (result != nullptr || error != nullptr)) {
|
2024-03-08 14:32:16 +00:00
|
|
|
completedSent = true;
|
2022-11-06 01:16:18 +00:00
|
|
|
onCompleteRequestCallback(result, error, networkType, responseTime, requestMsgId);
|
2015-09-24 20:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:32:04 +00:00
|
|
|
void Request::onWriteToSocket() {
|
|
|
|
if (onWriteToSocketCallback != nullptr) {
|
|
|
|
onWriteToSocketCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 02:07:02 +00:00
|
|
|
bool Request::hasInitFlag() {
|
|
|
|
return isInitRequest || isInitMediaRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Request::isMediaRequest() {
|
|
|
|
return Connection::isMediaConnectionType(connectionType);
|
|
|
|
}
|
|
|
|
|
2023-10-28 20:52:06 +00:00
|
|
|
bool Request::isCancelRequest() {
|
|
|
|
return (requestFlags & RequestFlagIsCancel) != 0;
|
|
|
|
}
|
|
|
|
|
2018-07-30 02:07:02 +00:00
|
|
|
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 20:52:02 +00:00
|
|
|
void Request::onQuickAck() {
|
|
|
|
if (onQuickAckCallback != nullptr) {
|
|
|
|
onQuickAckCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TLObject *Request::getRpcRequest() {
|
|
|
|
return rpcRequest.get();
|
|
|
|
}
|