Skip to content

Commit

Permalink
Reformat code base
Browse files Browse the repository at this point in the history
  • Loading branch information
pandazxx committed Mar 2, 2025
1 parent 63ec68e commit 86d69f7
Show file tree
Hide file tree
Showing 36 changed files with 1,457 additions and 2,059 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@
*.app

build
.cache
20 changes: 9 additions & 11 deletions examples/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ namespace net = ::beman::net;

// ----------------------------------------------------------------------------

namespace { auto use(auto&&...) -> void {} }
namespace {
auto use(auto&&...) -> void {}
} // namespace

auto main() -> int
{
auto main() -> int {
using namespace std::chrono_literals;
using on_exit = std::unique_ptr<char const, decltype([](auto m){ std::cout << m << "\n";})>;
using on_exit = std::unique_ptr<const char, decltype([](auto m) { std::cout << m << "\n"; })>;
net::io_context context;
demo::scope scope;

Expand All @@ -34,13 +35,10 @@ auto main() -> int
//| ex::upon_stopped([]{ std::cout << "5s timer got cancelled\n"; })
);

auto stop = [&scope, &context]{
scope.spawn(
ex::schedule(context.get_scheduler())
| ex::then([]{ std::cout << "sending stop\n"; })
| ex::then([&scope]{ scope.stop(); })
| ex::then([]{ std::cout << "task sending stop signal\n"; })
);
auto stop = [&scope, &context] {
scope.spawn(ex::schedule(context.get_scheduler()) | ex::then([] { std::cout << "sending stop\n"; }) |
ex::then([&scope] { scope.stop(); }) |
ex::then([] { std::cout << "task sending stop signal\n"; }));
};

scope.spawn(std::invoke(
Expand Down
72 changes: 30 additions & 42 deletions examples/cppcon-2024.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,72 +26,60 @@ std::unordered_map<std::string, std::string> files{
{"/logo.png", "examples/data/logo.png"},
};

auto process(auto& stream, auto const& request) -> demo::task<>
{
auto process(auto& stream, const auto& request) -> demo::task<> {
std::cout << "request=" << request << "\n";
std::string method, url, version;
std::string body;
std::string method, url, version;
std::string body;
std::ostringstream out;
if (std::istringstream(request) >> method >> url >> version
&& files.contains(url))
{
std::cout << "url=" << url << "\n";
if (std::istringstream(request) >> method >> url >> version && files.contains(url)) {
std::cout << "url=" << url << "\n";
out << std::ifstream(files[url]).rdbuf();
body = out.str();
out.str({});
}

out << "HTTP/1.1 " << (body.empty()? "404 not found": "200 found") << "\r\n"
out << "HTTP/1.1 " << (body.empty() ? "404 not found" : "200 found") << "\r\n"
<< "Content-Length: " << body.size() << "\r\n"
<< "\r\n"
<< body;
auto response{out.str()};
co_await net::async_send(stream, net::buffer(response));
}

auto timeout(auto scheduler, auto duration, auto sender)
{
return demo::when_any(
std::move(sender),
net::resume_after(scheduler, duration)
| demo::into_error([]{ return std::error_code(demo::timeout, demo::category()); })
);
auto timeout(auto scheduler, auto duration, auto sender) {
return demo::when_any(std::move(sender), net::resume_after(scheduler, duration) | demo::into_error([] {
return std::error_code(demo::timeout, demo::category());
}));
}

auto make_client_handler(auto scheduler, auto stream) -> demo::task<>
{
char buffer[16];
auto make_client_handler(auto scheduler, auto stream) -> demo::task<> {
char buffer[16];
std::string request;
while (true)
try {
if (auto n = co_await timeout(scheduler, 2s, net::async_receive(stream, net::buffer(buffer)))) {
std::string_view data(buffer, n);
request += data;
if (request.find("\r\n\r\n") != request.npos)
{
co_await process(stream, request);
break;
}
}
else {
//std::cout << "ERROR (via expected): " << std::get<0>(n.error()).message() << "\n";
break;
}
}
catch (std::variant<std::error_code> const& ex) {
std::cout << "ERROR: " << std::get<0>(ex).message() << "\n";
break;
}

try {
if (auto n = co_await timeout(scheduler, 2s, net::async_receive(stream, net::buffer(buffer)))) {
std::string_view data(buffer, n);
request += data;
if (request.find("\r\n\r\n") != request.npos) {
co_await process(stream, request);
break;
}
} else {
// std::cout << "ERROR (via expected): " << std::get<0>(n.error()).message() << "\n";
break;
}
} catch (const std::variant<std::error_code>& ex) {
std::cout << "ERROR: " << std::get<0>(ex).message() << "\n";
break;
}

co_return;
}

auto main() -> int
{
auto main() -> int {
demo::scope scope;

net::io_context context;
net::io_context context;
net::ip::tcp::endpoint endpoint(net::ip::address_v4::any(), 12345);
net::ip::tcp::acceptor acceptor(context, endpoint);

Expand Down
Loading

0 comments on commit 86d69f7

Please sign in to comment.