Telegram-Android/TMessagesProj/jni/tgnet/Connection.h

91 lines
2.2 KiB
C
Raw Normal View History

2015-09-24 22:52:02 +02:00
/*
* This is the source code of tgnet library v. 1.0
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2015.
*/
#ifndef CONNECTION_H
#define CONNECTION_H
#include <pthread.h>
#include <vector>
#include <string>
2016-03-06 02:49:31 +01:00
#include <openssl/aes.h>
2015-09-24 22:52:02 +02:00
#include "ConnectionSession.h"
#include "ConnectionSocket.h"
#include "Defines.h"
class Datacenter;
class Timer;
class ByteStream;
class Connection : public ConnectionSession, public ConnectionSocket {
public:
Connection(Datacenter *datacenter, ConnectionType type);
~Connection();
void connect();
void suspendConnection();
2017-12-08 18:35:59 +01:00
void suspendConnection(bool idle);
2015-09-24 22:52:02 +02:00
void sendData(NativeByteBuffer *buffer, bool reportAck);
2017-07-08 18:32:04 +02:00
bool hasUsefullData();
void setHasUsefullData();
2015-09-24 22:52:02 +02:00
uint32_t getConnectionToken();
ConnectionType getConnectionType();
Datacenter *getDatacenter();
protected:
void onReceivedData(NativeByteBuffer *buffer) override;
void onDisconnected(int reason) override;
void onConnected() override;
void reconnect();
private:
enum TcpConnectionState {
TcpConnectionStageIdle,
TcpConnectionStageConnecting,
TcpConnectionStageReconnecting,
TcpConnectionStageConnected,
TcpConnectionStageSuspended
};
TcpConnectionState connectionState = TcpConnectionStageIdle;
uint32_t connectionToken = 0;
std::string hostAddress;
uint16_t hostPort;
uint16_t failedConnectionCount;
Datacenter *currentDatacenter;
uint32_t currentAddressFlags;
ConnectionType connectionType;
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 18:32:04 +02:00
bool usefullData = false;
2017-12-08 18:35:59 +01:00
bool forceNextPort = false;
2016-03-06 02:49:31 +01: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 22:52:02 +02:00
friend class ConnectionsManager;
};
#endif