ORE Studio 0.0.4
Loading...
Searching...
No Matches
connection.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_CONNECTION_HPP
21#define ORES_COMMS_NET_CONNECTION_HPP
22
23#include <atomic>
24#include <cstdint>
25#include <optional>
26#include <boost/asio/ip/tcp.hpp>
27#include <boost/asio/ssl.hpp>
28#include <boost/asio/awaitable.hpp>
29#include "ores.comms/messaging/frame.hpp"
30#include "ores.logging/make_logger.hpp"
31
32namespace ores::comms::net {
33
42 std::expected<messaging::frame, ores::utility::serialization::error_code> frame;
43
50 std::optional<std::uint32_t> failed_correlation_id;
51};
52
58class connection final {
59private:
60 inline static std::string_view logger_name = "ores.comms.net.connection";
61
62 static auto& lg() {
63 using namespace ores::logging;
64 static auto instance = make_logger(logger_name);
65 return instance;
66 }
67
68public:
69 using ssl_socket = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
70
74 explicit connection(ssl_socket socket);
75
81 boost::asio::awaitable<void> ssl_handshake_server(
82 boost::asio::cancellation_slot cancel_slot = {});
83
89 boost::asio::awaitable<void> ssl_handshake_client(
90 boost::asio::cancellation_slot cancel_slot = {});
91
105 boost::asio::awaitable<read_frame_result>
106 read_frame(bool skip_version_check = false,
107 boost::asio::cancellation_slot cancel_slot = {});
108
117 boost::asio::awaitable<void> write_frame(const messaging::frame& frame,
118 boost::asio::cancellation_slot cancel_slot = {});
119
123 bool is_open() const;
124
128 void close();
129
133 std::string remote_address() const;
134
138 [[nodiscard]] std::uint64_t bytes_sent() const;
139
143 [[nodiscard]] std::uint64_t bytes_received() const;
144
148 void reset_byte_counters();
149
150private:
151 ssl_socket socket_;
152 std::atomic<std::uint64_t> bytes_sent_{0};
153 std::atomic<std::uint64_t> bytes_received_{0};
154};
155
156}
157
158#endif
Contains the networking elements of the comms library.
Definition client.hpp:48
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Complete frame with header and payload.
Definition frame.hpp:77
Result of a read_frame operation.
Definition connection.hpp:41
std::optional< std::uint32_t > failed_correlation_id
Correlation ID from the header when CRC validation fails.
Definition connection.hpp:50
SSL connection wrapper for frame-based communication.
Definition connection.hpp:58
std::string remote_address() const
Get the remote endpoint address.
Definition connection.cpp:158
boost::asio::awaitable< void > ssl_handshake_server(boost::asio::cancellation_slot cancel_slot={})
Perform SSL handshake as server.
Definition connection.cpp:33
bool is_open() const
Check if the connection is open.
Definition connection.cpp:147
boost::asio::awaitable< read_frame_result > read_frame(bool skip_version_check=false, boost::asio::cancellation_slot cancel_slot={})
Read a complete frame from the connection.
Definition connection.cpp:46
std::uint64_t bytes_sent() const
Get total bytes sent on this connection.
Definition connection.cpp:169
void close()
Close the connection.
Definition connection.cpp:151
boost::asio::awaitable< void > write_frame(const messaging::frame &frame, boost::asio::cancellation_slot cancel_slot={})
Write a frame to the connection.
Definition connection.cpp:130
void reset_byte_counters()
Reset byte counters to zero.
Definition connection.cpp:177
boost::asio::awaitable< void > ssl_handshake_client(boost::asio::cancellation_slot cancel_slot={})
Perform SSL handshake as client.
Definition connection.cpp:39
std::uint64_t bytes_received() const
Get total bytes received on this connection.
Definition connection.cpp:173