2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/net/HttpOutboundConnection.h"
|
|
|
|
#include "td/net/HttpQuery.h"
|
2018-08-16 00:06:53 +02:00
|
|
|
#include "td/net/SslStream.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/buffer.h"
|
2021-11-04 10:46:08 +01:00
|
|
|
#include "td/utils/BufferedFd.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/port/SocketFd.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
std::atomic<int> counter;
|
2019-06-17 18:12:54 +02:00
|
|
|
|
2021-11-06 21:45:02 +01:00
|
|
|
class HttpClient final : public td::HttpOutboundConnection::Callback {
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-11-06 21:45:02 +01:00
|
|
|
td::IPAddress addr;
|
2018-12-31 20:04:05 +01:00
|
|
|
addr.init_ipv4_port("127.0.0.1", 8082).ensure();
|
2021-11-06 21:45:02 +01:00
|
|
|
auto fd = td::SocketFd::open(addr);
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(fd.is_ok()) << fd.error();
|
2021-11-06 21:45:02 +01:00
|
|
|
connection_ = td::create_actor<td::HttpOutboundConnection>(
|
|
|
|
"Connect", td::BufferedFd<td::SocketFd>(fd.move_as_ok()), td::SslStream{}, std::numeric_limits<size_t>::max(),
|
|
|
|
0, 0, td::ActorOwn<td::HttpOutboundConnection::Callback>(actor_id(this)));
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
cnt_ = 100000;
|
|
|
|
counter++;
|
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (--counter == 0) {
|
2021-11-06 21:45:02 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (cnt_-- < 0) {
|
|
|
|
return stop();
|
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
send_closure(connection_, &td::HttpOutboundConnection::write_next, td::BufferSlice("GET / HTTP/1.1\r\n\r\n"));
|
|
|
|
send_closure(connection_, &td::HttpOutboundConnection::write_ok);
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "SEND";
|
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
|
|
|
|
void handle(td::unique_ptr<td::HttpQuery> result) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
|
|
|
|
void on_connection_error(td::Status error) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(ERROR) << "ERROR: " << error;
|
|
|
|
}
|
|
|
|
|
2021-11-06 21:45:02 +01:00
|
|
|
td::ActorOwn<td::HttpOutboundConnection> connection_;
|
2021-11-11 15:39:09 +01:00
|
|
|
int cnt_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
|
2022-09-14 14:06:52 +02:00
|
|
|
auto scheduler = td::make_unique<td::ConcurrentScheduler>(0, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler->create_actor_unsafe<HttpClient>(0, "Client1").release();
|
|
|
|
scheduler->create_actor_unsafe<HttpClient>(0, "Client2").release();
|
|
|
|
scheduler->start();
|
|
|
|
while (scheduler->run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
scheduler->finish();
|
|
|
|
}
|