2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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/actor/actor.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#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>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
std::atomic<int> counter;
|
2019-06-17 18:12:54 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class HttpClient : public HttpOutboundConnection::Callback {
|
|
|
|
void start_up() override {
|
|
|
|
IPAddress addr;
|
|
|
|
addr.init_ipv4_port("127.0.0.1", 8082).ensure();
|
|
|
|
auto fd = SocketFd::open(addr);
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(fd.is_ok()) << fd.error();
|
2018-08-15 14:41:42 +02:00
|
|
|
connection_ = create_actor<HttpOutboundConnection>("Connect", fd.move_as_ok(), SslStream{},
|
|
|
|
std::numeric_limits<size_t>::max(), 0, 0,
|
|
|
|
ActorOwn<HttpOutboundConnection::Callback>(actor_id(this)));
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
cnt_ = 100000;
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
void tear_down() override {
|
|
|
|
if (--counter == 0) {
|
|
|
|
Scheduler::instance()->finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void loop() override {
|
|
|
|
if (cnt_-- < 0) {
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
send_closure(connection_, &HttpOutboundConnection::write_next, BufferSlice("GET / HTTP/1.1\r\n\r\n"));
|
|
|
|
send_closure(connection_, &HttpOutboundConnection::write_ok);
|
|
|
|
LOG(INFO) << "SEND";
|
|
|
|
}
|
2019-06-17 18:12:54 +02:00
|
|
|
void handle(unique_ptr<HttpQuery> result) override {
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
void on_connection_error(Status error) override {
|
|
|
|
LOG(ERROR) << "ERROR: " << error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActorOwn<HttpOutboundConnection> connection_;
|
|
|
|
int cnt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
|
|
|
|
auto scheduler = make_unique<ConcurrentScheduler>();
|
|
|
|
scheduler->init(0);
|
|
|
|
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();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
return td::main();
|
|
|
|
}
|