Skip to content

Commit 2484083

Browse files
jasnellMoLow
authored andcommitted
quic: add more QUIC impl
* add BindingData * add LogStream * add TransportParams PR-URL: #47348 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent d0bda90 commit 2484083

10 files changed

+1082
-0
lines changed

src/async_wrap.h

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ namespace node {
6060
V(PROCESSWRAP) \
6161
V(PROMISE) \
6262
V(QUERYWRAP) \
63+
V(QUIC_LOGSTREAM) \
6364
V(SHUTDOWNWRAP) \
6465
V(SIGNALWRAP) \
6566
V(STATWATCHER) \

src/base_object_types.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#ifndef SRC_BASE_OBJECT_TYPES_H_
2+
#define SRC_BASE_OBJECT_TYPES_H_
3+
4+
#include <cinttypes>
5+
6+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
7+
8+
namespace node {
9+
// List of internalBinding() data wrappers. The first argument should match
10+
// what the class passes to SET_BINDING_ID(), the second argument should match
11+
// the C++ class name.
12+
#define SERIALIZABLE_BINDING_TYPES(V) \
13+
V(encoding_binding_data, encoding_binding::BindingData) \
14+
V(fs_binding_data, fs::BindingData) \
15+
V(v8_binding_data, v8_utils::BindingData) \
16+
V(blob_binding_data, BlobBindingData) \
17+
V(process_binding_data, process::BindingData) \
18+
V(timers_binding_data, timers::BindingData) \
19+
V(url_binding_data, url::BindingData)
20+
21+
#define UNSERIALIZABLE_BINDING_TYPES(V) \
22+
V(http2_binding_data, http2::BindingData) \
23+
V(http_parser_binding_data, http_parser::BindingData) \
24+
V(quic_binding_data, quic::BindingData)
25+
26+
// List of (non-binding) BaseObjects that are serializable in the snapshot.
27+
// The first argument should match what the type passes to
28+
// SET_OBJECT_ID(), the second argument should match the C++ class
29+
// name.
30+
#define SERIALIZABLE_NON_BINDING_TYPES(V) \
31+
V(util_weak_reference, util::WeakReference)
32+
33+
// Helper list of all binding data wrapper types.
34+
#define BINDING_TYPES(V) \
35+
SERIALIZABLE_BINDING_TYPES(V) \
36+
UNSERIALIZABLE_BINDING_TYPES(V)
37+
38+
// Helper list of all BaseObjects that implement snapshot support.
39+
#define SERIALIZABLE_OBJECT_TYPES(V) \
40+
SERIALIZABLE_BINDING_TYPES(V) \
41+
SERIALIZABLE_NON_BINDING_TYPES(V)
42+
43+
#define V(TypeId, NativeType) k_##TypeId,
44+
enum class BindingDataType : uint8_t { BINDING_TYPES(V) kBindingDataTypeCount };
45+
// Make sure that we put the bindings first so that we can also use the enums
46+
// for the bindings as index to the binding data store.
47+
enum class EmbedderObjectType : uint8_t {
48+
BINDING_TYPES(V) SERIALIZABLE_NON_BINDING_TYPES(V)
49+
// We do not need to know about all the unserializable non-binding types for
50+
// now so we do not list them.
51+
kEmbedderObjectTypeCount
52+
};
53+
#undef V
54+
55+
// For now, BaseObjects only need to call this when they implement snapshot
56+
// support.
57+
#define SET_OBJECT_ID(TypeId) \
58+
static constexpr EmbedderObjectType type_int = EmbedderObjectType::k_##TypeId;
59+
60+
// Binding data should call this so that they can be looked up from the binding
61+
// data store.
62+
#define SET_BINDING_ID(TypeId) \
63+
static constexpr BindingDataType binding_type_int = \
64+
BindingDataType::k_##TypeId; \
65+
SET_OBJECT_ID(TypeId) \
66+
static_assert(static_cast<uint8_t>(type_int) == \
67+
static_cast<uint8_t>(binding_type_int));
68+
69+
} // namespace node
70+
71+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
72+
73+
#endif // SRC_BASE_OBJECT_TYPES_H_

src/quic/bindingdata.cc

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#if HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
2+
#include "bindingdata.h"
3+
#include <base_object-inl.h>
4+
#include <env-inl.h>
5+
#include <memory_tracker-inl.h>
6+
#include <nghttp3/nghttp3.h>
7+
#include <ngtcp2/ngtcp2.h>
8+
#include <node.h>
9+
#include <node_errors.h>
10+
#include <node_external_reference.h>
11+
#include <node_mem-inl.h>
12+
#include <node_realm-inl.h>
13+
#include <v8.h>
14+
15+
namespace node {
16+
17+
using v8::Function;
18+
using v8::FunctionCallbackInfo;
19+
using v8::FunctionTemplate;
20+
using v8::Local;
21+
using v8::Object;
22+
using v8::String;
23+
using v8::Value;
24+
25+
namespace quic {
26+
27+
BindingData& BindingData::Get(Environment* env) {
28+
return *Realm::GetBindingData<BindingData>(env->context());
29+
}
30+
31+
BindingData::operator ngtcp2_mem() {
32+
return MakeAllocator();
33+
}
34+
35+
BindingData::operator nghttp3_mem() {
36+
ngtcp2_mem allocator = *this;
37+
nghttp3_mem http3_allocator = {
38+
allocator.user_data,
39+
allocator.malloc,
40+
allocator.free,
41+
allocator.calloc,
42+
allocator.realloc,
43+
};
44+
return http3_allocator;
45+
}
46+
47+
void BindingData::CheckAllocatedSize(size_t previous_size) const {
48+
CHECK_GE(current_ngtcp2_memory_, previous_size);
49+
}
50+
51+
void BindingData::IncreaseAllocatedSize(size_t size) {
52+
current_ngtcp2_memory_ += size;
53+
}
54+
55+
void BindingData::DecreaseAllocatedSize(size_t size) {
56+
current_ngtcp2_memory_ -= size;
57+
}
58+
59+
void BindingData::Initialize(Environment* env, Local<Object> target) {
60+
SetMethod(env->context(), target, "setCallbacks", SetCallbacks);
61+
Realm::GetCurrent(env->context())
62+
->AddBindingData<BindingData>(env->context(), target);
63+
}
64+
65+
void BindingData::RegisterExternalReferences(
66+
ExternalReferenceRegistry* registry) {
67+
registry->Register(SetCallbacks);
68+
}
69+
70+
BindingData::BindingData(Realm* realm, Local<Object> object)
71+
: BaseObject(realm, object) {
72+
MakeWeak();
73+
}
74+
75+
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
76+
#define V(name, _) tracker->TrackField(#name, name##_callback());
77+
78+
QUIC_JS_CALLBACKS(V)
79+
80+
#undef V
81+
82+
#define V(name, _) tracker->TrackField(#name, name##_string());
83+
84+
QUIC_STRINGS(V)
85+
86+
#undef V
87+
}
88+
89+
#define V(name) \
90+
void BindingData::set_##name##_constructor_template( \
91+
Local<FunctionTemplate> tmpl) { \
92+
name##_constructor_template_.Reset(env()->isolate(), tmpl); \
93+
} \
94+
Local<FunctionTemplate> BindingData::name##_constructor_template() const { \
95+
return PersistentToLocal::Default(env()->isolate(), \
96+
name##_constructor_template_); \
97+
}
98+
99+
QUIC_CONSTRUCTORS(V)
100+
101+
#undef V
102+
103+
#define V(name, _) \
104+
void BindingData::set_##name##_callback(Local<Function> fn) { \
105+
name##_callback_.Reset(env()->isolate(), fn); \
106+
} \
107+
Local<Function> BindingData::name##_callback() const { \
108+
return PersistentToLocal::Default(env()->isolate(), name##_callback_); \
109+
}
110+
111+
QUIC_JS_CALLBACKS(V)
112+
113+
#undef V
114+
115+
#define V(name, value) \
116+
Local<String> BindingData::name##_string() const { \
117+
if (name##_string_.IsEmpty()) \
118+
name##_string_.Set(env()->isolate(), \
119+
OneByteString(env()->isolate(), value)); \
120+
return name##_string_.Get(env()->isolate()); \
121+
}
122+
123+
QUIC_STRINGS(V)
124+
125+
#undef V
126+
127+
#define V(name, value) \
128+
Local<String> BindingData::on_##name##_string() const { \
129+
if (on_##name##_string_.IsEmpty()) \
130+
on_##name##_string_.Set( \
131+
env()->isolate(), \
132+
FIXED_ONE_BYTE_STRING(env()->isolate(), "on" #value)); \
133+
return on_##name##_string_.Get(env()->isolate()); \
134+
}
135+
136+
QUIC_JS_CALLBACKS(V)
137+
138+
#undef V
139+
140+
void BindingData::SetCallbacks(const FunctionCallbackInfo<Value>& args) {
141+
auto env = Environment::GetCurrent(args);
142+
auto isolate = env->isolate();
143+
BindingData& state = BindingData::Get(env);
144+
CHECK(args[0]->IsObject());
145+
Local<Object> obj = args[0].As<Object>();
146+
147+
#define V(name, key) \
148+
do { \
149+
Local<Value> val; \
150+
if (!obj->Get(env->context(), state.on_##name##_string()).ToLocal(&val) || \
151+
!val->IsFunction()) { \
152+
return THROW_ERR_MISSING_ARGS(isolate, "Missing Callback: on" #key); \
153+
} \
154+
state.set_##name##_callback(val.As<Function>()); \
155+
} while (0);
156+
157+
QUIC_JS_CALLBACKS(V)
158+
159+
#undef V
160+
}
161+
162+
} // namespace quic
163+
} // namespace node
164+
165+
#endif // HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC

0 commit comments

Comments
 (0)