generated from Warchant/cmake-hunter-seed
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
- Loading branch information
Showing
27 changed files
with
1,504 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import itertools | ||
import re | ||
|
||
re_cpp = re.compile(r"\.(h|hpp|c|cpp)$") | ||
|
||
copyright_cmake = """\ | ||
# | ||
# Copyright Quadrivium LLC | ||
# All Rights Reserved | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
""" | ||
copyright = """\ | ||
/** | ||
* Copyright Quadrivium LLC | ||
* All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
""" | ||
|
||
for dir_, _, names in itertools.chain(*map(os.walk, ["include", "src", "test"])): | ||
for name in names: | ||
is_cmake = name == "CMakeLists.txt" or name.endswith(".cmake") | ||
is_cpp = re_cpp.search(name) | ||
if not is_cmake and not is_cpp: | ||
continue | ||
path = os.path.join(dir_, name) | ||
txt = open(path).read() | ||
txt0 = txt | ||
if not txt.endswith("\n"): | ||
print("EOL", path) | ||
txt = txt + "\n" | ||
if is_cmake and not txt.startswith(copyright_cmake): | ||
print("COPYRIGHT", path) | ||
txt = copyright_cmake + "\n" + txt | ||
if is_cpp and not txt.startswith(copyright): | ||
print("COPYRIGHT", path) | ||
txt = copyright + "\n" + txt | ||
if txt != txt0: | ||
print("WRITE", path) | ||
with open(path, "w") as f: | ||
f.write(txt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright Quadrivium LLC | ||
* All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <libp2p/common/metrics/instance_count.hpp> | ||
#include <libp2p/connection/capable_connection.hpp> | ||
|
||
namespace boost::asio { | ||
class io_context; | ||
} // namespace boost::asio | ||
|
||
namespace libp2p::transport::lsquic { | ||
class Engine; | ||
struct ConnCtx; | ||
} // namespace libp2p::transport::lsquic | ||
|
||
namespace libp2p::transport { | ||
class QuicConnection : public connection::CapableConnection, | ||
public std::enable_shared_from_this<QuicConnection> { | ||
public: | ||
QuicConnection(std::shared_ptr<boost::asio::io_context> io_context, | ||
lsquic::ConnCtx *conn_ctx, | ||
bool initiator, | ||
Multiaddress local, | ||
Multiaddress remote, | ||
PeerId local_peer, | ||
PeerId peer, | ||
crypto::PublicKey key); | ||
~QuicConnection() override; | ||
|
||
// clang-tidy cppcoreguidelines-special-member-functions | ||
QuicConnection(const QuicConnection &) = delete; | ||
void operator=(const QuicConnection &) = delete; | ||
QuicConnection(QuicConnection &&) = delete; | ||
void operator=(QuicConnection &&) = delete; | ||
|
||
// Reader | ||
void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; | ||
void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; | ||
void deferReadCallback(outcome::result<size_t> res, | ||
ReadCallbackFunc cb) override; | ||
|
||
// Writer | ||
void writeSome(BytesIn in, size_t bytes, WriteCallbackFunc cb) override; | ||
void deferWriteCallback(std::error_code ec, WriteCallbackFunc cb) override; | ||
|
||
// Closeable | ||
bool isClosed() const override; | ||
outcome::result<void> close() override; | ||
|
||
// LayerConnection | ||
bool isInitiator() const noexcept override; | ||
outcome::result<Multiaddress> remoteMultiaddr() override; | ||
outcome::result<Multiaddress> localMultiaddr() override; | ||
|
||
// SecureConnection | ||
outcome::result<PeerId> localPeer() const override; | ||
outcome::result<PeerId> remotePeer() const override; | ||
outcome::result<crypto::PublicKey> remotePublicKey() const override; | ||
|
||
// CapableConnection | ||
void start() override; | ||
void stop() override; | ||
void newStream(StreamHandlerFunc cb) override; | ||
outcome::result<std::shared_ptr<libp2p::connection::Stream>> newStream() | ||
override; | ||
void onStream(NewStreamHandlerFunc cb) override; | ||
|
||
void onClose(); | ||
auto &onStream() const { | ||
return on_stream_; | ||
} | ||
|
||
private: | ||
std::shared_ptr<boost::asio::io_context> io_context_; | ||
lsquic::ConnCtx *conn_ctx_; | ||
bool initiator_; | ||
Multiaddress local_, remote_; | ||
PeerId local_peer_, peer_; | ||
crypto::PublicKey key_; | ||
NewStreamHandlerFunc on_stream_; | ||
|
||
public: | ||
LIBP2P_METRICS_INSTANCE_COUNT_IF_ENABLED(libp2p::transport::QuicConnection); | ||
}; | ||
} // namespace libp2p::transport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/** | ||
* Copyright Quadrivium LLC | ||
* All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <lsquic.h> | ||
#include <boost/asio/ip/udp.hpp> | ||
#include <boost/asio/steady_timer.hpp> | ||
#include <libp2p/multi/multiaddress.hpp> | ||
#include <libp2p/peer/peer_id.hpp> | ||
#include <memory> | ||
#include <optional> | ||
#include <qtils/bytes.hpp> | ||
#include <qtils/outcome.hpp> | ||
|
||
namespace boost::asio { | ||
class io_context; | ||
} // namespace boost::asio | ||
|
||
namespace boost::asio::ssl { | ||
class context; | ||
} // namespace boost::asio::ssl | ||
|
||
namespace libp2p::connection { | ||
struct QuicStream; | ||
} // namespace libp2p::connection | ||
|
||
namespace libp2p::crypto::marshaller { | ||
class KeyMarshaller; | ||
} // namespace libp2p::crypto::marshaller | ||
|
||
namespace libp2p::muxer { | ||
struct MuxedConnectionConfig; | ||
} // namespace libp2p::muxer | ||
|
||
namespace libp2p::transport { | ||
struct QuicConnection; | ||
} // namespace libp2p::transport | ||
|
||
namespace libp2p::transport::lsquic { | ||
using connection::QuicStream; | ||
|
||
struct Engine; | ||
struct ConnCtx; | ||
struct StreamCtx; | ||
|
||
using OnConnect = | ||
std::function<void(outcome::result<std::shared_ptr<QuicConnection>>)>; | ||
/** | ||
* Connect operation arguments. | ||
*/ | ||
struct Connecting { | ||
boost::asio::ip::udp::endpoint remote; | ||
PeerId peer; | ||
OnConnect cb; | ||
}; | ||
/** | ||
* `lsquic_conn_ctx_t` for libp2p connection. | ||
*/ | ||
struct ConnCtx { | ||
Engine *engine; | ||
lsquic_conn_t *ls_conn; | ||
std::optional<Connecting> connecting{}; | ||
std::optional<std::shared_ptr<QuicStream>> new_stream{}; | ||
std::weak_ptr<QuicConnection> conn{}; | ||
}; | ||
|
||
/** | ||
* `lsquic_stream_ctx_t` for libp2p stream. | ||
*/ | ||
struct StreamCtx { | ||
Engine *engine; | ||
lsquic_stream_t *ls_stream; | ||
std::weak_ptr<QuicStream> stream{}; | ||
/** | ||
* Stream read operation arguments. | ||
*/ | ||
struct Reading { | ||
BytesOut out; | ||
std::function<void(outcome::result<size_t>)> cb; | ||
}; | ||
std::optional<Reading> reading{}; | ||
}; | ||
|
||
using OnAccept = std::function<void(std::shared_ptr<QuicConnection>)>; | ||
|
||
/** | ||
* libp2p wrapper and adapter for lsquic server/client socket. | ||
*/ | ||
class Engine : public std::enable_shared_from_this<Engine> { | ||
public: | ||
Engine(std::shared_ptr<boost::asio::io_context> io_context, | ||
std::shared_ptr<boost::asio::ssl::context> ssl_context, | ||
const muxer::MuxedConnectionConfig &mux_config, | ||
PeerId local_peer, | ||
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_codec, | ||
boost::asio::ip::udp::socket &&socket, | ||
bool client); | ||
~Engine(); | ||
|
||
// clang-tidy cppcoreguidelines-special-member-functions | ||
Engine(const Engine &) = delete; | ||
void operator=(const Engine &) = delete; | ||
Engine(Engine &&) = delete; | ||
void operator=(Engine &&) = delete; | ||
|
||
auto &local() const { | ||
return local_; | ||
} | ||
void start(); | ||
void connect(const boost::asio::ip::udp::endpoint &remote, | ||
const PeerId &peer, | ||
OnConnect cb); | ||
outcome::result<std::shared_ptr<QuicStream>> newStream(ConnCtx *conn_ctx); | ||
void onAccept(OnAccept cb) { | ||
on_accept_ = std::move(cb); | ||
} | ||
void process(); | ||
|
||
private: | ||
void readLoop(); | ||
|
||
std::shared_ptr<boost::asio::io_context> io_context_; | ||
std::shared_ptr<boost::asio::ssl::context> ssl_context_; | ||
PeerId local_peer_; | ||
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_codec_; | ||
boost::asio::ip::udp::socket socket_; | ||
boost::asio::steady_timer timer_; | ||
boost::asio::ip::udp::endpoint socket_local_; | ||
Multiaddress local_; | ||
lsquic_engine_t *engine_ = nullptr; | ||
OnAccept on_accept_; | ||
bool started_ = false; | ||
std::optional<Connecting> connecting_; | ||
struct Reading { | ||
static constexpr size_t kMaxUdpPacketSize = 64 << 10; | ||
qtils::BytesN<kMaxUdpPacketSize> buf; | ||
boost::asio::ip::udp::endpoint remote; | ||
}; | ||
Reading reading_; | ||
}; | ||
} // namespace libp2p::transport::lsquic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright Quadrivium LLC | ||
* All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <qtils/enum_error_code.hpp> | ||
|
||
namespace libp2p { | ||
enum class QuicError { | ||
HANDSHAKE_FAILED, | ||
CONN_CLOSED, | ||
STREAM_CLOSED, | ||
TOO_MANY_STREAMS, | ||
CANT_CREATE_CONNECTION, | ||
CANT_OPEN_STREAM, | ||
}; | ||
Q_ENUM_ERROR_CODE(QuicError) { | ||
using E = decltype(e); | ||
switch (e) { | ||
case E::HANDSHAKE_FAILED: | ||
return "HANDSHAKE_FAILED"; | ||
case E::CONN_CLOSED: | ||
return "CONN_CLOSED"; | ||
case E::STREAM_CLOSED: | ||
return "STREAM_CLOSED"; | ||
case E::TOO_MANY_STREAMS: | ||
return "TOO_MANY_STREAMS"; | ||
case E::CANT_CREATE_CONNECTION: | ||
return "CANT_CREATE_CONNECTION"; | ||
case E::CANT_OPEN_STREAM: | ||
return "CANT_OPEN_STREAM"; | ||
} | ||
abort(); | ||
} | ||
} // namespace libp2p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright Quadrivium LLC | ||
* All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <lsquic.h> | ||
#include <stdexcept> | ||
|
||
namespace libp2p::transport { | ||
inline void lsquicInit() { | ||
static auto _ = [] { | ||
if (lsquic_global_init(LSQUIC_GLOBAL_CLIENT | LSQUIC_GLOBAL_SERVER) | ||
!= 0) { | ||
throw std::logic_error{"lsquic_global_init"}; | ||
} | ||
return 0; | ||
}(); | ||
} | ||
} // namespace libp2p::transport |
Oops, something went wrong.