network-experiment/bmrshared-web/include/bmrshared/response_promise.hpp

41 lines
1.2 KiB
C++

// GNU Lesser General Public License v3.0
// Copyright (c) 2025 Bart Beumer <bart@4beumer.nl>
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License v3.0 as published by
// the Free Software Foundation.
//
#pragma once
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast.hpp>
#include <functional>
namespace bmrshared::web
{
class response_promise final
{
public:
using response_sender = std::function<void(boost::beast::tcp_stream& stream)>;
using callback_on_response = std::function<void(const response_sender& fnSendResponse)>;
response_promise() = delete;
explicit response_promise(callback_on_response cbOnResponse);
~response_promise();
template<typename Body, typename Fields>
void SendResponse(boost::beast::http::response<Body, Fields> response)
{
response_sender responder = [&response](boost::beast::tcp_stream& stream)
{
boost::beast::http::write(stream, response);
};
m_call_on_response(responder);
}
private:
callback_on_response m_call_on_response;
};
} // namespace bmrshared::web