diff options
author | aunsane <aunsane@gmail.com> | 2018-04-27 21:33:17 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-04-27 21:33:17 +0300 |
commit | e1ec72eab6d00b3ba38e5932bc88920f103b6e4a (patch) | |
tree | 999de2725a83e30fbbf6576200525d4ef0c5fe38 /protocols/Telegram/tdlib/td/benchmark/bench_http_server_cheat.cpp | |
parent | b9ce1d4d98525490ca1a38e2d9fd4f3369adb3e0 (diff) |
Telegram: initial commit
- tdlib moved to telegram dir
Diffstat (limited to 'protocols/Telegram/tdlib/td/benchmark/bench_http_server_cheat.cpp')
-rw-r--r-- | protocols/Telegram/tdlib/td/benchmark/bench_http_server_cheat.cpp | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/protocols/Telegram/tdlib/td/benchmark/bench_http_server_cheat.cpp b/protocols/Telegram/tdlib/td/benchmark/bench_http_server_cheat.cpp new file mode 100644 index 0000000000..da6fbbd713 --- /dev/null +++ b/protocols/Telegram/tdlib/td/benchmark/bench_http_server_cheat.cpp @@ -0,0 +1,138 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// +// 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/HttpHeaderCreator.h" +#include "td/net/HttpInboundConnection.h" +#include "td/net/TcpListener.h" + +#include "td/utils/buffer.h" +#include "td/utils/logging.h" +#include "td/utils/port/Fd.h" +#include "td/utils/port/SocketFd.h" +#include "td/utils/Slice.h" +#include "td/utils/Status.h" + +#include <array> + +namespace td { + +// HttpInboundConnection header +static int cnt = 0; +class HelloWorld : public Actor { + public: + explicit HelloWorld(SocketFd socket_fd) : socket_fd_(std::move(socket_fd)) { + } + + private: + SocketFd socket_fd_; + + std::array<char, 1024> read_buf; + size_t read_new_lines{0}; + + std::string hello_; + std::string write_buf_; + size_t write_pos_{0}; + + void start_up() override { + socket_fd_.get_fd().set_observer(this); + subscribe(socket_fd_.get_fd()); + 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"); + hello_ = hc.finish(content).ok().str(); + } + + void loop() override { + auto status = do_loop(); + if (status.is_error()) { + unsubscribe(socket_fd_.get_fd()); + stop(); + LOG(ERROR) << "CLOSE: " << status; + } + } + Status do_loop() { + TRY_STATUS(read_loop()); + TRY_STATUS(write_loop()); + if (can_close(socket_fd_)) { + return Status::Error("CLOSE"); + } + return Status::OK(); + } + Status write_loop() { + while (can_write(socket_fd_) && write_pos_ < write_buf_.size()) { + TRY_RESULT(written, socket_fd_.write(Slice(write_buf_).substr(write_pos_))); + write_pos_ += written; + if (write_pos_ == write_buf_.size()) { + write_pos_ = 0; + write_buf_.clear(); + } + } + return Status::OK(); + } + Status read_loop() { + while (can_read(socket_fd_)) { + TRY_RESULT(read_size, socket_fd_.read(MutableSlice(read_buf.data(), read_buf.size()))); + for (size_t i = 0; i < read_size; i++) { + if (read_buf[i] == '\n') { + read_new_lines++; + if (read_new_lines == 2) { + read_new_lines = 0; + write_buf_.append(hello_); + } + } + } + } + return Status::OK(); + } +}; +const int N = 0; +class Server : public TcpListener::Callback { + public: + void start_up() override { + listener_ = create_actor<TcpListener>("Listener", 8082, ActorOwn<TcpListener::Callback>(actor_id(this))); + } + void accept(SocketFd fd) override { + LOG(ERROR) << "ACCEPT " << cnt++; + pos_++; + auto scheduler_id = pos_ % (N != 0 ? N : 1) + (N != 0); + create_actor_on_scheduler<HelloWorld>("HttpInboundConnection", scheduler_id, std::move(fd)).release(); + } + void hangup() override { + // may be it should be default?.. + LOG(ERROR) << "hangup.."; + 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(); +} |