| #include "rtc_base/byte_buffer.h" |
| 6 | #include "rtc_base/time_utils.h" |
| 7 |
| 8 | namespace tgcalls { |
| 9 | namespace { |
| 10 |
| 11 | constexpr auto kSingleMessagePacketSeqBit = (uint32_t(1) << 31); |
| 12 | constexpr auto kMessageRequiresAckSeqBit = (uint32_t(1) << 30); |
| 13 | constexpr auto kMaxAllowedCounter = std::numeric_limits<uint32_t>::max() |
| 14 | & ~kSingleMessagePacketSeqBit |
| 15 | & ~kMessageRequiresAckSeqBit; |
| 16 |
| 17 | static_assert(kMaxAllowedCounter < kSingleMessagePacketSeqBit, "bad"); |
| 18 | static_assert(kMaxAllowedCounter < kMessageRequiresAckSeqBit, "bad"); |
| 19 |
| 20 | constexpr auto kAckSerializedSize = sizeof(uint32_t) + sizeof(uint8_t); |
| 21 | constexpr auto kNotAckedMessagesLimit = 64 * 1024; |
| 22 | constexpr auto kMaxIncomingPacketSize = 128 * 1024; // don't try decrypting more |
| 23 | constexpr auto kKeepIncomingCountersCount = 64; |
| 24 | constexpr auto kMaxFullPacketSize = 1500; // IP_PACKET_SIZE from webrtc. |
| 25 |
| 26 | // Max seen turn_overhead is around 36. |
| 27 | constexpr auto kMaxOuterPacketSize = kMaxFullPacketSize - 48; |
| 28 |
| 29 | constexpr auto kMaxSignalingPacketSize = 16 * 1024; |
| 30 |
| 31 | constexpr auto kServiceCauseAcks = 1; |
| 32 | constexpr auto kServiceCauseResend = 2; |
| 33 |
| 34 | static constexpr uint8_t kAckId = uint8_t(-1); |
| 35 | static constexpr uint8_t kEmptyId = uint8_t(-2); |
| 36 |
| 37 | void AppendSeq(rtc::CopyOnWriteBuffer &buffer, uint32_t seq) { |
| 38 | const auto bytes = rtc::HostToNetwork32(seq); |
| 39 | buffer.AppendData(reinterpret_cast<const char*>(&bytes), sizeof(bytes)); |
| 40 | } |
| 41 |
| 42 | void WriteSeq(void *bytes, uint32_t seq) { |
| 43 | *reinterpret_cast<uint32_t*>(bytes) = rtc::HostToNetwork32(seq); |
| 44 | } |
| 45 |
| 46 | uint32_t ReadSeq(const void *bytes) { |
| 47 | return rtc::NetworkToHost32(*reinterpret_cast<const uint32_t*>(bytes)); |
| 48 | } |
| 49 |
| 50 | uint32_t CounterFromSeq(uint32_t seq) { |
| 51 | return seq & ~kSingleMessagePacketSeqBit & ~kMessageRequiresAckSeqBit; |
| 52 | } |
| 53 |
| 54 | absl::nullopt_t LogError( |
| 55 | const char *message, |
| 56 | const std::string &additional = std::string()) { |
| 57 | RTC_LOG(LS_ERROR) << "ERROR! " << message << additional; |
| 58 | return absl::nullopt; |
| 59 | } |
| 60 |
| 61 | bool ConstTimeIsDifferent(const void *a, const void *b, size_t size) { |
| 62 | auto ca = reinterpret_cast<const char*>(a); |
| 63 | auto cb = reinterpret_cast<const char*>(b); |
| 64 | volatile auto different = false; |
| 65 | for (const auto ce = ca + size; ca != ce; ++ca, ++cb) { |
| 66 | different |= (*ca != *cb); |
| 67 | } |
| 68 | return different; |
| 69 | } |
| 70 |
| 71 | } // namespace |
| 72 |
| 73 | EncryptedConnection::EncryptedConnection( |
| 74 | Type type, |
| 75 | const EncryptionKey &key, |
| 76 | std::function<void(int delayMs, int cause)> requestSendService) : |
| 77 | _type(type), |
| 78 | _key(key), |
| 79 | _delayIntervals(DelayIntervalsByType(type)), |
| 80 | _requestSendService(std::move(requestSendService)) { |
| 81 | assert(_key.value != nullptr); |
| 82 | } |
| 83 |
| 84 | auto EncryptedConnection::prepareForSending(const Message &message) |
| 85 | -> absl::optional<EncryptedPacket> { |
| 86 | const auto messageRequiresAck = absl::visit([](const auto &data) { |
| 87 | return std::decay_t<decltype(data)>::kRequiresAck; |
| 88 | }, message.data); |
| 89 |
| 90 | // If message requires ack, then we can't serialize it as a single |
| 91 | // message packet, because later it may be sent as a part of big packet. |
| 92 | const auto singleMessagePacket = !haveAdditionalMessages() && !messageRequiresAck; |
| 93 | const auto maybeSeq = computeNextSeq(messageRequiresAck, singleMessagePacket); |
| 94 | if (!maybeSeq) { |
| 95 | return absl::nullopt; |
| 96 | } |
| 97 | const auto seq = *maybeSeq; |
| 98 | auto serialized = SerializeMessageWithSeq(message, seq, singleMessagePacket); |
| 99 | if (!enoughSpaceInPacket(serialized, 0)) { |
| 100 | return LogError("Too large packet: ", std::to_string(serialized.size())); |
| 101 | } |
| 102 | const auto notYetAckedCopy = messageRequiresAck |
| 103 | ? serialized |
| 104 | : rtc::CopyOnWriteBuffer(); |
| 105 | if (!messageRequiresAck) { |
| 106 | appendAdditionalMessages(serialized); |
| 107 | return encryptPrepared(serialized); |
| 108 | } |
| 109 | const auto type = uint8_t(serialized.cdata()[4]); |
| 110 | const auto sendEnqueued = !_myNotYetAckedMessages.empty(); |
| 111 | if (sendEnqueued) { |
| 112 | // All requiring ack messages should always be sent in order within |
| 113 | // one packet, starting with the least not-yet-acked one. |
| 114 | // So if we still have those, we send an empty message with all |
| 115 | // requiring ack messages that will fit in correct order. |
| 116 | RTC_LOG(LS_INFO) << logHeader() |
| 117 | << "Enqueue SEND:type" << type << "#" << CounterFromSeq(seq); |
| 118 | } else { |
| 119 | RTC_LOG(LS_INFO) << logHeader() |
| 120 | << "Add SEND:type" << type << "#" << CounterFromSeq(seq); |
| 121 | appendAdditionalMessages(serialized); |
| 122 | } |
| 123 | _myNotYetAckedMessages.push_back({ notYetAckedCopy, rtc::TimeMillis() }); |
| 124 | if (!sendEnqueued) { |
| 125 | return encryptPrepared(serialized); |
| 126 | } |
| 127 | for (auto &queued : _myNotYetAckedMessages) { |
| 128 | queued.lastSent = 0; |
| 129 | } |
| 130 | return prepareForSendingService(0); |
| 131 | } |
| 132 |
| 133 | auto EncryptedConnection::prepareForSendingService(int cause) |
| 134 | -> absl::optional<EncryptedPacket> { |
| 135 | if (cause == kServiceCauseAcks) { |
| 136 | _sendAcksTimerActive = false; |
| 137 | } else if (cause == kServiceCauseResend) { |
| 138 | _resendTimerActive = false; |
| 139 | } |
| 140 | if (!haveAdditionalMessages()) { |
| 141 | return absl::nullopt; |
| 142 | } |
| 143 | const auto messageRequiresAck = false; |
| 144 | const auto singleMessagePacket = false; |
| 145 | const auto seq = computeNextSeq(messageRequiresAck, singleMessagePacket); |
| 146 | if (!seq) { |
| 147 | return absl::nullopt; |
| 148 | } |
| 149 | auto serialized = SerializeEmptyMessageWithSeq(*seq); |
| 150 | assert(enoughSpaceInPacket(serialized, 0)); |
| 151 |
| 152 | RTC_LOG(LS_INFO) << logHeader() |
| 153 | << "SEND:empty#" << CounterFromSeq(*seq); |
| 154 |
| 155 | appendAdditionalMessages(serialized); |
| 156 | return encryptPrepared(serialized); |
| 157 | } |
| 158 |
| 159 | bool EncryptedConnection::haveAdditionalMessages() const { |
| 160 | return !_myNotYetAckedMessages.empty() || !_acksToSendSeqs.empty(); |
| 161 | } |
| 162 |
| 163 | absl::optional<uint32_t> EncryptedConnection::computeNextSeq( |
| 164 | bool messageRequiresAck, |
| 165 | bool singleMessagePacket) { |
| 166 | if (messageRequiresAck && _myNotYetAckedMessages.size() >= kNotAckedMessagesLimit) { |
| 167 | return LogError("Too many not ACKed messages."); |
| 168 | } else if (_counter == kMaxAllowedCounter) { |
| 169 | return LogError("Outgoing packet limit reached."); |
| 170 | } |
| 171 |
| 172 | return (++_counter) |
| 173 | | (singleMessagePacket ? kSingleMessagePacketSeqBit : 0) |
| 174 | | (messageRequiresAck ? kMessageRequiresAckSeqBit : 0); |
| 175 | } |
| 176 |
| 177 | size_t EncryptedConnection::packetLimit() const { |
| 178 | switch (_type) { |
| 179 | case Type::Signaling: |
| 180 | return kMaxSignalingPacketSize; |
| 181 | default: |
| 182 | return kMaxOuterPacketSize; |
| 183 | } |
| 184 | } |
| 185 |
| 186 | bool EncryptedConnection::enoughSpaceInPacket(const rtc::CopyOnWriteBuffer &buffer, size_t amount) const { |
| 187 | const auto limit = packetLimit(); |
| 188 | return (amount < limit) |
| 189 | && (16 + buffer.size() + amount <= limit); |
| 190 | } |
| 191 |
| 192 | void EncryptedConnection::appendAcksToSend(rtc::CopyOnWriteBuffer &buffer) { |
| 193 | auto i = _acksToSendSeqs.begin(); |
| 194 | while ((i != _acksToSendSeqs.end()) |
| 195 | && enoughSpaceInPacket( |
| 196 | buffer, |
| 197 | kAckSerializedSize)) { |
| 198 |
| 199 | RTC_LOG(LS_INFO) << logHeader() |
| 200 | << "Add ACK#" << CounterFromSeq(*i); |
| 201 |
| 202 | AppendSeq(buffer, *i); |
| 203 | buffer.AppendData(&kAckId, 1); |
| 204 | ++i; |
| 205 | } |
| 206 | _acksToSendSeqs.erase(_acksToSendSeqs.begin(), i); |
| 207 | for (const auto seq : _acksToSendSeqs) { |
| 208 | RTC_LOG(LS_INFO) << logHeader() |
| 209 | << "Skip ACK#" << CounterFromSeq(seq) |
| 210 | << " (no space, length: " << kAckSerializedSize << ", already: " << buffer.size() << ")"; |
| 211 | } |
| 212 | } |
| 213 |
| 214 | size_t EncryptedConnection::fullNotAckedLength() const { |
| 215 | assert(_myNotYetAckedMessages.size() < kNotAckedMessagesLimit); |
| 216 |
| 217 | auto result = size_t(); |
| 218 | for (const auto &message : _myNotYetAckedMessages) { |
| 219 | result += message.data.size(); |
| 220 | } |
| 221 | return result; |
| 222 | } |
| 223 |
| 224 | void EncryptedConnection::appendAdditionalMessages(rtc::CopyOnWriteBuffer &buffer) { |
| 225 | appendAcksToSend(buffer); |
| 226 |
| 227 | if (_myNotYetAckedMessages.empty()) { |
| 228 | return; |
| 229 | } |
| 230 |
| 231 | const auto now = rtc::TimeMillis(); |
| 232 | auto someWereNotAdded = false; |
| 233 | for (auto &resending : _myNotYetAckedMessages) { |
| 234 | const auto sent = resending.lastSent; |
| 235 | const auto when = sent |
| 236 | ? (sent + _delayIntervals.minDelayBeforeMessageResend) |
| 237 | : 0; |
| 238 |
| 239 | assert(resending.data.size() >= 5); |
| 240 | const auto counter = CounterFromSeq(ReadSeq(resending.data.data())); |
| 241 | const auto type = uint8_t(resending.data.data()[4]); |
| 242 | if (when > now) { |
| 243 | RTC_LOG(LS_INFO) << logHeader() |
| 244 | << "Skip RESEND:type" << type << "#" << counter |
| 245 | << " (wait " << (when - now) << "ms)."; |
| 246 | break; |
| 247 | } else if (enoughSpaceInPacket(buffer, resending.data.size())) { |
| 248 | RTC_LOG(LS_INFO) << logHeader() |
| 249 | << "Add RESEND:type" << type << "#" << counter; |
| 250 | buffer.AppendData(resending.data); |
| 251 | resending.lastSent = now; |
| 252 | } else { |
| 253 | RTC_LOG(LS_INFO) << logHeader() |
| 254 | << "Skip RESEND:type" << type << "#" << counter |
| 255 | << " (no space, length: " << resending.data.size() << ", already: " << buffer.size() << ")"; |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | if (!_resendTimerActive) { |
| 260 | _resendTimerActive = true; |
| 261 | _requestSendService( |
| 262 | _delayIntervals.maxDelayBeforeMessageResend, |
| 263 | kServiceCauseResend); |
| 264 | } |
| 265 | } |
| 266 |
| 267 | auto EncryptedConnection::encryptPrepared(const rtc::CopyOnWriteBuffer &buffer) |
| 268 | -> EncryptedPacket { |
| 269 | auto result = EncryptedPacket(); |
| 270 | result.counter = CounterFromSeq(ReadSeq(buffer.data())); |
| 271 | result.bytes.resize(16 + buffer.size()); |
| 272 |
| 273 | const auto x = (_key.isOutgoing ? 0 : 8) + (_type == Type::Signaling ? 128 : 0); |
| 274 | const auto key = _key.value->data(); |
| 275 |
| 276 | const auto msgKeyLarge = ConcatSHA256( |
| 277 | MemorySpan{ key + 88 + x, 32 }, |
| 278 | MemorySpan{ buffer.data(), buffer.size() }); |
| 279 | const auto msgKey = result.bytes.data(); |
| 280 | memcpy(msgKey, msgKeyLarge.data() + 8, 16); |
| 281 |
| 282 | auto aesKeyIv = PrepareAesKeyIv(key, msgKey, x); |
| 283 |
| 284 | AesProcessCtr( |
| 285 | MemorySpan{ buffer.data(), buffer.size() }, |
| 286 | result.bytes.data() + 16, |
| 287 | std::move(aesKeyIv)); |
| 288 |
| 289 | return result; |
| 290 | } |
| 291 |
| 292 | bool EncryptedConnection::registerIncomingCounter(uint32_t incomingCounter) { |
| 293 | auto &list = _largestIncomingCounters; |
| 294 |
| 295 | const auto position = std::lower_bound(list.begin(), list.end(), incomingCounter); |
| 296 | const auto largest = list.empty() ? 0 : list.back(); |
| 297 | if (position != list.end() && *position == incomingCounter) { |
| 298 | // The packet is in the list already. |
| 299 | return false; |
| 300 | } else if (incomingCounter + kKeepIncomingCountersCount <= largest) { |
| 301 | // The packet is too old. |
| 302 | return false; |
| 303 | } |
| 304 | const auto eraseTill = std::find_if(list.begin(), list.end(), [&](uint32_t counter) { |
| 305 | return (counter + kKeepIncomingCountersCount > incomingCounter); |
| 306 | }); |
| 307 | const auto eraseCount = eraseTill - list.begin(); |
| 308 | const auto positionIndex = (position - list.begin()) - eraseCount; |
| 309 | list.erase(list.begin(), eraseTill); |
| 310 |
| 311 | assert(positionIndex >= 0 && positionIndex <= list.size()); |
| 312 | list.insert(list.begin() + positionIndex, incomingCounter); |
| 313 | return true; |
| 314 | } |
| 315 |
| 316 | auto EncryptedConnection::handleIncomingPacket(const char *bytes, size_t size) |
| 317 | -> absl::optional<DecryptedPacket> { |
| 318 | if (size < 21 || size > kMaxIncomingPacketSize) { |
| 319 | return LogError("Bad incoming packet size: ", std::to_string(size)); |
| 320 | } |
| 321 |
| 322 | const auto x = (_key.isOutgoing ? 8 : 0) + (_type == Type::Signaling ? 128 : 0); |
| 323 | const auto key = _key.value->data(); |
| 324 | const auto msgKey = reinterpret_cast<const uint8_t*>(bytes); |
| 325 | const auto encryptedData = msgKey + 16; |
| 326 | const auto dataSize = size - 16; |
| 327 |
| 328 | auto aesKeyIv = PrepareAesKeyIv(key, msgKey, x); |
| 329 |
| 330 | auto decryptionBuffer = rtc::Buffer(dataSize); |
| 331 | AesProcessCtr( |
| 332 | MemorySpan{ encryptedData, dataSize }, |
| 333 | decryptionBuffer.data(), |
| 334 | std::move(aesKeyIv)); |
| 335 |
| 336 | const auto msgKeyLarge = ConcatSHA256( |
| 337 | MemorySpan{ key + 88 + x, 32 }, |
| 338 | MemorySpan{ decryptionBuffer.data(), decryptionBuffer.size() }); |
| 339 | if (ConstTimeIsDifferent(msgKeyLarge.data() + 8, msgKey, 16)) { |
| 340 | return LogError("Bad incoming data hash."); |
| 341 | } |
| 342 |
| 343 | const auto incomingSeq = ReadSeq(decryptionBuffer.data()); |
| 344 | const auto incomingCounter = CounterFromSeq(incomingSeq); |
| 345 | if (!registerIncomingCounter(incomingCounter)) { |
| 346 | // We've received that packet already. |
| 347 | return LogError("Already handled packet received.", std::to_string(incomingCounter)); |
| 348 | } |
| 349 | return processPacket(decryptionBuffer, incomingSeq); |
| 350 | } |
| 351 |
| 352 | auto EncryptedConnection::processPacket( |
| 353 | const rtc::Buffer &fullBuffer, |
| 354 | uint32_t packetSeq) |
| 355 | -> absl::optional<DecryptedPacket> { |
| 356 | assert(fullBuffer.size() >= 5); |
| 357 |
| 358 | auto additionalMessage = false; |
| 359 | auto firstMessageRequiringAck = true; |
| 360 | auto newRequiringAckReceived = false; |
| 361 |
| 362 | auto currentSeq = packetSeq; |
| 363 | auto currentCounter = CounterFromSeq(currentSeq); |
| 364 | rtc::ByteBufferReader reader( |
| 365 | reinterpret_cast<const char*>(fullBuffer.data() + 4), // Skip seq. |
| 366 | fullBuffer.size() - 4); |
| 367 |
| 368 | auto result = absl::optional<DecryptedPacket>(); |
| 369 | while (true) { |
| 370 | const auto type = uint8_t(*reader.Data()); |
| 371 | const auto singleMessagePacket = ((currentSeq & kSingleMessagePacketSeqBit) != 0); |
| 372 | if (singleMessagePacket && additionalMessage) { |
| 373 | return LogError("Single message packet bit in not first message."); |
| 374 | } |
| 375 |
| 376 | if (type == kEmptyId) { |
| 377 | if (additionalMessage) { |
| 378 | return LogError("Empty message should be only the first one in the packet."); |
| 379 | } |
| 380 | RTC_LOG(LS_INFO) << logHeader() |
| 381 | << "Got RECV:empty" << "#" << currentCounter; |
| 382 | reader.Consume(1); |
| 383 | } else if (type == kAckId) { |
| 384 | if (!additionalMessage) { |
| 385 | return LogError("Ack message must not be the first one in the packet."); |
| 386 | } |
| 387 | ackMyMessage(currentSeq); |
| 388 | reader.Consume(1); |
| 389 | } else if (auto message = DeserializeMessage(reader, singleMessagePacket)) { |
| 390 | const auto messageRequiresAck = ((currentSeq & kMessageRequiresAckSeqBit) != 0); |
| 391 | const auto skipMessage = messageRequiresAck |
| 392 | ? !registerSentAck(currentCounter, firstMessageRequiringAck) |
| 393 | : (additionalMessage && !registerIncomingCounter(currentCounter)); |
| 394 | if (messageRequiresAck) { |
| 395 | firstMessageRequiringAck = false; |
| 396 | if (!skipMessage) { |
| 397 | newRequiringAckReceived = true; |
| 398 | } |
| 399 | sendAckPostponed(currentSeq); |
| 400 | RTC_LOG(LS_INFO) << logHeader() |
| 401 | << (skipMessage ? "Repeated RECV:type" : "Got RECV:type") << type << "#" << currentCounter; |
| 402 | } |
| 403 | if (!skipMessage) { |
| 404 | appendReceivedMessage(result, std::move(*message), currentSeq); |
| 405 | } |
| 406 | } else { |
| 407 | return LogError("Could not parse message from packet, type: ", std::to_string(type)); |
| 408 | } |
| 409 | if (!reader.Length()) { |
| 410 | break; |
| 411 | } else if (singleMessagePacket) { |
| 412 | return LogError("Single message didn't fill the entire packet."); |
| 413 | } else if (reader.Length() < 5) { |
| 414 | return LogError("Bad remaining data size: ", std::to_string(reader.Length())); |
| 415 | } |
| 416 | const auto success = reader.ReadUInt32(¤tSeq); |
| 417 | assert(success); |
| 418 | currentCounter = CounterFromSeq(currentSeq); |
| 419 |
| 420 | additionalMessage = true; |
| 421 | } |
| 422 |
| 423 | if (!_acksToSendSeqs.empty()) { |
| 424 | if (newRequiringAckReceived) { |
| 425 | _requestSendService(0, 0); |
| 426 | } else if (!_sendAcksTimerActive) { |
| 427 | _sendAcksTimerActive = true; |
| 428 | _requestSendService( |
| 429 | _delayIntervals.maxDelayBeforeAckResend, |
| 430 | kServiceCauseAcks); |
| 431 | } |
| 432 | } |
| 433 |
| 434 | return result; |
| 435 | } |
| 436 |
| 437 | void EncryptedConnection::appendReceivedMessage( |
| 438 | absl::optional<DecryptedPacket> &to, |
| 439 | Message &&message, |
| 440 | uint32_t incomingSeq) { |
| 441 | auto decrypted = DecryptedMessage{ |
| 442 | std::move(message), |
| 443 | CounterFromSeq(incomingSeq) |
| 444 | }; |
| 445 | if (to) { |
| 446 | to->additional.push_back(std::move(decrypted)); |
| 447 | } else { |
| 448 | to = DecryptedPacket{ std::move(decrypted) }; |
| 449 | } |
| 450 | } |
| 451 |
| 452 | const char *EncryptedConnection::logHeader() const { |
| 453 | return (_type == Type::Signaling) ? "(signaling) " : "(transport) "; |
| 454 | } |
| 455 |
| 456 | bool EncryptedConnection::registerSentAck(uint32_t counter, bool firstInPacket) { |
| 457 | auto &list = _acksSentCounters; |
| 458 |
| 459 | const auto position = std::lower_bound(list.begin(), list.end(), counter); |
| 460 | const auto already = (position != list.end()) && (*position == counter); |
| 461 |
| 462 | const auto was = list; |
| 463 | if (firstInPacket) { |
| 464 | list.erase(list.begin(), position); |
| 465 | if (!already) { |
| 466 | list.insert(list.begin(), counter); |
| 467 | } |
| 468 | } else if (!already) { |
| 469 | list.insert(position, counter); |
| 470 | } |
| 471 | return !already; |
| 472 | } |
| 473 |
| 474 | void EncryptedConnection::sendAckPostponed(uint32_t incomingSeq) { |
| 475 | auto &list = _acksToSendSeqs; |
| 476 | const auto already = std::find(list.begin(), list.end(), incomingSeq); |
| 477 | if (already == list.end()) { |
| 478 | list.push_back(incomingSeq); |
| 479 | } |
| 480 | } |
| 481 |
| 482 | void EncryptedConnection::ackMyMessage(uint32_t seq) { |
| 483 | auto type = uint8_t(0); |
| 484 | auto &list = _myNotYetAckedMessages; |
| 485 | for (auto i = list.begin(), e = list.end(); i != e; ++i) { |
| 486 | assert(i->data.size() >= 5); |
| 487 | if (ReadSeq(i->data.cdata()) == seq) { |
| 488 | type = uint8_t(i->data.cdata()[4]); |
| 489 | list.erase(i); |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | RTC_LOG(LS_INFO) << logHeader() |
| 494 | << (type ? "Got ACK:type" + std::to_string(type) + "#" : "Repeated ACK#") |
| 495 | << CounterFromSeq(seq); |
| 496 | } |
| 497 |
| 498 | auto EncryptedConnection::DelayIntervalsByType(Type type) -> DelayIntervals { |
| 499 | auto result = DelayIntervals(); |
| 500 | const auto signaling = (type == Type::Signaling); |
| 501 |
| 502 | // Don't resend faster than min delay even if we have a packet we can attach to. |
| 503 | result.minDelayBeforeMessageResend = signaling ? 3000 : 300; |
| 504 |
| 505 | // When max delay elapsed we resend anyway, in a dedicated packet. |
| 506 | result.maxDelayBeforeMessageResend = signaling ? 5000 : 1000; |
| 507 | result.maxDelayBeforeAckResend = signaling ? 5000 : 1000; |
| 508 |
| 509 | return result; |
| 510 | } |
| 511 |
| 512 | rtc::CopyOnWriteBuffer EncryptedConnection::SerializeEmptyMessageWithSeq(uint32_t seq) { |
| 513 | auto result = rtc::CopyOnWriteBuffer(5); |
| 514 | const auto bytes = result.data(); |
| 515 | WriteSeq(bytes, seq); |
| 516 | bytes[4] = kEmptyId; |
| 517 | return result; |
| 518 | } |
| 519 |
| 520 | } // namespace tgcalls |
| 521 |