| #include "Message.h" |
| 6 |
| 7 | namespace rtc { |
| 8 | class ByteBufferReader; |
| 9 | } // namespace rtc |
| 10 |
| 11 | namespace tgcalls { |
| 12 |
| 13 | class EncryptedConnection final { |
| 14 | public: |
| 15 | enum class Type : uint8_t { |
| 16 | Signaling, |
| 17 | Transport, |
| 18 | }; |
| 19 | EncryptedConnection( |
| 20 | Type type, |
| 21 | const EncryptionKey &key, |
| 22 | std::function<void(int delayMs, int cause)> requestSendService); |
| 23 |
| 24 | struct EncryptedPacket { |
| 25 | std::vector<uint8_t> bytes; |
| 26 | uint32_t counter = 0; |
| 27 | }; |
| 28 | absl::optional<EncryptedPacket> prepareForSending(const Message &message); |
| 29 | absl::optional<EncryptedPacket> prepareForSendingRawMessage(rtc::CopyOnWriteBuffer &serialized, bool messageRequiresAck); |
| 30 | absl::optional<EncryptedPacket> prepareForSendingService(int cause); |
| 31 |
| 32 | struct DecryptedPacket { |
| 33 | DecryptedMessage main; |
| 34 | std::vector<DecryptedMessage> additional; |
| 35 | }; |
| 36 | struct DecryptedRawPacket { |
| 37 | DecryptedRawMessage main; |
| 38 | std::vector<DecryptedRawMessage> additional; |
| 39 | }; |
| 40 | absl::optional<DecryptedPacket> handleIncomingPacket(const char *bytes, size_t size); |
| 41 | absl::optional<DecryptedRawPacket> handleIncomingRawPacket(const char *bytes, size_t size); |
| 42 |
| 43 | absl::optional<rtc::CopyOnWriteBuffer> encryptRawPacket(rtc::CopyOnWriteBuffer const &buffer); |
| 44 | absl::optional<rtc::CopyOnWriteBuffer> decryptRawPacket(rtc::CopyOnWriteBuffer const &buffer); |
| 45 |
| 46 | private: |
| 47 | struct DelayIntervals { |
| 48 | // In milliseconds. |
| 49 | int minDelayBeforeMessageResend = 0; |
| 50 | int maxDelayBeforeMessageResend = 0; |
| 51 | int maxDelayBeforeAckResend = 0; |
| 52 | }; |
| 53 | struct MessageForResend { |
| 54 | rtc::CopyOnWriteBuffer data; |
| 55 | int64_t lastSent = 0; |
| 56 | }; |
| 57 |
| 58 | bool enoughSpaceInPacket(const rtc::CopyOnWriteBuffer &buffer, size_t amount) const; |
| 59 | size_t packetLimit() const; |
| 60 | size_t fullNotAckedLength() const; |
| 61 | void appendAcksToSend(rtc::CopyOnWriteBuffer &buffer); |
| 62 | void appendAdditionalMessages(rtc::CopyOnWriteBuffer &buffer); |
| 63 | EncryptedPacket encryptPrepared(const rtc::CopyOnWriteBuffer &buffer); |
| 64 | bool registerIncomingCounter(uint32_t incomingCounter); |
| 65 | absl::optional<DecryptedPacket> processPacket(const rtc::Buffer &fullBuffer, uint32_t packetSeq); |
| 66 | absl::optional<DecryptedRawPacket> processRawPacket(const rtc::Buffer &fullBuffer, uint32_t packetSeq); |
| 67 | bool registerSentAck(uint32_t counter, bool firstInPacket); |
| 68 | void ackMyMessage(uint32_t counter); |
| 69 | void sendAckPostponed(uint32_t incomingSeq); |
| 70 | bool haveAdditionalMessages() const; |
| 71 | absl::optional<uint32_t> computeNextSeq(bool messageRequiresAck, bool singleMessagePacket); |
| 72 | void appendReceivedMessage( |
| 73 | absl::optional<DecryptedPacket> &to, |
| 74 | Message &&message, |
| 75 | uint32_t incomingSeq); |
| 76 | void appendReceivedRawMessage( |
| 77 | absl::optional<DecryptedRawPacket> &to, |
| 78 | rtc::CopyOnWriteBuffer &&message, |
| 79 | uint32_t incomingSeq); |
| 80 | absl::optional<EncryptedPacket> prepareForSendingMessageInternal(rtc::CopyOnWriteBuffer &serialized, uint32_t seq, bool messageRequiresAck); |
| 81 |
| 82 | const char *logHeader() const; |
| 83 |
| 84 | static DelayIntervals DelayIntervalsByType(Type type); |
| 85 | static rtc::CopyOnWriteBuffer SerializeEmptyMessageWithSeq(uint32_t seq); |
| 86 |
| 87 | Type _type = Type(); |
| 88 | EncryptionKey _key; |
| 89 | uint32_t _counter = 0; |
| 90 | DelayIntervals _delayIntervals; |
| 91 | std::vector<uint32_t> _largestIncomingCounters; |
| 92 | std::vector<uint32_t> _ackedIncomingCounters; |
| 93 | std::vector<uint32_t> _acksToSendSeqs; |
| 94 | std::vector<uint32_t> _acksSentCounters; |
| 95 | std::vector<MessageForResend> _myNotYetAckedMessages; |
| 96 | std::function<void(int delayMs, int cause)> _requestSendService; |
| 97 | bool _resendTimerActive = false; |
| 98 | bool _sendAcksTimerActive = false; |
| 99 |
| 100 | }; |
| 101 |
| 102 | } // namespace tgcalls |
| 103 |
| 104 | #endif |
| 105 |