Skip to content

Commit

Permalink
retyping
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Dec 5, 2024
1 parent d9800a6 commit 64304b4
Show file tree
Hide file tree
Showing 58 changed files with 706 additions and 701 deletions.
24 changes: 23 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,29 @@ KeepEmptyLinesAtTheStartOfBlocks: 'true'
NamespaceIndentation: All
QualifierAlignment: Left
RemoveSemicolon: true
SortIncludes: true

#includes
SortIncludes: CaseInsensitive
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '".+\.h'
Priority: 2
- Regex: '^<(oxenc|oxen)'
Priority: 3
- Regex: '^<(event2|ngtcp2)'
Priority: 4
- Regex: '<winsock2\.h>'
Priority: 5
- Regex: '<windows\.h>'
Priority: 6
- Regex: '^<CLI'
Priority: 7
- Regex: '^<catch2'
Priority: 8
- Regex: '^<.*\.h(pp)?>$'
Priority: 9
- Regex: '(<)(.)+(>)'
Priority: 10

# spacing
SpaceBeforeParens: ControlStatements
Expand Down
32 changes: 18 additions & 14 deletions include/oxen/quic/address.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <oxenc/endian.h>

#include <compare>

#include "formattable.hpp"
#include "ip.hpp"
#include "types.hpp"

#include <oxenc/endian.h>

#include <compare>

#if defined(__OpenBSD__) || defined(__DragonFly__)
// These systems are known to disallow dual stack binding, and so on such systems when
// invoked with an empty address we default to the IPv4 any address rather than the IPv6 any
Expand Down Expand Up @@ -292,28 +292,32 @@ namespace oxen::quic
struct RemoteAddress : public Address
{
private:
ustring remote_pubkey;
std::vector<unsigned char> _remote_pubkey;

public:
RemoteAddress() = delete;

template <typename... Opt>
RemoteAddress(std::string_view remote_pk, Opt&&... opts) :
Address{std::forward<Opt>(opts)...}, remote_pubkey{to_usv(remote_pk)}
{}
Address{std::forward<Opt>(opts)...}, _remote_pubkey(remote_pk.size())
{
std::memcpy(_remote_pubkey.data(), remote_pk.data(), remote_pk.size());
}

template <typename... Opt>
RemoteAddress(ustring_view remote_pk, Opt&&... opts) : Address{std::forward<Opt>(opts)...}, remote_pubkey{remote_pk}
{}
RemoteAddress(uspan remote_pk, Opt&&... opts) : Address{std::forward<Opt>(opts)...}
{
_remote_pubkey.assign(remote_pk.data(), remote_pk.data() + remote_pk.size());
}

ustring_view view_remote_key() const { return remote_pubkey; }
const ustring& get_remote_key() const& { return remote_pubkey; }
ustring&& get_remote_key() && { return std::move(remote_pubkey); }
uspan view_remote_key() const { return _remote_pubkey; }
const std::vector<unsigned char>& get_remote_key() const& { return _remote_pubkey; }
std::vector<unsigned char>&& get_remote_key() && { return std::move(_remote_pubkey); }

RemoteAddress(const RemoteAddress& obj) : Address{obj}, remote_pubkey{obj.remote_pubkey} {}
RemoteAddress(const RemoteAddress& obj) : Address{obj}, _remote_pubkey{obj._remote_pubkey} {}
RemoteAddress& operator=(const RemoteAddress& obj)
{
remote_pubkey = obj.remote_pubkey;
_remote_pubkey = obj._remote_pubkey;
Address::operator=(obj);
_copy_internals(obj);
return *this;
Expand Down
34 changes: 17 additions & 17 deletions include/oxen/quic/btstream.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <oxenc/bt.h>

#include "endpoint.hpp"
#include "stream.hpp"
#include "utils.hpp"

#include <oxenc/bt.h>

namespace oxen::quic
{
using time_point = std::chrono::steady_clock::time_point;
Expand Down Expand Up @@ -37,7 +37,7 @@ namespace oxen::quic

private:
int64_t req_id;
bstring data;
std::vector<std::byte> data;

// We keep the locations of variables fields as relative positions inside `data` *rather*
// than using std::string_view members because the string_views are more difficult to
Expand All @@ -54,15 +54,15 @@ namespace oxen::quic
// didn't get any reply from the other side in time. This *can* happen earlier than the
// requested timeout in cases where we detect early that the response cannot arrive, such
// as the connection closing.
message(BTRequestStream& bp, bstring req, bool is_timeout = false);
message(BTRequestStream& bp, std::vector<std::byte> req, bool is_timeout = false);

public:
inline static constexpr auto TYPE_REPLY = "R"sv;
inline static constexpr auto TYPE_ERROR = "E"sv;
inline static constexpr auto TYPE_COMMAND = "C"sv;

void respond(bstring_view body, bool error = false) const;
void respond(std::string_view body, bool error = false) const { respond(convert_sv<std::byte>(body), error); }
void respond(bspan body, bool error = false) const;
void respond(std::string_view body, bool error = false) const { respond(str_to_bspan(body), error); }

const bool timed_out{false};
bool is_error() const { return type() == TYPE_ERROR; }
Expand Down Expand Up @@ -152,7 +152,7 @@ namespace oxen::quic

bool is_expired(time_point now) const { return expiry < now; }

message to_timeout() && { return {return_sender, ""_bs, true}; }
message to_timeout() && { return {return_sender, {}, true}; }

private:
void handle_req_opts(std::function<void(message)> func) { cb = std::move(func); }
Expand All @@ -178,8 +178,8 @@ namespace oxen::quic
std::unordered_map<std::string, std::function<void(message)>> func_map;
std::function<void(message)> generic_handler;

bstring buf;
std::string size_buf;
std::vector<std::byte> buf;
std::vector<char> size_buf;

size_t current_len{0};

Expand Down Expand Up @@ -216,7 +216,7 @@ namespace oxen::quic
std::chrono::milliseconds timeout - request timeout (defaults to 10 seconds)
*/
template <typename... Opt>
void command(std::string ep, bstring_view body, Opt&&... opts)
void command(std::string ep, bspan body, Opt&&... opts)
{
auto rid = next_rid++;
auto req = std::make_shared<sent_request>(*this, encode_command(ep, rid, body), rid, std::forward<Opt>(opts)...);
Expand All @@ -233,10 +233,10 @@ namespace oxen::quic
template <typename... Opt>
void command(std::string ep, std::string_view body, Opt&&... opts)
{
command(std::move(ep), convert_sv<std::byte>(body), std::forward<Opt>(opts)...);
command(std::move(ep), str_to_bspan(body), std::forward<Opt>(opts)...);
}

void respond(int64_t rid, bstring_view body, bool error = false);
void respond(int64_t rid, bspan body, bool error = false);

/// Registers an individual endpoint to be recognized by this BTRequestStream object. Can be
/// called multiple times to set up multiple commands. See also register_generic_handler.
Expand All @@ -255,7 +255,7 @@ namespace oxen::quic
void check_timeouts() override;
void check_timeouts(std::optional<std::chrono::steady_clock::time_point> now);

void receive(bstring_view data) override;
void receive(bspan data) override;

void closed(uint64_t app_code) override;

Expand All @@ -269,15 +269,15 @@ namespace oxen::quic

void handle_input(message msg);

void process_incoming(std::string_view req);
void process_incoming(bspan req);

std::string encode_command(std::string_view endpoint, int64_t rid, bstring_view body);
std::string encode_command(std::string_view endpoint, int64_t rid, bspan body);

std::string encode_response(int64_t rid, bstring_view body, bool error);
std::string encode_response(int64_t rid, bspan body, bool error);

sent_request* add_sent_request(std::shared_ptr<sent_request> req);

size_t parse_length(std::string_view req);
size_t parse_length(cspan req);

size_t num_pending_impl() const { return user_buffers.size(); }
};
Expand Down
42 changes: 21 additions & 21 deletions include/oxen/quic/connection.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

#include "connection_ids.hpp"
#include "context.hpp"
#include "format.hpp"
#include "types.hpp"

#include <cstddef>
#include <cstdint>
#include <cstdio>
Expand All @@ -11,11 +16,6 @@
#include <set>
#include <stdexcept>

#include "connection_ids.hpp"
#include "context.hpp"
#include "format.hpp"
#include "types.hpp"

namespace oxen::quic
{
struct dgram_interface;
Expand All @@ -34,7 +34,7 @@ namespace oxen::quic
virtual std::shared_ptr<Stream> get_stream_impl(int64_t id) = 0;

public:
virtual ustring_view selected_alpn() const = 0;
virtual uspan selected_alpn() const = 0;

/// Queues an incoming stream of the given StreamT type, forwarding the given arguments to
/// the StreamT constructor. The stream will be given the next unseen incoming connection
Expand Down Expand Up @@ -119,26 +119,26 @@ namespace oxen::quic
requires(!std::same_as<CharType, std::byte>)
void send_datagram(std::basic_string_view<CharType> data, std::shared_ptr<void> keep_alive = nullptr)
{
send_datagram(convert_sv<std::byte>(data), std::move(keep_alive));
send_datagram(str_to_bspan(data), std::move(keep_alive));
}

template <oxenc::basic_char Char>
void send_datagram(std::vector<Char>&& buf)
{
auto keep_alive = std::make_shared<std::vector<Char>>(std::move(buf));
std::basic_string_view<Char> view{keep_alive->data(), keep_alive->size()};
send_datagram(view, std::move(keep_alive));
send_datagram(str_to_bspan(view), std::move(keep_alive));
}

template <oxenc::basic_char CharType>
void send_datagram(std::basic_string<CharType>&& data)
{
auto keep_alive = std::make_shared<std::basic_string<CharType>>(std::move(data));
std::basic_string_view<CharType> view{*keep_alive};
send_datagram(view, std::move(keep_alive));
send_datagram(str_to_bspan(view), std::move(keep_alive));
}

virtual void send_datagram(bstring_view data, std::shared_ptr<void> keep_alive = nullptr) = 0;
virtual void send_datagram(bspan data, std::shared_ptr<void> keep_alive = nullptr) = 0;

virtual Endpoint& endpoint() = 0;
virtual const Endpoint& endpoint() const = 0;
Expand All @@ -150,7 +150,7 @@ namespace oxen::quic
virtual const ConnectionID& reference_id() const = 0;
virtual bool is_validated() const = 0;
virtual Direction direction() const = 0;
virtual ustring_view remote_key() const = 0;
virtual uspan remote_key() const = 0;
virtual bool is_inbound() const = 0;
virtual bool is_outbound() const = 0;
virtual std::string direction_str() = 0;
Expand Down Expand Up @@ -269,9 +269,9 @@ namespace oxen::quic
const quic_cid& dcid,
const Path& path,
std::shared_ptr<IOContext> ctx,
const std::vector<ustring>& alpns,
const std::vector<std::vector<unsigned char>>& alpns,
std::chrono::nanoseconds default_handshake_timeout,
std::optional<ustring> remote_pk = std::nullopt,
std::optional<std::vector<unsigned char>> remote_pk = std::nullopt,
ngtcp2_pkt_hd* hdr = nullptr,
std::optional<ngtcp2_token_type> token_type = std::nullopt,
ngtcp2_cid* ocid = nullptr);
Expand All @@ -280,7 +280,7 @@ namespace oxen::quic

TLSSession* get_session() const;

ustring_view remote_key() const override;
uspan remote_key() const override;

Direction direction() const override { return dir; }

Expand All @@ -305,7 +305,7 @@ namespace oxen::quic
Endpoint& endpoint() override { return _endpoint; }
const Endpoint& endpoint() const override { return _endpoint; }

ustring_view selected_alpn() const override;
uspan selected_alpn() const override;

uint64_t get_streams_available_impl() const override;
size_t get_max_datagram_size_impl() override;
Expand All @@ -324,7 +324,7 @@ namespace oxen::quic
// public debug functions; to be removed with friend test fixture class
int last_cleared() const override;

void send_datagram(bstring_view data, std::shared_ptr<void> keep_alive = nullptr) override;
void send_datagram(bspan data, std::shared_ptr<void> keep_alive = nullptr) override;

void close_connection(uint64_t error_code = 0) override;

Expand Down Expand Up @@ -381,9 +381,9 @@ namespace oxen::quic
const quic_cid& dcid,
const Path& path,
std::shared_ptr<IOContext> ctx,
const std::vector<ustring>& alpns,
const std::vector<std::vector<unsigned char>>& alpns,
std::chrono::nanoseconds default_handshake_timeout,
std::optional<ustring> remote_pk = std::nullopt,
std::optional<std::vector<unsigned char>> remote_pk = std::nullopt,
ngtcp2_pkt_hd* hdr = nullptr,
std::optional<ngtcp2_token_type> token_type = std::nullopt,
ngtcp2_cid* ocid = nullptr);
Expand Down Expand Up @@ -416,7 +416,7 @@ namespace oxen::quic
std::atomic<bool> _close_quietly{false};
std::atomic<bool> _is_validated{false};

ustring remote_pubkey{};
std::vector<unsigned char> remote_pubkey{};

std::vector<int64_t> _early_streams;

Expand Down Expand Up @@ -505,12 +505,12 @@ namespace oxen::quic
// these are public so ngtcp2 can access them from callbacks
int stream_opened(int64_t id);
int stream_ack(int64_t id, size_t size);
int stream_receive(int64_t id, bstring_view data, bool fin);
int stream_receive(int64_t id, bspan data, bool fin);
void stream_execute_close(Stream& s, uint64_t app_code);
void stream_closed(int64_t id, uint64_t app_code);
void close_all_streams();
void check_pending_streams(uint64_t available, bool is_early_stream = false);
int recv_datagram(bstring_view data, bool fin);
int recv_datagram(bspan data, bool fin);
int ack_datagram(uint64_t dgram_id);
int recv_token(const uint8_t* token, size_t tokenlen);

Expand Down
6 changes: 3 additions & 3 deletions include/oxen/quic/context.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once

#include <memory>
#include <unordered_map>

#include "crypto.hpp"
#include "datagram.hpp"
#include "opt.hpp"
#include "stream.hpp"
#include "udp.hpp"
#include "utils.hpp"

#include <memory>
#include <unordered_map>

namespace oxen::quic
{
// created to store user configuration values; more values to be added later
Expand Down
Loading

0 comments on commit 64304b4

Please sign in to comment.