ORE Studio 0.0.4
Loading...
Searching...
No Matches
response_channel.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_COMMS_NET_RESPONSE_CHANNEL_HPP
21#define ORES_COMMS_NET_RESPONSE_CHANNEL_HPP
22
23#include <mutex>
24#include <optional>
25#include <expected>
26#include <boost/asio/any_io_executor.hpp>
27#include <boost/asio/awaitable.hpp>
28#include <boost/asio/steady_timer.hpp>
29#include <boost/asio/use_awaitable.hpp>
30#include "ores.comms/messaging/frame.hpp"
31#include "ores.comms/messaging/message_types.hpp"
32
33namespace ores::comms::net {
34
46class response_channel final {
47public:
48 explicit response_channel(boost::asio::any_io_executor executor);
49
50 // Non-copyable, non-movable (timer has executor affinity)
51 response_channel(const response_channel&) = delete;
52 response_channel& operator=(const response_channel&) = delete;
54 response_channel& operator=(response_channel&&) = delete;
55
64 void set_value(messaging::frame response);
65
74 void set_error(messaging::error_code ec);
75
83 boost::asio::awaitable<std::expected<messaging::frame, messaging::error_code>> get();
84
88 bool is_ready() const;
89
90private:
91 boost::asio::steady_timer signal_;
92 mutable std::mutex mutex_;
93 std::optional<messaging::frame> response_;
94 std::optional<messaging::error_code> error_;
95 bool ready_;
96};
97
98}
99
100#endif
Contains the networking elements of the comms library.
Definition client.hpp:42
Complete frame with header and payload.
Definition frame.hpp:77
Single-value async channel for delivering a response frame.
Definition response_channel.hpp:46
boost::asio::awaitable< std::expected< messaging::frame, messaging::error_code > > get()
Wait for and retrieve the response (consumer side).
Definition response_channel.cpp:61
void set_error(messaging::error_code ec)
Set an error (producer side).
Definition response_channel.cpp:47
bool is_ready() const
Check if the channel has already received a value or error.
Definition response_channel.cpp:93
void set_value(messaging::frame response)
Set the response value (producer side).
Definition response_channel.cpp:34