forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-client.hpp
118 lines (95 loc) · 3.36 KB
/
http-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
/*
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 2019-2020 Telegram Systems LLP
*/
#include "http-client.h"
#include "http-outbound-connection.h"
#include "td/utils/Random.h"
namespace ton {
namespace http {
class HttpClientImpl : public HttpClient {
public:
HttpClientImpl(std::string domain, td::IPAddress addr, std::shared_ptr<Callback> callback)
: domain_(std::move(domain)), addr_(addr), callback_(std::move(callback)) {
}
void start_up() override {
create_connection();
}
void check_ready(td::Promise<td::Unit> promise) override {
if (ready_) {
promise.set_value(td::Unit());
} else {
promise.set_error(td::Status::Error(ErrorCode::notready, "connection not ready"));
}
}
void client_ready(bool value) {
if (ready_ == value) {
return;
}
ready_ = value;
if (ready_) {
callback_->on_ready();
} else {
callback_->on_stop_ready();
conn_.reset();
if (next_create_at_.is_in_past()) {
create_connection();
} else {
alarm_timestamp().relax(next_create_at_);
}
}
}
void alarm() override {
create_connection();
}
void send_request(
std::unique_ptr<HttpRequest> request, std::shared_ptr<HttpPayload> payload, td::Timestamp timeout,
td::Promise<std::pair<std::unique_ptr<HttpResponse>, std::shared_ptr<HttpPayload>>> promise) override;
void create_connection();
private:
bool ready_ = false;
std::string domain_;
td::IPAddress addr_;
td::Timestamp next_create_at_;
std::shared_ptr<Callback> callback_;
td::actor::ActorOwn<HttpOutboundConnection> conn_;
};
class HttpMultiClientImpl : public HttpClient {
public:
HttpMultiClientImpl(std::string domain, td::IPAddress addr, td::uint32 max_connections,
td::uint32 max_requests_per_connect, std::shared_ptr<Callback> callback)
: domain_(std::move(domain))
, addr_(addr)
, max_connections_(max_connections)
, max_requests_per_connect_(max_requests_per_connect)
, callback_(std::move(callback)) {
}
void start_up() override {
callback_->on_ready();
}
void check_ready(td::Promise<td::Unit> promise) override {
promise.set_value(td::Unit());
}
void send_request(
std::unique_ptr<HttpRequest> request, std::shared_ptr<HttpPayload> payload, td::Timestamp timeout,
td::Promise<std::pair<std::unique_ptr<HttpResponse>, std::shared_ptr<HttpPayload>>> promise) override;
private:
std::string domain_;
td::IPAddress addr_;
size_t max_connections_;
td::uint32 max_requests_per_connect_;
td::Timestamp next_create_at_;
std::shared_ptr<Callback> callback_;
};
} // namespace http
} // namespace ton