|
| 1 | +/** |
| 2 | + * Copyright Quadrivium LLC |
| 3 | + * All Rights Reserved |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <lsquic.h> |
| 10 | +#include <boost/asio/ip/udp.hpp> |
| 11 | +#include <boost/asio/steady_timer.hpp> |
| 12 | +#include <libp2p/multi/multiaddress.hpp> |
| 13 | +#include <libp2p/peer/peer_id.hpp> |
| 14 | +#include <memory> |
| 15 | +#include <optional> |
| 16 | +#include <qtils/bytes.hpp> |
| 17 | +#include <qtils/outcome.hpp> |
| 18 | + |
| 19 | +namespace boost::asio { |
| 20 | + class io_context; |
| 21 | +} // namespace boost::asio |
| 22 | + |
| 23 | +namespace boost::asio::ssl { |
| 24 | + class context; |
| 25 | +} // namespace boost::asio::ssl |
| 26 | + |
| 27 | +namespace libp2p::connection { |
| 28 | + struct QuicStream; |
| 29 | +} // namespace libp2p::connection |
| 30 | + |
| 31 | +namespace libp2p::crypto::marshaller { |
| 32 | + class KeyMarshaller; |
| 33 | +} // namespace libp2p::crypto::marshaller |
| 34 | + |
| 35 | +namespace libp2p::muxer { |
| 36 | + struct MuxedConnectionConfig; |
| 37 | +} // namespace libp2p::muxer |
| 38 | + |
| 39 | +namespace libp2p::transport { |
| 40 | + struct QuicConnection; |
| 41 | +} // namespace libp2p::transport |
| 42 | + |
| 43 | +namespace libp2p::transport::lsquic { |
| 44 | + using connection::QuicStream; |
| 45 | + |
| 46 | + struct Engine; |
| 47 | + struct ConnCtx; |
| 48 | + struct StreamCtx; |
| 49 | + |
| 50 | + using OnConnect = |
| 51 | + std::function<void(outcome::result<std::shared_ptr<QuicConnection>>)>; |
| 52 | + /** |
| 53 | + * Connect operation arguments. |
| 54 | + */ |
| 55 | + struct Connecting { |
| 56 | + boost::asio::ip::udp::endpoint remote; |
| 57 | + PeerId peer; |
| 58 | + OnConnect cb; |
| 59 | + }; |
| 60 | + /** |
| 61 | + * `lsquic_conn_ctx_t` for libp2p connection. |
| 62 | + */ |
| 63 | + struct ConnCtx { |
| 64 | + Engine *engine; |
| 65 | + lsquic_conn_t *ls_conn; |
| 66 | + std::optional<Connecting> connecting{}; |
| 67 | + std::optional<std::shared_ptr<QuicStream>> new_stream{}; |
| 68 | + std::weak_ptr<QuicConnection> conn{}; |
| 69 | + }; |
| 70 | + |
| 71 | + /** |
| 72 | + * `lsquic_stream_ctx_t` for libp2p stream. |
| 73 | + */ |
| 74 | + struct StreamCtx { |
| 75 | + Engine *engine; |
| 76 | + lsquic_stream_t *ls_stream; |
| 77 | + std::weak_ptr<QuicStream> stream{}; |
| 78 | + /** |
| 79 | + * Stream read operation arguments. |
| 80 | + */ |
| 81 | + struct Reading { |
| 82 | + BytesOut out; |
| 83 | + std::function<void(outcome::result<size_t>)> cb; |
| 84 | + }; |
| 85 | + std::optional<Reading> reading{}; |
| 86 | + }; |
| 87 | + |
| 88 | + using OnAccept = std::function<void(std::shared_ptr<QuicConnection>)>; |
| 89 | + |
| 90 | + /** |
| 91 | + * libp2p wrapper and adapter for lsquic server/client socket. |
| 92 | + */ |
| 93 | + class Engine : public std::enable_shared_from_this<Engine> { |
| 94 | + public: |
| 95 | + Engine(std::shared_ptr<boost::asio::io_context> io_context, |
| 96 | + std::shared_ptr<boost::asio::ssl::context> ssl_context, |
| 97 | + const muxer::MuxedConnectionConfig &mux_config, |
| 98 | + PeerId local_peer, |
| 99 | + std::shared_ptr<crypto::marshaller::KeyMarshaller> key_codec, |
| 100 | + boost::asio::ip::udp::socket &&socket, |
| 101 | + bool client); |
| 102 | + ~Engine(); |
| 103 | + |
| 104 | + // clang-tidy cppcoreguidelines-special-member-functions |
| 105 | + Engine(const Engine &) = delete; |
| 106 | + void operator=(const Engine &) = delete; |
| 107 | + Engine(Engine &&) = delete; |
| 108 | + void operator=(Engine &&) = delete; |
| 109 | + |
| 110 | + auto &local() const { |
| 111 | + return local_; |
| 112 | + } |
| 113 | + void start(); |
| 114 | + void connect(const boost::asio::ip::udp::endpoint &remote, |
| 115 | + const PeerId &peer, |
| 116 | + OnConnect cb); |
| 117 | + outcome::result<std::shared_ptr<QuicStream>> newStream(ConnCtx *conn_ctx); |
| 118 | + void onAccept(OnAccept cb) { |
| 119 | + on_accept_ = std::move(cb); |
| 120 | + } |
| 121 | + void process(); |
| 122 | + |
| 123 | + private: |
| 124 | + void readLoop(); |
| 125 | + |
| 126 | + std::shared_ptr<boost::asio::io_context> io_context_; |
| 127 | + std::shared_ptr<boost::asio::ssl::context> ssl_context_; |
| 128 | + PeerId local_peer_; |
| 129 | + std::shared_ptr<crypto::marshaller::KeyMarshaller> key_codec_; |
| 130 | + boost::asio::ip::udp::socket socket_; |
| 131 | + boost::asio::steady_timer timer_; |
| 132 | + boost::asio::ip::udp::endpoint socket_local_; |
| 133 | + Multiaddress local_; |
| 134 | + lsquic_engine_t *engine_ = nullptr; |
| 135 | + OnAccept on_accept_; |
| 136 | + bool started_ = false; |
| 137 | + std::optional<Connecting> connecting_; |
| 138 | + struct Reading { |
| 139 | + static constexpr size_t kMaxUdpPacketSize = 64 << 10; |
| 140 | + qtils::BytesN<kMaxUdpPacketSize> buf; |
| 141 | + boost::asio::ip::udp::endpoint remote; |
| 142 | + }; |
| 143 | + Reading reading_; |
| 144 | + }; |
| 145 | +} // namespace libp2p::transport::lsquic |
0 commit comments