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