diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/seastar/demos/tutorial_examples.cc | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/seastar/demos/tutorial_examples.cc | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/seastar/demos/tutorial_examples.cc b/src/seastar/demos/tutorial_examples.cc new file mode 100644 index 000000000..0f67f5fd8 --- /dev/null +++ b/src/seastar/demos/tutorial_examples.cc @@ -0,0 +1,117 @@ +/* + * This file is open source software, licensed to you under the terms + * of the Apache License, Version 2.0 (the "License"). See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. You may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Copyright (C) 2020 ScyllaDB. + */ + +#include <iostream> + +#include <seastar/core/seastar.hh> +#include <seastar/core/reactor.hh> +#include <seastar/core/future-util.hh> +#include <seastar/net/api.hh> + +seastar::future<> service_loop() { + return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234})), + [] (auto& listener) { + return seastar::keep_doing([&listener] () { + return listener.accept().then( + [] (seastar::accept_result res) { + std::cout << "Accepted connection from " << res.remote_address << "\n"; + }); + }); + }); +} + +const char* canned_response = "Seastar is the future!\n"; + +seastar::future<> service_loop_2() { + seastar::listen_options lo; + lo.reuse_address = true; + return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234}), lo), + [] (auto& listener) { + return seastar::keep_doing([&listener] () { + return listener.accept().then( + [] (seastar::accept_result res) { + auto s = std::move(res.connection); + auto out = s.output(); + return seastar::do_with(std::move(s), std::move(out), + [] (auto& s, auto& out) { + return out.write(canned_response).then([&out] { + return out.close(); + }); + }); + }); + }); + }); +} + +seastar::future<> handle_connection_3(seastar::connected_socket s, + seastar::socket_address a) { + auto out = s.output(); + auto in = s.input(); + return do_with(std::move(s), std::move(out), std::move(in), + [] (auto& s, auto& out, auto& in) { + return seastar::repeat([&out, &in] { + return in.read().then([&out] (auto buf) { + if (buf) { + return out.write(std::move(buf)).then([&out] { + return out.flush(); + }).then([] { + return seastar::stop_iteration::no; + }); + } else { + return seastar::make_ready_future<seastar::stop_iteration>( + seastar::stop_iteration::yes); + } + }); + }).then([&out] { + return out.close(); + }); + }); +} + +seastar::future<> service_loop_3() { + seastar::listen_options lo; + lo.reuse_address = true; + return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234}), lo), + [] (auto& listener) { + return seastar::keep_doing([&listener] () { + return listener.accept().then( + [] (seastar::accept_result res) { + // Note we ignore, not return, the future returned by + // handle_connection(), so we do not wait for one + // connection to be handled before accepting the next one. + (void)handle_connection_3(std::move(res.connection), std::move(res.remote_address)).handle_exception( + [] (std::exception_ptr ep) { + fmt::print(stderr, "Could not handle connection: {}\n", ep); + }); + }); + }); + }); +} + +#include <seastar/core/app-template.hh> + +int main(int ac, char** av) { + seastar::app_template app; + return app.run(ac, av, [] { + std::cout << "This is the tutorial examples demo. It is not running anything but rather makes sure the tutorial examples compile" << std::endl; + return seastar::make_ready_future<>(); + }); +} |