Skip to content

Commit

Permalink
quic: apply format-cpp to multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Mar 27, 2023
1 parent 6e1b1b8 commit abdf755
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 110 deletions.
35 changes: 23 additions & 12 deletions src/quic/cid.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "cid.h"
#include <crypto/crypto_util.h>
#include <memory_tracker-inl.h>
#include <node_mutex.h>
#include <string_bytes.h>
#include <crypto/crypto_util.h>

namespace node {
namespace quic {

// ============================================================================
// CID

CID::CID() : ptr_(&cid_) { cid_.datalen = 0; }
CID::CID() : ptr_(&cid_) {
cid_.datalen = 0;
}

CID::CID(const ngtcp2_cid& cid) : CID() {
DCHECK_GE(cid.datalen, kMinLength);
Expand All @@ -36,8 +38,7 @@ CID::CID(const CID& other) : ptr_(&cid_) {
}

bool CID::operator==(const CID& other) const noexcept {
if (this == &other || (length() == 0 && other.length() == 0))
return true;
if (this == &other || (length() == 0 && other.length() == 0)) return true;
if (length() != other.length()) return false;
return memcmp(ptr_->data, other.ptr_->data, ptr_->datalen) == 0;
}
Expand All @@ -46,12 +47,22 @@ bool CID::operator!=(const CID& other) const noexcept {
return !(*this == other);
}

CID::operator const uint8_t*() const { return ptr_->data; }
CID::operator const ngtcp2_cid&() const { return *ptr_; }
CID::operator const ngtcp2_cid*() const { return ptr_; }
CID::operator bool() const { return ptr_->datalen >= kMinLength; }
CID::operator const uint8_t*() const {
return ptr_->data;
}
CID::operator const ngtcp2_cid&() const {
return *ptr_;
}
CID::operator const ngtcp2_cid*() const {
return ptr_;
}
CID::operator bool() const {
return ptr_->datalen >= kMinLength;
}

size_t CID::length() const { return ptr_->datalen; }
size_t CID::length() const {
return ptr_->datalen;
}

std::string CID::ToString() const {
char dest[kMaxLength * 2];
Expand All @@ -63,16 +74,16 @@ std::string CID::ToString() const {
return std::string(dest, written);
}

CID CID::kInvalid {};
CID CID::kInvalid{};

// ============================================================================
// CID::Hash

size_t CID::Hash::operator()(const CID& cid) const {
size_t hash = 0;
for (size_t n = 0; n < cid.length(); n++) {
hash ^= std::hash<uint8_t>{}(cid.ptr_->data[n] + 0x9e3779b9 +
(hash << 6) + (hash >> 2));
hash ^= std::hash<uint8_t>{}(cid.ptr_->data[n] + 0x9e3779b9 + (hash << 6) +
(hash >> 2));
}
return hash;
}
Expand Down
2 changes: 1 addition & 1 deletion src/quic/cid.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace quic {
// While the connection is being established, it is possible for either
// peer to generate additional CIDs that are also associated with the
// connection.
class CID final: public MemoryRetainer {
class CID final : public MemoryRetainer {
public:
static constexpr size_t kMinLength = NGTCP2_MIN_CIDLEN;
static constexpr size_t kMaxLength = NGTCP2_MAX_CIDLEN;
Expand Down
115 changes: 49 additions & 66 deletions src/quic/data.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "data.h"
#include <env-inl.h>
#include <memory_tracker-inl.h>
#include <node_sockaddr-inl.h>
#include <ngtcp2/ngtcp2.h>
#include <node_sockaddr-inl.h>
#include <v8.h>
#include "util.h"

Expand All @@ -23,27 +23,27 @@ Path::Path(const SocketAddress& local, const SocketAddress& remote) {
ngtcp2_addr_init(&this->remote, remote.data(), remote.length());
}

PathStorage::PathStorage() { ngtcp2_path_storage_zero(this); }
PathStorage::operator ngtcp2_path() { return path; }
PathStorage::PathStorage() {
ngtcp2_path_storage_zero(this);
}
PathStorage::operator ngtcp2_path() {
return path;
}

// ============================================================================

Store::Store(std::shared_ptr<v8::BackingStore> store,
size_t length,
size_t offset)
: store_(std::move(store)),
length_(length),
offset_(offset) {
size_t length,
size_t offset)
: store_(std::move(store)), length_(length), offset_(offset) {
CHECK_LE(offset_, store->ByteLength());
CHECK_LE(length_, store->ByteLength() - offset_);
}

Store::Store(std::unique_ptr<v8::BackingStore> store,
size_t length,
size_t offset)
: store_(std::move(store)),
length_(length),
offset_(offset) {
size_t length,
size_t offset)
: store_(std::move(store)), length_(length), offset_(offset) {
CHECK_LE(offset_, store->ByteLength());
CHECK_LE(length_, store->ByteLength() - offset_);
}
Expand All @@ -64,15 +64,18 @@ Store::Store(v8::Local<v8::ArrayBufferView> view, Option option)
}
}

Store::operator bool() const { return store_ != nullptr; }
size_t Store::length() const { return length_; }
Store::operator bool() const {
return store_ != nullptr;
}
size_t Store::length() const {
return length_;
}

template <typename T, typename t>
T Store::convert() const {
T buf;
buf.base = store_ != nullptr ?
static_cast<t*>(store_->Data()) + offset_ :
nullptr;
buf.base =
store_ != nullptr ? static_cast<t*>(store_->Data()) + offset_ : nullptr;
buf.len = length_;
return buf;
}
Expand All @@ -98,18 +101,21 @@ void Store::MemoryInfo(MemoryTracker* tracker) const {
namespace {
std::string TypeName(QuicError::Type type) {
switch (type) {
case QuicError::Type::APPLICATION: return "APPLICATION";
case QuicError::Type::TRANSPORT: return "TRANSPORT";
case QuicError::Type::VERSION_NEGOTIATION: return "VERSION_NEGOTIATION";
case QuicError::Type::IDLE_CLOSE: return "IDLE_CLOSE";
case QuicError::Type::APPLICATION:
return "APPLICATION";
case QuicError::Type::TRANSPORT:
return "TRANSPORT";
case QuicError::Type::VERSION_NEGOTIATION:
return "VERSION_NEGOTIATION";
case QuicError::Type::IDLE_CLOSE:
return "IDLE_CLOSE";
}
UNREACHABLE();
}
} // namespace

QuicError::QuicError(const std::string_view reason)
: reason_(reason),
ptr_(&error_) {}
: reason_(reason), ptr_(&error_) {}

QuicError::QuicError(const ngtcp2_connection_close_error* ptr)
: reason_(reinterpret_cast<const char*>(ptr->reason), ptr->reasonlen),
Expand All @@ -122,7 +128,7 @@ QuicError::QuicError(const ngtcp2_connection_close_error& error)

QuicError::operator bool() const {
if ((code() == NO_ERROR && type() == Type::TRANSPORT) ||
((code() == APP_NO_ERROR && type() == Type::APPLICATION))) {
((code() == APP_NO_ERROR && type() == Type::APPLICATION))) {
return false;
}
return true;
Expand All @@ -138,8 +144,7 @@ bool QuicError::operator!=(const QuicError& other) const {

bool QuicError::operator==(const QuicError& other) const {
if (this == &other) return true;
return type() == other.type() &&
code() == other.code() &&
return type() == other.type() && code() == other.code() &&
frame_type() == other.frame_type();
}

Expand Down Expand Up @@ -169,9 +174,9 @@ QuicError::operator const ngtcp2_connection_close_error*() const {

MaybeLocal<Value> QuicError::ToV8Value(Environment* env) const {
Local<Value> argv[] = {
Integer::New(env->isolate(), static_cast<int>(type())),
BigInt::NewFromUnsigned(env->isolate(), code()),
Undefined(env->isolate()),
Integer::New(env->isolate(), static_cast<int>(type())),
BigInt::NewFromUnsigned(env->isolate(), code()),
Undefined(env->isolate()),
};

if (reason_.length() > 0 &&
Expand All @@ -193,61 +198,41 @@ void QuicError::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("reason", reason_.length());
}

QuicError QuicError::ForTransport(
error_code code,
const std::string_view reason) {
QuicError QuicError::ForTransport(error_code code,
const std::string_view reason) {
QuicError error(reason);
ngtcp2_connection_close_error_set_transport_error(
&error.error_,
code,
error.reason_c_str(),
reason.length());
&error.error_, code, error.reason_c_str(), reason.length());
return error;
}

QuicError QuicError::ForApplication(
error_code code,
const std::string_view reason) {
QuicError QuicError::ForApplication(error_code code,
const std::string_view reason) {
QuicError error(reason);
ngtcp2_connection_close_error_set_application_error(
&error.error_,
code,
error.reason_c_str(),
reason.length());
&error.error_, code, error.reason_c_str(), reason.length());
return error;
}

QuicError QuicError::ForVersionNegotiation(
const std::string_view reason) {
QuicError QuicError::ForVersionNegotiation(const std::string_view reason) {
return ForNgtcp2Error(NGTCP2_ERR_RECV_VERSION_NEGOTIATION, reason);
}

QuicError QuicError::ForIdleClose(
const std::string_view reason) {
QuicError QuicError::ForIdleClose(const std::string_view reason) {
return ForNgtcp2Error(NGTCP2_ERR_IDLE_CLOSE, reason);
}

QuicError QuicError::ForNgtcp2Error(
int code,
const std::string_view reason) {
QuicError QuicError::ForNgtcp2Error(int code, const std::string_view reason) {
QuicError error(reason);
ngtcp2_connection_close_error_set_transport_error_liberr(
&error.error_,
code,
error.reason_c_str(),
reason.length());
&error.error_, code, error.reason_c_str(), reason.length());
return error;
}

QuicError QuicError::ForTlsAlert(
int code,
const std::string_view reason) {
QuicError QuicError::ForTlsAlert(int code, const std::string_view reason) {
QuicError error(reason);
ngtcp2_connection_close_error_set_transport_error_tls_alert(
&error.error_,
code,
error.reason_c_str(),
reason.length());
&error.error_, code, error.reason_c_str(), reason.length());
return error;
}

Expand All @@ -261,10 +246,8 @@ QuicError QuicError::TRANSPORT_NO_ERROR =
QuicError::ForTransport(QuicError::NO_ERROR);
QuicError QuicError::APPLICATION_NO_ERROR =
QuicError::ForApplication(QuicError::APP_NO_ERROR);
QuicError QuicError::VERSION_NEGOTIATION =
QuicError::ForVersionNegotiation();
QuicError QuicError::IDLE_CLOSE =
QuicError::ForIdleClose();
QuicError QuicError::VERSION_NEGOTIATION = QuicError::ForVersionNegotiation();
QuicError QuicError::IDLE_CLOSE = QuicError::ForIdleClose();
QuicError QuicError::INTERNAL_ERROR =
QuicError::ForNgtcp2Error(NGTCP2_ERR_INTERNAL);

Expand Down
9 changes: 5 additions & 4 deletions src/quic/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <memory_tracker.h>
#include <nghttp3/nghttp3.h>
#include <ngtcp2/ngtcp2.h>
#include <node_internals.h>
#include <node_sockaddr.h>
#include <ngtcp2/ngtcp2.h>
#include <nghttp3/nghttp3.h>
#include <v8.h>

namespace node {
Expand All @@ -16,7 +16,7 @@ struct Path final : public ngtcp2_path {
Path(const SocketAddress& local, const SocketAddress& remote);
};

struct PathStorage final: public ngtcp2_path_storage {
struct PathStorage final : public ngtcp2_path_storage {
PathStorage();
operator ngtcp2_path();
};
Expand Down Expand Up @@ -51,7 +51,8 @@ class Store final : public MemoryRetainer {
SET_SELF_SIZE(Store);

private:
template <typename T, typename t> T convert() const;
template <typename T, typename t>
T convert() const;
std::shared_ptr<v8::BackingStore> store_;
size_t length_ = 0;
size_t offset_ = 0;
Expand Down

0 comments on commit abdf755

Please sign in to comment.