forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adnl-ext-client.hpp
191 lines (163 loc) · 6.56 KB
/
adnl-ext-client.hpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
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 "auto/tl/lite_api.h"
#include "adnl-ext-connection.hpp"
#include "tl-utils/lite-utils.hpp"
#include "td/utils/Random.h"
#include "adnl-query.h"
#include "keys/encryptor.h"
#include "adnl-ext-client.h"
namespace ton {
namespace adnl {
class AdnlExtClientImpl;
class AdnlOutboundConnection : public AdnlExtConnection {
private:
AdnlNodeIdFull dst_;
PrivateKey local_id_;
td::actor::ActorId<AdnlExtClientImpl> ext_client_;
td::SecureString nonce_;
bool authorization_complete_ = false;
public:
AdnlOutboundConnection(td::SocketFd fd, std::unique_ptr<AdnlExtConnection::Callback> callback, AdnlNodeIdFull dst,
td::actor::ActorId<AdnlExtClientImpl> ext_client)
: AdnlExtConnection(std::move(fd), std::move(callback), true), dst_(std::move(dst)), ext_client_(ext_client) {
}
AdnlOutboundConnection(td::SocketFd fd, std::unique_ptr<AdnlExtConnection::Callback> callback, AdnlNodeIdFull dst,
PrivateKey local_id, td::actor::ActorId<AdnlExtClientImpl> ext_client)
: AdnlExtConnection(std::move(fd), std::move(callback), true)
, dst_(std::move(dst))
, local_id_(local_id)
, ext_client_(ext_client) {
}
td::Status process_packet(td::BufferSlice data) override;
td::Status process_init_packet(td::BufferSlice data) override {
UNREACHABLE();
}
td::Status process_custom_packet(td::BufferSlice &data, bool &processed) override;
void start_up() override;
bool authorized() const override {
return local_id_.empty() ? true : authorization_complete_;
}
};
class AdnlExtClientImpl : public AdnlExtClient {
public:
AdnlExtClientImpl(AdnlNodeIdFull dst_id, td::IPAddress dst_addr, std::unique_ptr<Callback> callback)
: dst_(std::move(dst_id)), dst_addr_(dst_addr), callback_(std::move(callback)) {
}
AdnlExtClientImpl(AdnlNodeIdFull dst_id, PrivateKey local_id, td::IPAddress dst_addr,
std::unique_ptr<Callback> callback)
: dst_(std::move(dst_id)), local_id_(local_id), dst_addr_(dst_addr), callback_(std::move(callback)) {
}
void start_up() override {
alarm();
}
void conn_stopped(td::actor::ActorId<AdnlExtConnection> conn) {
if (!conn_.empty() && conn_.get() == conn) {
callback_->on_stop_ready();
conn_ = {};
alarm_timestamp() = next_create_at_;
try_stop();
}
}
void conn_ready(td::actor::ActorId<AdnlExtConnection> conn) {
if (!conn_.empty() && conn_.get() == conn) {
callback_->on_ready();
}
}
void check_ready(td::Promise<td::Unit> promise) override;
void send_query(std::string name, td::BufferSlice data, td::Timestamp timeout,
td::Promise<td::BufferSlice> promise) override {
auto P = [SelfId = actor_id(this)](AdnlQueryId id) {
td::actor::send_closure(SelfId, &AdnlExtClientImpl::destroy_query, id);
};
auto q_id = generate_next_query_id();
out_queries_.emplace(q_id, AdnlQuery::create(std::move(promise), std::move(P), name, timeout, q_id));
if (!conn_.empty()) {
auto obj = create_tl_object<lite_api::adnl_message_query>(q_id, std::move(data));
td::actor::send_closure(conn_, &AdnlOutboundConnection::send, serialize_tl_object(obj, true));
}
}
void destroy_query(AdnlQueryId id) {
out_queries_.erase(id);
try_stop();
}
void answer_query(AdnlQueryId id, td::BufferSlice data) {
auto it = out_queries_.find(id);
if (it != out_queries_.end()) {
td::actor::send_closure(it->second, &AdnlQuery::result, std::move(data));
}
}
void alarm() override;
void hangup() override;
AdnlQueryId generate_next_query_id() {
while (true) {
AdnlQueryId q_id = AdnlQuery::random_query_id();
if (out_queries_.count(q_id) == 0) {
return q_id;
}
}
}
private:
AdnlNodeIdFull dst_;
PrivateKey local_id_;
td::IPAddress dst_addr_;
std::unique_ptr<Callback> callback_;
td::actor::ActorOwn<AdnlOutboundConnection> conn_;
td::Timestamp next_create_at_ = td::Timestamp::now_cached();
std::map<AdnlQueryId, td::actor::ActorId<AdnlQuery>> out_queries_;
bool is_closing_{false};
td::uint32 ref_cnt_{1};
void try_stop();
};
class AdnlExtMultiClientImpl : public AdnlExtMultiClient {
public:
AdnlExtMultiClientImpl(std::vector<std::pair<AdnlNodeIdFull, td::IPAddress>> ids,
std::unique_ptr<AdnlExtClient::Callback> callback)
: ids_(std::move(ids)), callback_(std::move(callback)) {
}
void start_up() override;
void add_server(AdnlNodeIdFull dst, td::IPAddress dst_addr, td::Promise<td::Unit> promise) override;
void del_server(td::IPAddress dst_addr, td::Promise<td::Unit> promise) override;
void check_ready(td::Promise<td::Unit> promise) override {
if (total_ready_ > 0) {
promise.set_value(td::Unit());
} else {
promise.set_error(td::Status::Error(ErrorCode::notready, "conn not ready"));
}
}
void send_query(std::string name, td::BufferSlice data, td::Timestamp timeout,
td::Promise<td::BufferSlice> promise) override;
void client_ready(td::uint32 idx, bool value);
private:
std::unique_ptr<Callback> make_callback(td::uint32 g);
struct Client {
Client(td::actor::ActorOwn<AdnlExtClient> client, AdnlNodeIdFull pubkey, td::IPAddress addr, td::uint32 generation)
: client(std::move(client)), pubkey(std::move(pubkey)), addr(addr), generation(generation), ready(false) {
}
td::actor::ActorOwn<AdnlExtClient> client;
AdnlNodeIdFull pubkey;
td::IPAddress addr;
td::uint32 generation;
bool ready = false;
};
td::uint32 total_ready_ = 0;
td::uint32 generation_ = 0;
std::map<td::uint32, std::unique_ptr<Client>> clients_;
std::vector<std::pair<AdnlNodeIdFull, td::IPAddress>> ids_;
std::unique_ptr<AdnlExtClient::Callback> callback_;
};
} // namespace adnl
} // namespace ton