Skip to content

snp quic #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1b02d30
snp quic
turuslan Feb 11, 2025
f67cf4e
ci
turuslan Feb 11, 2025
ed8eb71
ci
turuslan Feb 11, 2025
f7cf132
snp quic + rebase
turuslan Feb 12, 2025
8444b77
merge rebase
turuslan Feb 12, 2025
bffc90b
forward
turuslan Feb 17, 2025
e499d39
make_shared
turuslan Feb 17, 2025
95699a2
move
turuslan Feb 17, 2025
89f987b
weak
turuslan Feb 17, 2025
0c9f159
set coro thread
turuslan Feb 17, 2025
f742c39
coro yield
turuslan Feb 17, 2025
e814063
enum type
turuslan Feb 17, 2025
bbddad4
cppcodec macro
turuslan Feb 17, 2025
b12bdcd
numeric limits
turuslan Feb 17, 2025
856d7f4
if return
turuslan Feb 17, 2025
5bb57a4
likely
turuslan Feb 17, 2025
b853b9a
self from void
turuslan Feb 17, 2025
22699ad
example
turuslan Feb 17, 2025
ef17c47
sizeof
turuslan Feb 17, 2025
9814d46
rename
turuslan Feb 17, 2025
387193e
rename self
turuslan Feb 17, 2025
0bfb4fd
error text
turuslan Feb 17, 2025
b9d83cc
comment
turuslan Feb 17, 2025
90b06ab
comment
turuslan Feb 17, 2025
a34d339
comment
turuslan Feb 17, 2025
25c3e6d
variant get
turuslan Feb 17, 2025
6aa4b49
hash constraint
turuslan Feb 18, 2025
ad703ed
forward
turuslan Feb 18, 2025
749ee00
comment
turuslan Feb 18, 2025
34c487a
executor constraint
turuslan Feb 18, 2025
8e9d06e
optional
turuslan Feb 18, 2025
4c9e48b
revert optional
turuslan Feb 25, 2025
f5b2466
update qtils
turuslan Feb 26, 2025
927632c
soralog
turuslan Feb 26, 2025
e97ecc8
if
turuslan Feb 26, 2025
4572529
make shared
turuslan Feb 27, 2025
bb881b5
revert "make shared"
turuslan Feb 27, 2025
812e33f
port type
turuslan Mar 3, 2025
2eaf302
shutdown
turuslan Mar 3, 2025
5632697
remove
turuslan Mar 3, 2025
790ce87
log socket read/write error
turuslan Mar 3, 2025
7526bb9
Merge remote-tracking branch 'origin/master' into snp/quic
turuslan Mar 21, 2025
b932469
soralog constructor
turuslan Mar 31, 2025
7169543
build md
turuslan Mar 31, 2025
b4173f0
make shared private
turuslan Mar 31, 2025
5b679c7
rename
turuslan Mar 31, 2025
e8b2c10
rename
turuslan Apr 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ if (TESTING)
list(APPEND VCPKG_MANIFEST_FEATURES test)
endif ()

option(BUILD_EXAMPLES "Build examples" ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
Expand Down Expand Up @@ -45,6 +47,8 @@ find_package(ZLIB REQUIRED)

include(vcpkg-overlay/cppcodec.cmake)

include_directories(${CMAKE_SOURCE_DIR}/src)

add_subdirectory(src)

if (TESTING)
Expand All @@ -56,3 +60,7 @@ if (TESTING)
add_subdirectory(test-vectors)
add_subdirectory(tests)
endif ()

if (BUILD_EXAMPLES)
add_subdirectory(example)
endif ()
7 changes: 7 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#

add_subdirectory(snp_chat)
13 changes: 13 additions & 0 deletions example/snp_chat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#

add_executable(example_snp_chat
main.cpp
)
target_link_libraries(example_snp_chat
snp
)

15 changes: 7 additions & 8 deletions src/snp/example_chat.cpp → example/snp_chat/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct ChatController : ConnectionsController {
fmt::println("#{} > {}", i_msg, msg);
}

Coro<void> broadcast(std::optional<size_t> i_read,
Coro<void> broadcast(qtils::Optional<size_t> i_read,
size_t i_msg,
std::string msg) {
for (auto &[i_write, writer] : writers) {
Expand Down Expand Up @@ -152,7 +152,7 @@ struct ChatController : ConnectionsController {
struct Input {
Input(IoContextPtr io_context_ptr) : fd_{*io_context_ptr, STDIN_FILENO} {}

Coro<std::optional<std::string>> read() {
Coro<qtils::Optional<std::string>> read() {
auto [ec, n] = co_await boost::asio::async_read_until(
fd_, buf_, "\n", boost::asio::as_tuple(boost::asio::use_awaitable));
if (ec) {
Expand All @@ -175,7 +175,7 @@ struct Input {
CoroOutcome<void> co_main(IoContextPtr io_context_ptr, size_t arg_i) {
fmt::println("#{} (self)", arg_i);

std::optional<Port> listen_port;
qtils::Optional<Port> listen_port;
GenesisHash genesis;
ConnectionsConfig config{genesis, keys.at(arg_i)};
auto is_server = arg_i == 0;
Expand All @@ -189,7 +189,7 @@ CoroOutcome<void> co_main(IoContextPtr io_context_ptr, size_t arg_i) {
Input input{io_context_ptr};
while (true) {
auto msg = co_await input.read();
if (not msg) {
if (not msg.has_value()) {
break;
}
msg->resize(std::min(msg->size(), ChatController::kMaxMsg));
Expand All @@ -216,10 +216,9 @@ CoroOutcome<void> co_main(IoContextPtr io_context_ptr, size_t arg_i) {
[chat](ConnectionInfo info, StreamPtr stream) -> CoroOutcome<void> {
co_return co_await chat->add(info, stream);
});
std::optional<CoroHandler<void>> work_guard;
co_await coroHandler<void>([&](CoroHandler<void> &&handler) {
work_guard.emplace(std::move(handler));
});
qtils::Optional<CoroHandler<void>> work_guard;
co_await coroHandler<void>(
[&](CoroHandler<void> &&handler) { work_guard = std::move(handler); });
}
co_return outcome::success();
}
Expand Down
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# SPDX-License-Identifier: Apache-2.0
#

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Executables (should contain `main()` function)
add_subdirectory(executable)

Expand Down
3 changes: 2 additions & 1 deletion src/TODO_qtils/from_span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <optional>
#include <stdexcept>

#include <TODO_qtils/optional.hpp>
#include <qtils/bytes.hpp>

namespace qtils {
Expand All @@ -22,7 +23,7 @@ namespace qtils {
}

template <typename T>
std::optional<T> fromSpan(BytesIn span) {
Optional<T> fromSpan(BytesIn span) {
T out;
if (not fromSpan(out, span)) {
return std::nullopt;
Expand Down
11 changes: 0 additions & 11 deletions src/TODO_qtils/macro/forward.hpp

This file was deleted.

16 changes: 0 additions & 16 deletions src/TODO_qtils/macro/make_shared.hpp

This file was deleted.

19 changes: 0 additions & 19 deletions src/TODO_qtils/macro/move.hpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/TODO_qtils/macro/weak.hpp

This file was deleted.

7 changes: 5 additions & 2 deletions src/TODO_qtils/map_entry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ namespace qtils {
}
auto &operator*() {
if (not has()) {
throw std::logic_error{"MapEntry::operator*"};
throw std::logic_error{
"Call dereference operator of MapEntry without valid iterator"};
}
return std::get<I>(it_or_key)->second;
}
auto *operator->() {
if (not has()) {
throw std::logic_error{"MapEntry::operator->"};
throw std::logic_error{
"Call member access through pointer operator of MapEntry without "
"valid iterator"};
}
return &std::get<I>(it_or_key)->second;
}
Expand Down
79 changes: 79 additions & 0 deletions src/TODO_qtils/optional.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <optional>

namespace qtils {
/**
* `std::optional` wrapper.
* Becomes `std::nullopt` when moved from.
* `std::nullopt` throws on access to value.
* Requires to write `.has_value()`.
* Can be move assigned for move only types.
*/
template <typename T>
class Optional {
public:
Optional() : value_{std::nullopt} {}

Optional(const std::nullopt_t &) : value_{std::nullopt} {}

Optional(const T &value) : value_{value} {}
void operator=(const T &value) {
value_.emplace(value);
}

Optional(T &&value) : value_{std::move(value)} {}
void operator=(T &&value) {
value_.emplace(std::move(value));
}

Optional(const Optional<T> &other) : value_{other.value_} {}
void operator=(const Optional<T> &other) {
value_ = other.value_;
}

Optional(Optional<T> &&other)
: value_{std::exchange(other.value_, std::nullopt)} {}
void operator=(Optional<T> &&other) {
if (other.value_.has_value()) {
value_.emplace(std::exchange(other.value_, std::nullopt).value());
} else {
value_.reset();
}
}

bool has_value() const {
return value_.has_value();
}

T &operator*() {
return value_.value();
}
const T &operator*() const {
return value_.value();
}
T *operator->() {
return &value_.value();
}
const T *operator->() const {
return &value_.value();
}

Optional<T> take() {
return std::exchange(*this, Optional<T>{});
}

bool operator==(const T &value) const {
return value_ == value;
}

private:
std::optional<T> value_;
};
} // namespace qtils
1 change: 1 addition & 0 deletions src/TODO_qtils/std_hash_of.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace qtils {
template <typename T>
requires(requires(const T &v) { std::hash<T>()(v); })
size_t stdHashOf(const T &v) {
return std::hash<T>()(v);
}
Expand Down
41 changes: 41 additions & 0 deletions src/TODO_qtils/variant_get.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <variant>

namespace qtils {
/**
* Some people don't like pointer in
* if (auto *t = std::get_if<T>(&v))
*/
template <typename T, typename V>
struct VariantGet {
VariantGet(V &variant) : variant_{variant} {}
operator bool() const {
return std::holds_alternative<T>(variant_);
}
auto &operator*() {
return std::get<T>(variant_);
}
auto *operator->() {
return &std::get<T>(variant_);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
V &variant_;
};

template <typename T, typename... V>
auto variantGet(const std::variant<V...> &variant) {
return VariantGet<T, decltype(variant)>{variant};
}

template <typename T, typename... V>
auto variantGet(std::variant<V...> &variant) {
return VariantGet<T, decltype(variant)>{variant};
}
} // namespace qtils
21 changes: 11 additions & 10 deletions src/coro/future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <stdexcept>
#include <variant>

#include <TODO_qtils/macro/move.hpp>
#include <TODO_qtils/variant_get.hpp>

#include "coro/handler.hpp"
#include "coro/set_thread.hpp"
Expand All @@ -19,21 +19,22 @@ namespace jam {
template <typename T>
class SharedFuture {
public:
using Self = std::shared_ptr<SharedFuture<T>>;
using SelfSPtr = std::shared_ptr<SharedFuture<T>>;

SharedFuture(IoContextPtr io_context_ptr) : MOVE_(io_context_ptr) {}
SharedFuture(IoContextPtr io_context_ptr)
: io_context_ptr_{std::move(io_context_ptr)} {}

static Coro<bool> ready(Self self) {
SET_CORO_THREAD(self->io_context_ptr_);
static Coro<bool> ready(SelfSPtr self) {
co_await setCoroThread(self->io_context_ptr_);
co_return std::holds_alternative<T>(self->state_);
}

/**
* Resumes coroutine immediately or inside `set`.
*/
static Coro<T> get(Self self) {
SET_CORO_THREAD(self->io_context_ptr_);
if (auto *value = std::get_if<T>(&self->state_)) {
static Coro<T> get(SelfSPtr self) {
co_await setCoroThread(self->io_context_ptr_);
if (auto value = qtils::variantGet<T>(self->state_)) {
co_return *value;
}
auto &handlers = std::get<Handlers>(self->state_);
Expand All @@ -46,8 +47,8 @@ namespace jam {
* Set value and wake waiting coroutines.
* Coroutines may complete before `set` returns.
*/
static Coro<void> set(Self self, T value) {
SET_CORO_THREAD(self->io_context_ptr_);
static Coro<void> set(SelfSPtr self, T value) {
co_await setCoroThread(self->io_context_ptr_);
if (std::holds_alternative<T>(self->state_)) {
throw std::logic_error{"SharedFuture::set must be called once"};
}
Expand Down
Loading
Loading