Ton

Форк
0
/
InboundTransfer.cpp 
77 строк · 2.6 Кб
1
/*
2
    This file is part of TON Blockchain Library.
3

4
    TON Blockchain Library is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU Lesser General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
7
    (at your option) any later version.
8

9
    TON Blockchain Library is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU Lesser General Public License for more details.
13

14
    You should have received a copy of the GNU Lesser General Public License
15
    along with TON Blockchain Library.  If not, see <http://www.gnu.org/licenses/>.
16

17
    Copyright 2017-2020 Telegram Systems LLP
18
*/
19

20
#include "InboundTransfer.h"
21

22
#include "common/errorcode.h"
23

24
namespace ton {
25
namespace rldp2 {
26
size_t InboundTransfer::total_size() const {
27
  return data_.size();
28
}
29

30
std::map<td::uint32, InboundTransfer::Part> &InboundTransfer::parts() {
31
  return parts_;
32
}
33

34
bool InboundTransfer::is_part_completed(td::uint32 part_i) {
35
  return parts_.count(part_i) == 0 && part_i < next_part_;
36
}
37

38
td::Result<InboundTransfer::Part *> InboundTransfer::get_part(td::uint32 part_i, const ton::fec::FecType &fec_type) {
39
  auto it = parts_.find(part_i);
40
  if (it != parts_.end()) {
41
    return &it->second;
42
  }
43
  //TODO: pass offset off and process even newer parts.
44
  //LOG_CHECK(next_part_ >= part_i) << next_part_ << " >= " << part_i;
45
  if (next_part_ == part_i && parts_.size() < 20) {
46
    auto offset = offset_;
47
    offset_ += fec_type.size();
48
    if (offset_ > total_size()) {
49
      return td::Status::Error(ErrorCode::protoviolation,
50
                               PSTRING() << "too big part: offset=" << offset_ << " total_size=" << total_size()
51
                                         << " total_size=" << fec_type.size() << " part=" << part_i);
52
    }
53

54
    auto decoder = fec_type.create_decoder().move_as_ok();
55
    auto it = parts_.emplace(part_i, Part{std::move(decoder), RldpReceiver(RldpSender::Config()), offset});
56
    next_part_++;
57
    return &it.first->second;
58
  }
59
  return nullptr;
60
}
61

62
void InboundTransfer::finish_part(td::uint32 part_i, td::Slice data) {
63
  auto it = parts_.find(part_i);
64
  CHECK(it != parts_.end());
65
  data_.as_slice().substr(it->second.offset).copy_from(data);
66
  parts_.erase(it);
67
}
68

69
td::optional<td::Result<td::BufferSlice>> InboundTransfer::try_finish() {
70
  if (parts_.empty() && offset_ == data_.size()) {
71
    return std::move(data_);
72
  }
73
  return {};
74
}
75

76
}  // namespace rldp2
77
}  // namespace ton
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.