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

115 lines
3.6 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 <openssl/rand.h>
#include "ConnectionSession.h"
#include "MTProtoScheme.h"
#include "ConnectionsManager.h"
#include "NativeByteBuffer.h"
2018-07-30 04:07:02 +02:00
ConnectionSession::ConnectionSession(int32_t instance) {
instanceNum = instance;
}
2015-09-24 22:52:02 +02:00
void ConnectionSession::recreateSession() {
processedMessageIds.clear();
messagesIdsForConfirmation.clear();
processedSessionChanges.clear();
nextSeqNo = 0;
genereateNewSessionId();
}
void ConnectionSession::genereateNewSessionId() {
int64_t newSessionId;
RAND_bytes((uint8_t *) &newSessionId, 8);
#if USE_DEBUG_SESSION
sessionId = (0xabcd000000000000L | (newSessionId & 0x0000ffffffffffffL));
#else
sessionId = newSessionId;
#endif
}
void ConnectionSession::setSessionId(int64_t id) {
sessionId = id;
}
2018-07-30 04:07:02 +02:00
int64_t ConnectionSession::getSessionId() {
2015-09-24 22:52:02 +02:00
return sessionId;
}
uint32_t ConnectionSession::generateMessageSeqNo(bool increment) {
uint32_t value = nextSeqNo;
if (increment) {
nextSeqNo++;
}
return value * 2 + (increment ? 1 : 0);
}
2019-01-23 18:03:33 +01:00
int32_t ConnectionSession::isMessageIdProcessed(int64_t messageId) {
if (!(messageId & 1)) {
return 1;
}
if (minProcessedMessageId != 0 && messageId < minProcessedMessageId) {
return 2;
}
if (std::find(processedMessageIds.begin(), processedMessageIds.end(), messageId) != processedMessageIds.end()) {
return 1;
}
return 0;
2015-09-24 22:52:02 +02:00
}
void ConnectionSession::addProcessedMessageId(int64_t messageId) {
if (processedMessageIds.size() > 300) {
2017-03-31 01:58:05 +02:00
std::sort(processedMessageIds.begin(), processedMessageIds.end());
2015-09-24 22:52:02 +02:00
processedMessageIds.erase(processedMessageIds.begin(), processedMessageIds.begin() + 100);
2017-03-31 01:58:05 +02:00
minProcessedMessageId = *(processedMessageIds.begin());
2015-09-24 22:52:02 +02:00
}
processedMessageIds.push_back(messageId);
}
bool ConnectionSession::hasMessagesToConfirm() {
return !messagesIdsForConfirmation.empty();
}
void ConnectionSession::addMessageToConfirm(int64_t messageId) {
2019-01-23 18:03:33 +01:00
if (std::find(messagesIdsForConfirmation.begin(), messagesIdsForConfirmation.end(), messageId) != messagesIdsForConfirmation.end()) {
2015-09-24 22:52:02 +02:00
return;
}
messagesIdsForConfirmation.push_back(messageId);
}
NetworkMessage *ConnectionSession::generateConfirmationRequest() {
NetworkMessage *networkMessage = nullptr;
if (!messagesIdsForConfirmation.empty()) {
TL_msgs_ack *msgAck = new TL_msgs_ack();
msgAck->msg_ids.insert(msgAck->msg_ids.begin(), messagesIdsForConfirmation.begin(), messagesIdsForConfirmation.end());
NativeByteBuffer *os = new NativeByteBuffer(true);
msgAck->serializeToStream(os);
networkMessage = new NetworkMessage();
networkMessage->message = std::unique_ptr<TL_message>(new TL_message);
2018-07-30 04:07:02 +02:00
networkMessage->message->msg_id = ConnectionsManager::getInstance(instanceNum).generateMessageId();
2015-09-24 22:52:02 +02:00
networkMessage->message->seqno = generateMessageSeqNo(false);
networkMessage->message->bytes = os->capacity();
networkMessage->message->body = std::unique_ptr<TLObject>(msgAck);
messagesIdsForConfirmation.clear();
}
return networkMessage;
}
bool ConnectionSession::isSessionProcessed(int64_t sessionId) {
return std::find(processedSessionChanges.begin(), processedSessionChanges.end(), sessionId) != processedSessionChanges.end();
}
void ConnectionSession::addProcessedSession(int64_t sessionId) {
processedSessionChanges.push_back(sessionId);
}