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/HttpHeaderCreator.h"
|
|
|
|
#include "td/net/HttpQuery.h"
|
|
|
|
#include "td/net/HttpReader.h"
|
|
|
|
#include "td/net/TcpListener.h"
|
|
|
|
|
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"
|
|
|
|
#include "td/utils/BufferedFd.h"
|
|
|
|
#include "td/utils/logging.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/SocketFd.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2021-11-06 21:45:02 +01:00
|
|
|
class HttpEchoConnection final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-11-06 21:45:02 +01:00
|
|
|
explicit HttpEchoConnection(td::SocketFd fd) : fd_(std::move(fd)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-11-06 21:45:02 +01:00
|
|
|
td::BufferedFd<td::SocketFd> fd_;
|
|
|
|
td::HttpReader reader_;
|
|
|
|
td::HttpQuery query_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-11-06 21:45:02 +01:00
|
|
|
td::Scheduler::subscribe(fd_.get_poll_info().extract_pollable_fd(this));
|
2018-12-31 20:04:05 +01:00
|
|
|
reader_.init(&fd_.input_buffer(), 1024 * 1024, 0);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2021-11-06 21:45:02 +01:00
|
|
|
td::Scheduler::unsubscribe_before_close(fd_.get_poll_info().get_pollable_fd_ref());
|
2020-07-13 10:55:43 +02:00
|
|
|
fd_.close();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void handle_query() {
|
2021-11-06 21:45:02 +01:00
|
|
|
query_ = td::HttpQuery();
|
|
|
|
td::HttpHeaderCreator hc;
|
|
|
|
td::Slice content = "hello world";
|
|
|
|
//auto content = td::BufferSlice("hello world");
|
2018-12-31 20:04:05 +01:00
|
|
|
hc.init_ok();
|
|
|
|
hc.set_keep_alive();
|
|
|
|
hc.set_content_size(content.size());
|
|
|
|
hc.add_header("Server", "TDLib/test");
|
|
|
|
hc.add_header("Date", "Thu Dec 14 01:41:50 2017");
|
|
|
|
hc.add_header("Content-Type:", "text/html");
|
|
|
|
auto res = hc.finish(content);
|
|
|
|
fd_.output_buffer().append(res.ok());
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2020-07-21 17:29:39 +02:00
|
|
|
sync_with_poll(fd_);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto status = [&] {
|
|
|
|
TRY_STATUS(loop_read());
|
|
|
|
TRY_STATUS(loop_write());
|
2021-11-06 21:45:02 +01:00
|
|
|
return td::Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}();
|
2020-07-21 17:29:39 +02:00
|
|
|
if (status.is_error() || can_close_local(fd_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
td::Status loop_read() {
|
2020-07-21 17:29:39 +02:00
|
|
|
TRY_STATUS(fd_.flush_read());
|
2018-12-31 20:04:05 +01:00
|
|
|
while (true) {
|
|
|
|
TRY_RESULT(need, reader_.read_next(&query_));
|
|
|
|
if (need == 0) {
|
|
|
|
handle_query();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
return td::Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
td::Status loop_write() {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(fd_.flush_write());
|
2021-11-06 21:45:02 +01:00
|
|
|
return td::Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-13 10:55:43 +02:00
|
|
|
const int N = 8;
|
2021-11-06 21:45:02 +01:00
|
|
|
class Server final : public td::TcpListener::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-11-06 21:45:02 +01:00
|
|
|
listener_ =
|
|
|
|
td::create_actor<td::TcpListener>("Listener", 8082, td::ActorOwn<td::TcpListener::Callback>(actor_id(this)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-11-06 21:45:02 +01:00
|
|
|
void accept(td::SocketFd fd) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
pos_++;
|
|
|
|
auto scheduler_id = pos_ % (N != 0 ? N : 1) + (N != 0);
|
2021-11-06 21:45:02 +01:00
|
|
|
td::create_actor_on_scheduler<HttpEchoConnection>("HttpEchoConnection", scheduler_id, std::move(fd)).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(ERROR) << "Hanging up..";
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-11-06 21:45:02 +01:00
|
|
|
td::ActorOwn<td::TcpListener> listener_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int pos_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
|
2021-11-06 21:45:02 +01:00
|
|
|
auto scheduler = td::make_unique<td::ConcurrentScheduler>();
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler->init(N);
|
|
|
|
scheduler->create_actor_unsafe<Server>(0, "Server").release();
|
|
|
|
scheduler->start();
|
|
|
|
while (scheduler->run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
scheduler->finish();
|
|
|
|
return 0;
|
|
|
|
}
|