Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit ea095af

Browse files
committed
quic: more general C++ cleanup
Use default initializers, `const` qualifiers, minor style changes to match more common style in the codebase. PR-URL: #126 Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2e29b5c commit ea095af

File tree

5 files changed

+111
-128
lines changed

5 files changed

+111
-128
lines changed

src/node_quic_socket.cc

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,14 @@ QuicSocket::QuicSocket(
7171
HandleWrap(env, wrap,
7272
reinterpret_cast<uv_handle_t*>(&handle_),
7373
AsyncWrap::PROVIDER_QUICSOCKET),
74-
flags_(QUICSOCKET_FLAGS_NONE),
7574
options_(options),
76-
pending_callbacks_(0),
7775
max_connections_per_host_(max_connections_per_host),
78-
current_ngtcp2_memory_(0),
7976
retry_token_expiration_(retry_token_expiration),
80-
rx_loss_(0.0),
81-
tx_loss_(0.0),
82-
server_secure_context_(nullptr),
8377
server_alpn_(NGTCP2_ALPN_H3),
8478
stats_buffer_(
85-
env->isolate(),
86-
sizeof(socket_stats_) / sizeof(uint64_t),
87-
reinterpret_cast<uint64_t*>(&socket_stats_)),
79+
env->isolate(),
80+
sizeof(socket_stats_) / sizeof(uint64_t),
81+
reinterpret_cast<uint64_t*>(&socket_stats_)),
8882
alloc_info_(MakeAllocator()) {
8983
CHECK_EQ(uv_udp_init(env->event_loop(), &handle_), 0);
9084
Debug(this, "New QuicSocket created.");
@@ -282,23 +276,19 @@ void QuicSocket::OnAlloc(
282276
uv_handle_t* handle,
283277
size_t suggested_size,
284278
uv_buf_t* buf) {
285-
buf->base = node::Malloc(suggested_size);
286-
buf->len = suggested_size;
279+
QuicSocket* socket =
280+
ContainerOf(&QuicSocket::handle_, reinterpret_cast<uv_udp_t*>(handle));
281+
*buf = socket->env()->AllocateManaged(suggested_size).release();
287282
}
288283

289284
void QuicSocket::OnRecv(
290285
uv_udp_t* handle,
291286
ssize_t nread,
292-
const uv_buf_t* buf,
287+
const uv_buf_t* buf_,
293288
const struct sockaddr* addr,
294289
unsigned int flags) {
295-
OnScopeLeave on_scope_leave([&]() {
296-
if (buf->base != nullptr)
297-
free(buf->base);
298-
});
299-
300-
QuicSocket* socket = static_cast<QuicSocket*>(handle->data);
301-
CHECK_NOT_NULL(socket);
290+
QuicSocket* socket = ContainerOf(&QuicSocket::handle_, handle);
291+
AllocatedBuffer buf(socket->env(), *buf_);
302292

303293
if (nread == 0)
304294
return;
@@ -313,12 +303,12 @@ void QuicSocket::OnRecv(
313303
return;
314304
}
315305

316-
socket->Receive(nread, buf, addr, flags);
306+
socket->Receive(nread, std::move(buf), addr, flags);
317307
}
318308

319309
void QuicSocket::Receive(
320310
ssize_t nread,
321-
const uv_buf_t* buf,
311+
AllocatedBuffer buf,
322312
const struct sockaddr* addr,
323313
unsigned int flags) {
324314
Debug(this, "Receiving %d bytes from the UDP socket.", nread);
@@ -332,7 +322,7 @@ void QuicSocket::Receive(
332322

333323
IncrementSocketStat(nread, &socket_stats_, &socket_stats::bytes_received);
334324

335-
const uint8_t* data = reinterpret_cast<const uint8_t*>(buf->base);
325+
const uint8_t* data = reinterpret_cast<const uint8_t*>(buf.data());
336326

337327
uint32_t pversion;
338328
const uint8_t* pdcid;
@@ -365,8 +355,8 @@ void QuicSocket::Receive(
365355
QuicCID dcid(pdcid, pdcidlen);
366356
QuicCID scid(pscid, pscidlen);
367357

368-
auto dcid_hex = dcid.ToHex();
369-
auto dcid_str = dcid.ToStr();
358+
std::string dcid_hex = dcid.ToHex();
359+
std::string dcid_str = dcid.ToStr();
370360
Debug(this, "Received a QUIC packet for dcid %s", dcid_hex.c_str());
371361

372362
// Grabbing a shared pointer to prevent the QuicSession from
@@ -625,11 +615,11 @@ void QuicSocket::SetValidatedAddress(const sockaddr* addr) {
625615
}
626616
}
627617

628-
bool QuicSocket::IsValidatedAddress(const sockaddr* addr) {
618+
bool QuicSocket::IsValidatedAddress(const sockaddr* addr) const {
629619
if (IsOptionSet(QUICSOCKET_OPTIONS_VALIDATE_ADDRESS_LRU)) {
630620
auto res = std::find(std::begin(validated_addrs_),
631621
std::end(validated_addrs_),
632-
addr_hash((addr)));
622+
addr_hash(addr));
633623
return res != std::end(validated_addrs_);
634624
}
635625
return false;
@@ -643,7 +633,6 @@ std::shared_ptr<QuicSession> QuicSocket::AcceptInitialPacket(
643633
const uint8_t* data,
644634
const struct sockaddr* addr,
645635
unsigned int flags) {
646-
std::shared_ptr<QuicSession> session;
647636
HandleScope handle_scope(env()->isolate());
648637
Context::Scope context_scope(env()->context());
649638
ngtcp2_pkt_hd hd;
@@ -653,7 +642,7 @@ std::shared_ptr<QuicSession> QuicSocket::AcceptInitialPacket(
653642

654643
if (!IsFlagSet(QUICSOCKET_FLAGS_SERVER_LISTENING)) {
655644
Debug(this, "QuicSocket is not listening");
656-
return session;
645+
return {};
657646
}
658647

659648
// Perform some initial checks on the packet to see if it is an
@@ -663,9 +652,8 @@ std::shared_ptr<QuicSession> QuicSocket::AcceptInitialPacket(
663652
SendVersionNegotiation(version, dcid, scid, addr);
664653
// Fall-through to ignore packet
665654
case QuicServerSession::InitialPacketResult::PACKET_IGNORE:
666-
return session;
655+
return {};
667656
case QuicServerSession::InitialPacketResult::PACKET_OK:
668-
// Fall-through
669657
break;
670658
}
671659

@@ -711,7 +699,7 @@ std::shared_ptr<QuicSession> QuicSocket::AcceptInitialPacket(
711699
retry_token_expiration_)) {
712700
Debug(this, "A valid retry token was not found. Sending retry.");
713701
SendRetry(version, dcid, scid, addr);
714-
return session;
702+
return {};
715703
}
716704
Debug(this, "A valid retry token was found. Continuing.");
717705
SetValidatedAddress(addr);
@@ -721,7 +709,7 @@ std::shared_ptr<QuicSession> QuicSocket::AcceptInitialPacket(
721709
}
722710
}
723711

724-
session =
712+
std::shared_ptr<QuicSession> session {
725713
QuicServerSession::New(
726714
this,
727715
&server_session_config_,
@@ -732,7 +720,7 @@ std::shared_ptr<QuicSession> QuicSocket::AcceptInitialPacket(
732720
version,
733721
server_alpn_,
734722
server_options_,
735-
initial_connection_close);
723+
initial_connection_close) };
736724
Local<Value> arg = session->object();
737725
MakeCallback(env()->quic_on_session_ready_function(), 1, &arg);
738726

@@ -762,7 +750,7 @@ size_t QuicSocket::GetCurrentSocketAddressCounter(const sockaddr* addr) {
762750
auto it = addr_counts_.find(addr);
763751
if (it == std::end(addr_counts_))
764752
return 0;
765-
return (*it).second;
753+
return it->second;
766754
}
767755

768756
void QuicSocket::SetServerBusy(bool on) {
@@ -841,13 +829,13 @@ void QuicSocket::OnSend(
841829
size_t length,
842830
const char* diagnostic_label) {
843831
IncrementSocketStat(
844-
length,
845-
&socket_stats_,
846-
&socket_stats::bytes_sent);
832+
length,
833+
&socket_stats_,
834+
&socket_stats::bytes_sent);
847835
IncrementSocketStat(
848-
1,
849-
&socket_stats_,
850-
&socket_stats::packets_sent);
836+
1,
837+
&socket_stats_,
838+
&socket_stats::packets_sent);
851839

852840
Debug(this, "Packet sent status: %d (label: %s)",
853841
status,
@@ -928,18 +916,16 @@ QuicSocket::SendWrap::SendWrap(
928916
SocketAddress* dest,
929917
QuicBuffer* buffer,
930918
std::shared_ptr<QuicSession> session,
931-
const char* diagnostic_label) :
932-
SendWrapBase(socket, **dest, diagnostic_label),
933-
buffer_(buffer),
934-
session_(session) {}
919+
const char* diagnostic_label)
920+
: SendWrap(socket, **dest, buffer, session, diagnostic_label) {}
935921

936922
QuicSocket::SendWrap::SendWrap(
937923
QuicSocket* socket,
938924
const sockaddr* dest,
939925
QuicBuffer* buffer,
940926
std::shared_ptr<QuicSession> session,
941-
const char* diagnostic_label) :
942-
SendWrapBase(socket, dest, diagnostic_label),
927+
const char* diagnostic_label)
928+
: SendWrapBase(socket, dest, diagnostic_label),
943929
buffer_(buffer),
944930
session_(session) {}
945931

@@ -1029,12 +1015,15 @@ void NewQuicSocket(const FunctionCallbackInfo<Value>& args) {
10291015
Environment* env = Environment::GetCurrent(args);
10301016
CHECK(args.IsConstructCall());
10311017

1032-
uint32_t options = 0;
1033-
USE(args[0]->Uint32Value(env->context()).To(&options));
1034-
uint32_t retry_token_expiration = DEFAULT_RETRYTOKEN_EXPIRATION;
1035-
uint32_t max_connections_per_host = DEFAULT_MAX_CONNECTIONS_PER_HOST;
1036-
USE(args[1]->Uint32Value(env->context()).To(&retry_token_expiration));
1037-
USE(args[2]->Uint32Value(env->context()).To(&max_connections_per_host));
1018+
uint32_t options;
1019+
uint32_t retry_token_expiration;
1020+
uint32_t max_connections_per_host;
1021+
1022+
if (!args[0]->Uint32Value(env->context()).To(&options) ||
1023+
!args[1]->Uint32Value(env->context()).To(&retry_token_expiration) ||
1024+
!args[2]->Uint32Value(env->context()).To(&max_connections_per_host)) {
1025+
return;
1026+
}
10381027
CHECK_GE(retry_token_expiration, MIN_RETRYTOKEN_EXPIRATION);
10391028
CHECK_LE(retry_token_expiration, MAX_RETRYTOKEN_EXPIRATION);
10401029

@@ -1054,14 +1043,13 @@ void NewQuicSocket(const FunctionCallbackInfo<Value>& args) {
10541043
// arguments to a value between 0.0 and 1.0. Setting both values to 0.0
10551044
// disables the mechanism.
10561045
void QuicSocketSetDiagnosticPacketLoss(
1057-
const FunctionCallbackInfo<Value>& args) {
1046+
const FunctionCallbackInfo<Value>& args) {
10581047
Environment* env = Environment::GetCurrent(args);
10591048
QuicSocket* socket;
10601049
ASSIGN_OR_RETURN_UNWRAP(&socket, args.Holder());
1061-
double rx = 0.0f;
1062-
double tx = 0.0f;
1063-
USE(args[0]->NumberValue(env->context()).To(&rx));
1064-
USE(args[1]->NumberValue(env->context()).To(&tx));
1050+
double rx, tx;
1051+
if (!args[0]->NumberValue(env->context()).To(&rx) ||
1052+
!args[1]->NumberValue(env->context()).To(&tx)) return;
10651053
CHECK_GE(rx, 0.0f);
10661054
CHECK_GE(tx, 0.0f);
10671055
CHECK_LE(rx, 1.0f);
@@ -1128,7 +1116,8 @@ void QuicSocketListen(const FunctionCallbackInfo<Value>& args) {
11281116
QuicSocket* socket;
11291117
ASSIGN_OR_RETURN_UNWRAP(&socket, args.Holder(),
11301118
args.GetReturnValue().Set(UV_EBADF));
1131-
CHECK(args[0]->IsObject()); // Secure Context
1119+
CHECK(args[0]->IsObject() &&
1120+
env->secure_context_constructor_template()->HasInstance(args[0]));
11321121
SecureContext* sc;
11331122
ASSIGN_OR_RETURN_UNWRAP(&sc, args[0].As<Object>(),
11341123
args.GetReturnValue().Set(UV_EBADF));
@@ -1140,9 +1129,10 @@ void QuicSocketListen(const FunctionCallbackInfo<Value>& args) {
11401129
node::Utf8Value preferred_address_host(args.GetIsolate(), args[1]);
11411130
int32_t preferred_address_family;
11421131
uint32_t preferred_address_port;
1143-
if (args[2]->Int32Value(env->context()).To(&preferred_address_family) &&
1144-
args[3]->Uint32Value(env->context()).To(&preferred_address_port) &&
1145-
SocketAddress::ToSockAddr(
1132+
if (!args[2]->Int32Value(env->context()).To(&preferred_address_family) ||
1133+
!args[3]->Uint32Value(env->context()).To(&preferred_address_port))
1134+
return;
1135+
if (SocketAddress::ToSockAddr(
11461136
preferred_address_family,
11471137
*preferred_address_host,
11481138
preferred_address_port,
@@ -1160,7 +1150,7 @@ void QuicSocketListen(const FunctionCallbackInfo<Value>& args) {
11601150
}
11611151

11621152
uint32_t options = 0;
1163-
USE(args[5]->Uint32Value(env->context()).To(&options));
1153+
if (!args[5]->Uint32Value(env->context()).To(&options)) return;
11641154

11651155
socket->Listen(sc, preferred_address, alpn, options);
11661156
}

src/node_quic_socket.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class QuicSocket : public HandleWrap,
146146

147147
void Receive(
148148
ssize_t nread,
149-
const uv_buf_t* buf,
149+
AllocatedBuffer buf,
150150
const struct sockaddr* addr,
151151
unsigned int flags);
152152

@@ -168,8 +168,7 @@ class QuicSocket : public HandleWrap,
168168
const char* diagnostic_label);
169169

170170
void SetValidatedAddress(const sockaddr* addr);
171-
172-
bool IsValidatedAddress(const sockaddr* addr);
171+
bool IsValidatedAddress(const sockaddr* addr) const;
173172

174173
std::shared_ptr<QuicSession> AcceptInitialPacket(
175174
uint32_t version,
@@ -223,7 +222,7 @@ class QuicSocket : public HandleWrap,
223222
flags_ &= ~flag;
224223
}
225224

226-
bool IsFlagSet(QuicSocketFlags flag) {
225+
bool IsFlagSet(QuicSocketFlags flag) const {
227226
return flags_ & flag;
228227
}
229228

@@ -234,28 +233,28 @@ class QuicSocket : public HandleWrap,
234233
options_ &= ~option;
235234
}
236235

237-
bool IsOptionSet(QuicSocketOptions option) {
236+
bool IsOptionSet(QuicSocketOptions option) const {
238237
return options_ & option;
239238
}
240239

241240
uv_udp_t handle_;
242-
uint32_t flags_;
241+
uint32_t flags_ = QUICSOCKET_FLAGS_NONE;
243242
uint32_t options_;
244243
uint32_t server_options_;
245244

246-
size_t pending_callbacks_;
245+
size_t pending_callbacks_ = 0;
247246
size_t max_connections_per_host_;
248-
size_t current_ngtcp2_memory_;
247+
size_t current_ngtcp2_memory_ = 0;
249248

250249
uint64_t retry_token_expiration_;
251250

252251
// Used to specify diagnostic packet loss probabilities
253-
double rx_loss_;
254-
double tx_loss_;
252+
double rx_loss_ = 0.0;
253+
double tx_loss_ = 0.0;
255254

256255
SocketAddress local_address_;
257256
QuicSessionConfig server_session_config_;
258-
crypto::SecureContext* server_secure_context_;
257+
crypto::SecureContext* server_secure_context_ = nullptr;
259258
std::string server_alpn_;
260259
std::unordered_map<std::string, std::shared_ptr<QuicSession>> sessions_;
261260
std::unordered_map<std::string, std::string> dcid_to_scid_;
@@ -270,7 +269,7 @@ class QuicSocket : public HandleWrap,
270269
// attempts to create new connections will be ignored
271270
// until the value falls back below the limit.
272271
std::unordered_map<const sockaddr*, size_t, SocketAddress::Hash>
273-
addr_counts_;
272+
addr_counts_;
274273

275274
// The validated_addrs_ vector is used as an LRU cache for
276275
// validated addresses only when the VALIDATE_ADDRESS_LRU

0 commit comments

Comments
 (0)