Miscellaneous API improvements (#4318)

This commit is contained in:
Eddy Ashton 2022-10-07 11:02:11 +01:00 коммит произвёл GitHub
Родитель dd3c08f003
Коммит 06ef4d6094
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 56 добавлений и 64 удалений

Просмотреть файл

@ -7,6 +7,7 @@
#include <array>
#include <cstdint>
#include <small_vector/SmallVector.h>
#include <string_view>
#include <vector>
namespace ds::hashutils
@ -109,14 +110,13 @@ namespace ds
}
template <typename T>
static constexpr T fnv_1a(char const* s)
static constexpr T fnv_1a(const std::string_view& sv)
{
using params = fnv_parameters<T>;
T hash = params::offset_basis;
T c = 0;
while ((c = *s++))
for (const auto& c : sv)
{
hash ^= c;
hash *= params::prime;

Просмотреть файл

@ -14,9 +14,6 @@ namespace ccfapp
std::map<std::string, std::shared_ptr<ccf::AbstractNodeSubSystem>>
subsystems;
public:
virtual ~AbstractNodeContext() = default;
void install_subsystem(
const std::shared_ptr<ccf::AbstractNodeSubSystem>& subsystem,
const std::string& name)
@ -36,12 +33,6 @@ namespace ccfapp
subsystems.emplace_hint(it, name, subsystem);
}
template <typename T>
void install_subsystem(const std::shared_ptr<T>& subsystem)
{
install_subsystem(subsystem, T::get_subsystem_name());
}
template <typename T>
std::shared_ptr<T> get_subsystem(const std::string& name) const
{
@ -55,6 +46,15 @@ namespace ccfapp
return nullptr;
}
public:
virtual ~AbstractNodeContext() = default;
template <typename T>
void install_subsystem(const std::shared_ptr<T>& subsystem)
{
install_subsystem(subsystem, T::get_subsystem_name());
}
template <typename T>
std::shared_ptr<T> get_subsystem() const
{

Просмотреть файл

@ -8,5 +8,8 @@ namespace ccf
{
public:
virtual ~AbstractNodeSubSystem() = default;
// Must contain a static function with signature:
// static char const* get_subsystem_name()
};
}

Просмотреть файл

@ -2,16 +2,35 @@
// Licensed under the Apache 2.0 License.
#pragma once
#include "ccf/ds/hash.h"
#include <llhttp/llhttp.h>
#include <string>
namespace http
{
extern llhttp_method http_method_from_str(const char* s);
}
namespace ccf
{
static inline llhttp_method http_method_from_str(const std::string_view& s)
{
const auto hashed_name = ds::fnv_1a<size_t>(s);
#define XX(num, name, string) \
case (ds::fnv_1a<size_t>(#string)): \
{ \
return llhttp_method(num); \
}
switch (hashed_name)
{
HTTP_METHOD_MAP(XX)
default:
{
throw std::logic_error(fmt::format("Unknown HTTP method '{}'", s));
}
}
#undef XX
}
/*!
Extension of llhttp_method
to allow make_*_endpoint() to be a single uniform interface to define
@ -32,15 +51,7 @@ namespace ccf
RESTVerb(const llhttp_method& hm) : verb(hm) {}
RESTVerb(const std::string& s)
{
#define HTTP_METHOD_GEN(NUM, NAME, STRING) \
if (s == #STRING) \
{ \
verb = static_cast<llhttp_method>(NUM); \
return; \
}
HTTP_METHOD_MAP(HTTP_METHOD_GEN)
#undef HTTP_METHOD_GEN
throw std::logic_error(fmt::format("unknown method {}", s));
verb = http_method_from_str(s.c_str());
}
std::optional<llhttp_method> get_http_method() const
@ -78,19 +89,6 @@ namespace ccf
j = s;
}
static inline llhttp_method http_method_from_str(const char* s)
{
#define XX(num, name, string) \
if (strcmp(s, #string) == 0) \
{ \
return llhttp_method(num); \
}
HTTP_METHOD_MAP(XX)
#undef XX
throw std::logic_error(fmt::format("Unknown HTTP method '{}'", s));
}
inline void from_json(const nlohmann::json& j, RESTVerb& verb)
{
if (!j.is_string())
@ -102,6 +100,6 @@ namespace ccf
std::string s = j.get<std::string>();
nonstd::to_upper(s);
verb = RESTVerb(http_method_from_str(s.c_str()));
verb = RESTVerb(s.c_str());
}
}

Просмотреть файл

@ -10,6 +10,7 @@
#define FMT_HEADER_ONLY
#include <fmt/format.h>
#include <optional>
#include <span>
#include <string>
#include <vector>
@ -206,6 +207,20 @@ namespace ringbuffer
return S::deserialize(data, size);
}
template <ringbuffer::Message m>
inline auto read_message(std::span<const uint8_t>& span)
{
using S = MessageSerializers<m>;
const uint8_t* data = span.data();
size_t size = span.size();
size_t original_size = size;
auto ret = S::deserialize(data, size);
span = span.subspan(original_size - size);
return ret;
}
template <ringbuffer::Message m, typename... Ts>
inline void write_message_with_error_wrapper(char const* prefix, Ts&&... ts)
{

Просмотреть файл

@ -37,18 +37,6 @@ namespace ccf
return status;
}
virtual std::vector<uint8_t> oversized_message_error(
size_t msg_size, size_t max_msg_size)
{
const auto s = fmt::format(
"Requested message ({} bytes) is too large. Maximum allowed is {} "
"bytes. Closing connection.",
msg_size,
max_msg_size);
const auto data = (const uint8_t*)s.data();
return std::vector<uint8_t>(data, data + s.size());
}
private:
std::vector<uint8_t> pending_write;
std::vector<uint8_t> pending_read;

Просмотреть файл

@ -35,18 +35,6 @@ namespace quic
return status;
}
virtual std::vector<uint8_t> oversized_message_error(
size_t msg_size, size_t max_msg_size)
{
const auto s = fmt::format(
"Requested message ({} bytes) is too large. Maximum allowed is {} "
"bytes. Closing connection.",
msg_size,
max_msg_size);
const auto data = (const uint8_t*)s.data();
return std::vector<uint8_t>(data, data + s.size());
}
protected:
using PendingBuffer = PendingIO<uint8_t>;
using PendingList = std::vector<PendingBuffer>;