2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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/HttpInboundConnection.h"
|
|
|
|
#include "td/net/HttpQuery.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/logging.h"
|
|
|
|
#include "td/utils/port/SocketFd.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
static int cnt = 0;
|
2019-06-17 18:12:54 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class HelloWorld final : public HttpInboundConnection::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void handle(unique_ptr<HttpQuery> query, ActorOwn<HttpInboundConnection> connection) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
// LOG(ERROR) << *query;
|
|
|
|
HttpHeaderCreator hc;
|
|
|
|
Slice content = "hello world";
|
|
|
|
//auto content = BufferSlice("hello world");
|
|
|
|
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);
|
|
|
|
LOG_IF(FATAL, res.is_error()) << res.error();
|
|
|
|
send_closure(connection, &HttpInboundConnection::write_next, BufferSlice(res.ok()));
|
|
|
|
send_closure(connection.release(), &HttpInboundConnection::write_ok);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(ERROR) << "CLOSE " << cnt--;
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const int N = 0;
|
2021-07-04 04:58:54 +02:00
|
|
|
class Server final : public TcpListener::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
listener_ = create_actor<TcpListener>("Listener", 8082, ActorOwn<TcpListener::Callback>(actor_id(this)));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void accept(SocketFd fd) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(ERROR) << "ACCEPT " << cnt++;
|
|
|
|
pos_++;
|
|
|
|
auto scheduler_id = pos_ % (N != 0 ? N : 1) + (N != 0);
|
2021-10-27 19:29:01 +02:00
|
|
|
create_actor_on_scheduler<HttpInboundConnection>("HttpInboundConnection", scheduler_id,
|
|
|
|
BufferedFd<SocketFd>(std::move(fd)), 1024 * 1024, 0, 0,
|
2018-12-31 20:04:05 +01:00
|
|
|
create_actor_on_scheduler<HelloWorld>("HelloWorld", scheduler_id))
|
|
|
|
.release();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
// may be it should be default?..
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(ERROR) << "Hanging up..";
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorOwn<TcpListener> listener_;
|
|
|
|
int pos_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
|
|
|
|
auto scheduler = make_unique<ConcurrentScheduler>();
|
|
|
|
scheduler->init(N);
|
|
|
|
scheduler->create_actor_unsafe<Server>(0, "Server").release();
|
|
|
|
scheduler->start();
|
|
|
|
while (scheduler->run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
scheduler->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
return td::main();
|
|
|
|
}
|