Connection.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This is the source code of tgnet library v. 1.1
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Copyright Nikolai Kudashov, 2015-2018.
  7. */
  8. #ifndef CONNECTION_H
  9. #define CONNECTION_H
  10. #include <pthread.h>
  11. #include <vector>
  12. #include <string>
  13. #include <openssl/aes.h>
  14. #include "ConnectionSession.h"
  15. #include "ConnectionSocket.h"
  16. #include "Defines.h"
  17. class Datacenter;
  18. class Timer;
  19. class ByteStream;
  20. class ByteArray;
  21. class Connection : public ConnectionSession, public ConnectionSocket {
  22. public:
  23. Connection(Datacenter *datacenter, ConnectionType type, int8_t num);
  24. ~Connection();
  25. void connect();
  26. void suspendConnection();
  27. void suspendConnection(bool idle);
  28. void sendData(NativeByteBuffer *buffer, bool reportAck, bool encrypted);
  29. bool hasUsefullData();
  30. void setHasUsefullData();
  31. bool allowsCustomPadding();
  32. uint32_t getConnectionToken();
  33. ConnectionType getConnectionType();
  34. int8_t getConnectionNum();
  35. Datacenter *getDatacenter();
  36. bool isSuspended();
  37. static bool isMediaConnectionType(ConnectionType type);
  38. protected:
  39. void onReceivedData(NativeByteBuffer *buffer) override;
  40. void onDisconnected(int32_t reason, int32_t error) override;
  41. void onConnected() override;
  42. bool hasPendingRequests() override;
  43. void reconnect();
  44. private:
  45. enum TcpConnectionState {
  46. TcpConnectionStageIdle,
  47. TcpConnectionStageConnecting,
  48. TcpConnectionStageReconnecting,
  49. TcpConnectionStageConnected,
  50. TcpConnectionStageSuspended
  51. };
  52. enum ProtocolType {
  53. ProtocolTypeEF,
  54. ProtocolTypeEE,
  55. ProtocolTypeDD,
  56. ProtocolTypeTLS
  57. };
  58. inline void encryptKeyWithSecret(uint8_t *array, uint8_t secretType);
  59. inline std::string *getCurrentSecret(uint8_t secretType);
  60. void onDisconnectedInternal(int32_t reason, int32_t error);
  61. ProtocolType currentProtocolType = ProtocolTypeEE;
  62. TcpConnectionState connectionState = TcpConnectionStageIdle;
  63. uint32_t connectionToken = 0;
  64. std::string hostAddress;
  65. std::string secret;
  66. uint16_t hostPort;
  67. uint16_t failedConnectionCount;
  68. Datacenter *currentDatacenter;
  69. uint32_t currentAddressFlags;
  70. ConnectionType connectionType;
  71. int8_t connectionNum;
  72. bool firstPacketSent = false;
  73. NativeByteBuffer *restOfTheData = nullptr;
  74. uint32_t lastPacketLength = 0;
  75. bool hasSomeDataSinceLastConnect = false;
  76. bool isTryingNextPort = false;
  77. bool wasConnected = false;
  78. uint32_t willRetryConnectCount = 5;
  79. Timer *reconnectTimer;
  80. bool usefullData = false;
  81. bool forceNextPort = false;
  82. bool isMediaConnection = false;
  83. bool waitForReconnectTimer = false;
  84. bool connectionInProcess = false;
  85. uint32_t lastReconnectTimeout = 100;
  86. int64_t usefullDataReceiveTime;
  87. uint32_t currentTimeout = 4;
  88. uint32_t receivedDataAmount = 0;
  89. uint8_t temp[64];
  90. AES_KEY encryptKey;
  91. uint8_t encryptIv[16];
  92. uint32_t encryptNum;
  93. uint8_t encryptCount[16];
  94. AES_KEY decryptKey;
  95. uint8_t decryptIv[16];
  96. uint32_t decryptNum;
  97. uint8_t decryptCount[16];
  98. friend class ConnectionsManager;
  99. };
  100. #endif