]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | /* |
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. | |
6 | * | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
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 | |
16 | * under the License. | |
17 | */ | |
18 | /* | |
19 | * Copyright (C) 2019 Elazar Leibovich | |
20 | */ | |
21 | ||
22 | #include <seastar/core/reactor.hh> | |
f67539c2 | 23 | #include <seastar/core/seastar.hh> |
9f95a23c TL |
24 | #include <seastar/core/app-template.hh> |
25 | #include <seastar/core/print.hh> | |
26 | #include <seastar/core/memory.hh> | |
27 | #include <seastar/util/std-compat.hh> | |
28 | ||
29 | #include <seastar/net/posix-stack.hh> | |
30 | ||
9f95a23c TL |
31 | using namespace seastar; |
32 | ||
33 | future<> handle_connection(connected_socket s) { | |
34 | auto in = s.input(); | |
35 | auto out = s.output(); | |
36 | return do_with(std::move(in), std::move(out), [](auto& in, auto& out) { | |
37 | return do_until([&in]() { return in.eof(); }, | |
38 | [&in, &out] { | |
39 | return in.read().then([&out](auto buf) { | |
40 | return out.write(std::move(buf)).then([&out]() { return out.close(); }); | |
41 | }); | |
42 | }); | |
43 | }); | |
44 | } | |
45 | ||
46 | future<> echo_server_loop() { | |
47 | return do_with( | |
f67539c2 | 48 | server_socket(listen(make_ipv4_address({1234}), listen_options{.reuse_address = true})), [](auto& listener) { |
9f95a23c TL |
49 | // Connect asynchronously in background. |
50 | (void)connect(make_ipv4_address({"127.0.0.1", 1234})).then([](connected_socket&& socket) { | |
51 | socket.shutdown_output(); | |
52 | }); | |
53 | return listener.accept().then( | |
54 | [](accept_result ar) { | |
55 | connected_socket s = std::move(ar.connection); | |
56 | return handle_connection(std::move(s)); | |
57 | }).then([l = std::move(listener)]() mutable { return l.abort_accept(); }); | |
58 | }); | |
59 | } | |
60 | ||
f67539c2 | 61 | class my_malloc_allocator : public std::pmr::memory_resource { |
9f95a23c TL |
62 | public: |
63 | int allocs; | |
64 | int frees; | |
65 | void* do_allocate(std::size_t bytes, std::size_t alignment) override { allocs++; return malloc(bytes); } | |
66 | void do_deallocate(void *ptr, std::size_t bytes, std::size_t alignment) override { frees++; return free(ptr); } | |
f67539c2 | 67 | virtual bool do_is_equal(const std::pmr::memory_resource& __other) const noexcept override { abort(); } |
9f95a23c TL |
68 | }; |
69 | ||
70 | my_malloc_allocator malloc_allocator; | |
f67539c2 | 71 | std::pmr::polymorphic_allocator<char> allocator{&malloc_allocator}; |
9f95a23c TL |
72 | |
73 | int main(int ac, char** av) { | |
74 | register_network_stack("posix", boost::program_options::options_description(), | |
75 | [](boost::program_options::variables_map ops) { | |
76 | return smp::main_thread() ? net::posix_network_stack::create(ops, &allocator) | |
77 | : net::posix_ap_network_stack::create(ops); | |
78 | }, true); | |
79 | return app_template().run_deprecated(ac, av, [] { | |
80 | return echo_server_loop().finally([](){ engine().exit((malloc_allocator.allocs == malloc_allocator.frees) ? 0 : 1); }); | |
81 | }); | |
82 | } |