Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
secure_tcp_socket.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6#ifndef KCENON_NETWORK_INTERNAL_TCP_SECURE_TCP_SOCKET_H_
7#define KCENON_NETWORK_INTERNAL_TCP_SECURE_TCP_SOCKET_H_
8
9#include <array>
10#include <atomic>
11#include <functional>
12#include <memory>
13#include <mutex>
14#include <span>
15#include <system_error>
16#include <vector>
17
18#include <asio.hpp>
19#include <asio/ssl.hpp>
20
22
24{
46 class secure_tcp_socket : public std::enable_shared_from_this<secure_tcp_socket>
47 {
48 public:
49 using ssl_socket = asio::ssl::stream<asio::ip::tcp::socket>;
50
61 secure_tcp_socket(asio::ip::tcp::socket socket,
62 asio::ssl::context& ssl_context);
63
67 ~secure_tcp_socket() = default;
68
76 auto async_handshake(
77 asio::ssl::stream_base::handshake_type type,
78 std::function<void(std::error_code)> handler) -> void;
79
92 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
93
127 std::function<void(std::span<const uint8_t>)> callback) -> void;
128
138 auto set_error_callback(std::function<void(std::error_code)> callback)
139 -> void;
140
148 auto start_read() -> void;
149
164 auto async_send(
165 std::vector<uint8_t>&& data,
166 std::function<void(std::error_code, std::size_t)> handler) -> void;
167
172 auto stream() -> ssl_socket& { return ssl_stream_; }
173
178 auto socket() -> ssl_socket::lowest_layer_type&
179 {
180 return ssl_stream_.lowest_layer();
181 }
182
186 auto stop_read() -> void;
187
195 auto close() -> void;
196
201 [[nodiscard]] auto is_closed() const -> bool;
202
203 private:
211 auto do_read() -> void;
212
213 private:
215 using receive_callback_t = std::function<void(const std::vector<uint8_t>&)>;
216 using receive_callback_view_t = std::function<void(std::span<const uint8_t>)>;
217 using error_callback_t = std::function<void(std::error_code)>;
218
221 std::array<uint8_t, 4096>
231
234 std::atomic<bool> is_reading_{false};
235 std::atomic<bool> is_closed_{false};
236 };
237} // namespace kcenon::network::internal
238
239#endif // KCENON_NETWORK_INTERNAL_TCP_SECURE_TCP_SOCKET_H_
A lightweight wrapper around asio::ssl::stream<asio::ip::tcp::socket>, enabling asynchronous TLS/SSL ...
std::shared_ptr< receive_callback_view_t > receive_callback_view_
std::shared_ptr< error_callback_t > error_callback_
auto set_receive_callback(std::function< void(const std::vector< uint8_t > &)> callback) -> void
Sets a callback to receive inbound data chunks.
secure_tcp_socket(asio::ip::tcp::socket socket, asio::ssl::context &ssl_context)
Constructs a secure_tcp_socket by taking ownership of a moved socket and SSL context.
auto async_handshake(asio::ssl::stream_base::handshake_type type, std::function< void(std::error_code)> handler) -> void
Performs asynchronous SSL handshake.
auto close() -> void
Safely closes the socket and stops all async operations.
auto stream() -> ssl_socket &
Provides direct access to the underlying SSL stream.
asio::ssl::stream< asio::ip::tcp::socket > ssl_socket
auto socket() -> ssl_socket::lowest_layer_type &
Provides access to the underlying TCP socket.
std::function< void(std::span< const uint8_t >)> receive_callback_view_t
auto start_read() -> void
Begins the continuous asynchronous read loop.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type aliases for lock-free storage.
auto async_send(std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> void
Initiates an asynchronous write of the given data buffer with encryption.
auto do_read() -> void
Internal function to handle the read logic with async_read_some().
auto is_closed() const -> bool
Checks if the socket has been closed.
auto set_receive_callback_view(std::function< void(std::span< const uint8_t >)> callback) -> void
Sets a zero-copy callback to receive inbound data as a view.
std::function< void(std::error_code)> error_callback_t
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
Sets a callback to handle socket errors (e.g., read/write failures).
std::shared_ptr< receive_callback_t > receive_callback_
auto stop_read() -> void
Stops the read loop to prevent further async operations.
~secure_tcp_socket()=default
Default destructor (no special cleanup needed).
std::mutex mutex