1
0
mirror of https://github.com/MGislv/NekoX.git synced 2024-07-04 11:13:36 +00:00
NekoX/TMessagesProj/jni/tgnet/Connection.h

121 lines
3.2 KiB
C
Raw Normal View History

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
*/
#ifndef CONNECTION_H
#define CONNECTION_H
#include <pthread.h>
#include <vector>
#include <string>
2016-03-06 01:49:31 +00:00
#include <openssl/aes.h>
2015-09-24 20:52:02 +00:00
#include "ConnectionSession.h"
#include "ConnectionSocket.h"
#include "Defines.h"
class Datacenter;
class Timer;
class ByteStream;
2018-07-30 02:07:02 +00:00
class ByteArray;
2015-09-24 20:52:02 +00:00
class Connection : public ConnectionSession, public ConnectionSocket {
public:
2018-07-30 02:07:02 +00:00
Connection(Datacenter *datacenter, ConnectionType type, int8_t num);
2015-09-24 20:52:02 +00:00
~Connection();
void connect();
void suspendConnection();
2017-12-08 17:35:59 +00:00
void suspendConnection(bool idle);
2018-07-30 02:07:02 +00:00
void sendData(NativeByteBuffer *buffer, bool reportAck, bool encrypted);
2017-07-08 16:32:04 +00:00
bool hasUsefullData();
void setHasUsefullData();
2018-07-30 02:07:02 +00:00
bool allowsCustomPadding();
2015-09-24 20:52:02 +00:00
uint32_t getConnectionToken();
ConnectionType getConnectionType();
2018-07-30 02:07:02 +00:00
int8_t getConnectionNum();
2015-09-24 20:52:02 +00:00
Datacenter *getDatacenter();
2018-07-30 02:07:02 +00:00
bool isSuspended();
static bool isMediaConnectionType(ConnectionType type);
2015-09-24 20:52:02 +00:00
protected:
void onReceivedData(NativeByteBuffer *buffer) override;
2018-07-30 02:07:02 +00:00
void onDisconnected(int32_t reason, int32_t error) override;
2015-09-24 20:52:02 +00:00
void onConnected() override;
2019-01-23 17:03:33 +00:00
bool hasPendingRequests() override;
2015-09-24 20:52:02 +00:00
void reconnect();
private:
enum TcpConnectionState {
TcpConnectionStageIdle,
TcpConnectionStageConnecting,
TcpConnectionStageReconnecting,
TcpConnectionStageConnected,
TcpConnectionStageSuspended
};
2018-07-30 02:07:02 +00:00
enum ProtocolType {
ProtocolTypeEF,
ProtocolTypeEE,
2019-08-21 23:53:26 +00:00
ProtocolTypeDD,
ProtocolTypeTLS
2018-07-30 02:07:02 +00:00
};
inline void encryptKeyWithSecret(uint8_t *array, uint8_t secretType);
inline std::string *getCurrentSecret(uint8_t secretType);
2019-05-14 12:08:05 +00:00
void onDisconnectedInternal(int32_t reason, int32_t error);
2018-07-30 02:07:02 +00:00
ProtocolType currentProtocolType = ProtocolTypeEE;
2015-09-24 20:52:02 +00:00
TcpConnectionState connectionState = TcpConnectionStageIdle;
uint32_t connectionToken = 0;
std::string hostAddress;
2018-07-30 02:07:02 +00:00
std::string secret;
2015-09-24 20:52:02 +00:00
uint16_t hostPort;
uint16_t failedConnectionCount;
Datacenter *currentDatacenter;
uint32_t currentAddressFlags;
ConnectionType connectionType;
2018-07-30 02:07:02 +00:00
int8_t connectionNum;
2015-09-24 20:52:02 +00:00
bool firstPacketSent = false;
NativeByteBuffer *restOfTheData = nullptr;
uint32_t lastPacketLength = 0;
bool hasSomeDataSinceLastConnect = false;
bool isTryingNextPort = false;
bool wasConnected = false;
uint32_t willRetryConnectCount = 5;
Timer *reconnectTimer;
2017-07-08 16:32:04 +00:00
bool usefullData = false;
2017-12-08 17:35:59 +00:00
bool forceNextPort = false;
2018-07-30 02:07:02 +00:00
bool isMediaConnection = false;
bool waitForReconnectTimer = false;
2019-05-14 12:08:05 +00:00
bool connectionInProcess = false;
2018-07-30 02:07:02 +00:00
uint32_t lastReconnectTimeout = 100;
int64_t usefullDataReceiveTime;
2019-01-23 17:03:33 +00:00
uint32_t currentTimeout = 4;
uint32_t receivedDataAmount = 0;
2018-07-30 02:07:02 +00:00
uint8_t temp[64];
2016-03-06 01:49:31 +00:00
AES_KEY encryptKey;
uint8_t encryptIv[16];
uint32_t encryptNum;
uint8_t encryptCount[16];
AES_KEY decryptKey;
uint8_t decryptIv[16];
uint32_t decryptNum;
uint8_t decryptCount[16];
2015-09-24 20:52:02 +00:00
friend class ConnectionsManager;
};
#endif