]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/demos/tutorial_examples.cc
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2020 ScyllaDB.
24 #include <seastar/core/seastar.hh>
25 #include <seastar/core/reactor.hh>
26 #include <seastar/core/future-util.hh>
27 #include <seastar/net/api.hh>
29 seastar::future
<> service_loop() {
30 return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234})),
32 return seastar::keep_doing([&listener
] () {
33 return listener
.accept().then(
34 [] (seastar::accept_result res
) {
35 std::cout
<< "Accepted connection from " << res
.remote_address
<< "\n";
41 const char* canned_response
= "Seastar is the future!\n";
43 seastar::future
<> service_loop_2() {
44 seastar::listen_options lo
;
45 lo
.reuse_address
= true;
46 return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234}), lo
),
48 return seastar::keep_doing([&listener
] () {
49 return listener
.accept().then(
50 [] (seastar::accept_result res
) {
51 auto s
= std::move(res
.connection
);
52 auto out
= s
.output();
53 return seastar::do_with(std::move(s
), std::move(out
),
54 [] (auto& s
, auto& out
) {
55 return out
.write(canned_response
).then([&out
] {
64 seastar::future
<> handle_connection_3(seastar::connected_socket s
,
65 seastar::socket_address a
) {
66 auto out
= s
.output();
68 return do_with(std::move(s
), std::move(out
), std::move(in
),
69 [] (auto& s
, auto& out
, auto& in
) {
70 return seastar::repeat([&out
, &in
] {
71 return in
.read().then([&out
] (auto buf
) {
73 return out
.write(std::move(buf
)).then([&out
] {
76 return seastar::stop_iteration::no
;
79 return seastar::make_ready_future
<seastar::stop_iteration
>(
80 seastar::stop_iteration::yes
);
89 seastar::future
<> service_loop_3() {
90 seastar::listen_options lo
;
91 lo
.reuse_address
= true;
92 return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234}), lo
),
94 return seastar::keep_doing([&listener
] () {
95 return listener
.accept().then(
96 [] (seastar::accept_result res
) {
97 // Note we ignore, not return, the future returned by
98 // handle_connection(), so we do not wait for one
99 // connection to be handled before accepting the next one.
100 (void)handle_connection_3(std::move(res
.connection
), std::move(res
.remote_address
)).handle_exception(
101 [] (std::exception_ptr ep
) {
102 fmt::print(stderr
, "Could not handle connection: {}\n", ep
);
109 #include <seastar/core/app-template.hh>
111 int main(int ac
, char** av
) {
112 seastar::app_template app
;
113 return app
.run(ac
, av
, [] {
114 std::cout
<< "This is the tutorial examples demo. It is not running anything but rather makes sure the tutorial examples compile" << std::endl
;
115 return seastar::make_ready_future
<>();