<commit_before>#include "ClientConnection.hpp"

#include "ApiUtils.hpp"
#include "ClientDhLayer.hpp"
#include "ClientRpcLayer.hpp"
#include "CTelegramTransport.hpp"
#include "SendPackageHelper.hpp"
#include "Utils.hpp"
#include "Debug_p.hpp"

#include "PendingRpcOperation.hpp"
#include "Operations/ConnectionOperation.hpp"

#include <QDateTime>
#include <QLoggingCategory>

Q_LOGGING_CATEGORY(c_clientConnectionCategory, "telegram.client.connection", QtWarningMsg)

namespace Telegram {

namespace Client {

class SendPackageHelper : public BaseMTProtoSendHelper
{
public:
    explicit SendPackageHelper(BaseConnection *connection) :
        BaseMTProtoSendHelper()
    {
        m_connection = connection;
    }

    quint64 newMessageId(SendMode mode) override
    {
        quint64 ts = Telegram::Utils::formatTimeStamp(QDateTime::currentMSecsSinceEpoch() + deltaTime() * 1000);
        if (mode == SendMode::Client) {
            ts &= ~quint64(3);
        } else {
            qCWarning(c_clientConnectionCategory) << Q_FUNC_INFO << "Invalid mode";
        }
        return m_connection->transport()->getNewMessageId(ts);
    }

    void sendPacket(const QByteArray &package) override
    {
        return m_connection->transport()->sendPacket(package);
    }
};

/*
    \class Telegram::Client::Connection
    \brief Client-specific connection with DH and RPC layers.
    \inmodule TelegramQt
    \ingroup Client
*/
Connection::Connection(QObject *parent) :
    BaseConnection(parent)
{
    m_sendHelper = new SendPackageHelper(this);
    m_dhLayer = new DhLayer(this);
    m_dhLayer->setSendPackageHelper(m_sendHelper);
    connect(m_dhLayer, &BaseDhLayer::stateChanged, this, &Connection::onClientDhStateChanged);
    m_rpcLayer = new RpcLayer(this);
    m_rpcLayer->setSendHelper(m_sendHelper);
}

void Connection::setDcOption(const DcOption &dcOption)
{
    m_dcOption = dcOption;
    setObjectName(QStringLiteral("Client(dc%1)").arg(dcOption.id));
    m_dhLayer->setObjectName(objectName());
    m_rpcLayer->setObjectName(objectName());
}

qint32 Connection::deltaTime() const
{
    return m_sendHelper->deltaTime();
}

void Connection::setDeltaTime(const qint32 newDt)
{
    m_sendHelper->setDeltaTime(newDt);
}

Connection *Connection::fromOperation(PendingRpcOperation *operation)
{
    return reinterpret_cast<Telegram::Client::Connection*>(operation->getConnection());
}

RpcLayer *Connection::rpcLayer()
{
    return reinterpret_cast<RpcLayer*>(m_rpcLayer);
}

/*!
  \fn ConnectOperation *Connection::connectToDc()

  Initiates connection to DC and returns an Operation object.

  This method establish Transport (TCP/UDP/HTTP) level connection.<commit_msg>Connection: Set the object name in constructor<commit_after>#include "ClientConnection.hpp"

#include "ApiUtils.hpp"
#include "ClientDhLayer.hpp"
#include "ClientRpcLayer.hpp"
#include "CTelegramTransport.hpp"
#include "SendPackageHelper.hpp"
#include "Utils.hpp"
#include "Debug_p.hpp"

#include "PendingRpcOperation.hpp"
#include "Operations/ConnectionOperation.hpp"

#include <QDateTime>
#include <QLoggingCategory>

Q_LOGGING_CATEGORY(c_clientConnectionCategory, "telegram.client.connection", QtWarningMsg)

namespace Telegram {

namespace Client {

class SendPackageHelper : public BaseMTProtoSendHelper
{
public:
    explicit SendPackageHelper(BaseConnection *connection) :
        BaseMTProtoSendHelper()
    {
        m_connection = connection;
    }

    quint64 newMessageId(SendMode mode) override
    {
        quint64 ts = Telegram::Utils::formatTimeStamp(QDateTime::currentMSecsSinceEpoch() + deltaTime() * 1000);
        if (mode == SendMode::Client) {
            ts &= ~quint64(3);
        } else {
            qCWarning(c_clientConnectionCategory) << Q_FUNC_INFO << "Invalid mode";
        }
        return m_connection->transport()->getNewMessageId(ts);
    }

    void sendPacket(const QByteArray &package) override
    {
        return m_connection->transport()->sendPacket(package);
    }
};

/*
    \class Telegram::Client::Connection
    \brief Client-specific connection with DH and RPC layers.
    \inmodule TelegramQt
    \ingroup Client
*/
Connection::Connection(QObject *parent) :
    BaseConnection(parent)
{
    m_sendHelper = new SendPackageHelper(this);
    m_dhLayer = new DhLayer(this);
    m_dhLayer->setSendPackageHelper(m_sendHelper);
    connect(m_dhLayer, &BaseDhLayer::stateChanged, this, &Connection::onClientDhStateChanged);
    m_rpcLayer = new RpcLayer(this);
    m_rpcLayer->setSendHelper(m_sendHelper);
}

void Connection::setDcOption(const DcOption &dcOption)
{
    m_dcOption = dcOption;
    setObjectName(QStringLiteral("Client(dc%1, flags %2)")
                  .arg(dcOption.id)
                  .arg(dcOption.flags)
                  );
    m_dhLayer->setObjectName(objectName());
    m_rpcLayer->setObjectName(objectName());
}

qint32 Connection::deltaTime() const
{
    return m_sendHelper->deltaTime();
}

void Connection::setDeltaTime(const qint32 newDt)
{
    m_sendHelper->setDeltaTime(newDt);
}

Connection *Connection::fromOperation(PendingRpcOperation *operation)
{
    return reinterpret_cast<Telegram::Client::Connection*>(operation->getConnection());
}

RpcLayer *Connection::rpcLayer()
{
    return reinterpret_cast<RpcLayer*>(m_rpcLayer);
}

/*!
  \fn ConnectOperation *Connection::connectToDc()

  Initiates connection to DC and returns an Operation object.

  This method establish Transport (TCP/UDP/HTTP) level connection.
{'fraction_non_alphanumeric': 0.11462522851919561, 'fraction_numerical': 0.00603290676416819, 'mean_word_length': 4.58265306122449, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6320807', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1670, 'n_words': 307}