forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPeerState.h
138 lines (117 loc) · 3.88 KB
/
PeerState.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#pragma once
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/optional.h"
#include "td/actor/actor.h"
#include "TorrentInfo.h"
#include "SpeedLimiter.h"
#include <map>
#include <atomic>
namespace ton {
using PeerId = td::uint64;
using PartId = td::uint32;
// Concurrent buffer for messages with one writer and one reader
// Reader reads all existing messages at once
// TODO: Use some better algorithm here, or maybe a concurrent queue
template <typename T>
class MessageBuffer {
public:
MessageBuffer() = default;
MessageBuffer(const MessageBuffer<T>&) = delete;
MessageBuffer& operator=(const MessageBuffer<T>&) = delete;
~MessageBuffer() {
delete ptr_.load();
}
void add_element(T x) {
std::vector<T>* vec = ptr_.exchange(nullptr);
if (vec == nullptr) {
vec = new std::vector<T>();
}
vec->push_back(std::move(x));
CHECK(ptr_.exchange(vec) == nullptr);
}
void add_elements(std::vector<T> elements) {
if (elements.empty()) {
return;
}
std::vector<T>* vec = ptr_.exchange(nullptr);
if (vec == nullptr) {
vec = new std::vector<T>(std::move(elements));
} else {
for (auto& x : elements) {
vec->push_back(std::move(x));
}
}
CHECK(ptr_.exchange(vec) == nullptr);
}
std::vector<T> read() {
std::vector<T>* vec = ptr_.exchange(nullptr);
std::vector<T> result;
if (vec != nullptr) {
result = std::move(*vec);
delete vec;
}
return result;
}
private:
std::atomic<std::vector<T>*> ptr_{nullptr};
};
struct PeerState {
explicit PeerState(td::actor::ActorId<> node) : node(std::move(node)) {
}
struct State {
bool will_upload;
bool want_download;
auto key() const {
return std::tie(will_upload, want_download);
}
bool operator==(const State &other) const {
return key() == other.key();
}
};
// Thread-safe fields
std::atomic<State> node_state_{State{false, false}};
std::atomic_bool peer_state_ready_{false};
std::atomic<State> peer_state_{State{false, false}};
std::atomic_bool peer_online_{false};
std::atomic_bool peer_online_set_{false};
SpeedLimiters speed_limiters_;
struct Part {
td::BufferSlice proof;
td::BufferSlice data;
};
std::set<PartId> node_queries_active_; // Node only
MessageBuffer<PartId> node_queries_; // Node -> Peer
MessageBuffer<std::pair<PartId, td::Result<Part>>> node_queries_results_; // Peer -> Node
std::set<PartId> peer_queries_active_; // Peer only
MessageBuffer<PartId> peer_queries_; // Peer -> Node
MessageBuffer<std::pair<PartId, td::Result<Part>>> peer_queries_results_; // Node -> Peer
// Peer -> Node
MessageBuffer<PartId> peer_ready_parts_;
// Node -> Peer
MessageBuffer<PartId> node_ready_parts_;
// Node -> Peer
std::atomic_bool torrent_info_ready_{false};
std::shared_ptr<TorrentInfo> torrent_info_;
std::function<void(td::BufferSlice)> torrent_info_response_callback_;
const td::actor::ActorId<> node;
std::atomic_bool peer_ready_{false};
td::actor::ActorId<> peer;
void notify_node();
void notify_peer();
};
} // namespace ton