Skip to content

Commit

Permalink
quic: refactor native object flags for better readability
Browse files Browse the repository at this point in the history
Use is_* and set_* pattern for native object flags to improve
readability in the code.

PR-URL: #34160
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell committed Jul 5, 2020
1 parent b1750a4 commit b5bf5bb
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 299 deletions.
8 changes: 5 additions & 3 deletions lib/internal/quic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,9 +966,11 @@ class QuicSocket extends EventEmitter {
state.lookup = lookup || (type === AF_INET6 ? lookup6 : lookup4);
state.server = server;

const socketOptions =
(validateAddress ? QUICSOCKET_OPTIONS_VALIDATE_ADDRESS : 0) |
(validateAddressLRU ? QUICSOCKET_OPTIONS_VALIDATE_ADDRESS_LRU : 0);
let socketOptions = 0;
if (validateAddress)
socketOptions |= (1 << QUICSOCKET_OPTIONS_VALIDATE_ADDRESS);
if (validateAddressLRU)
socketOptions |= (1 << QUICSOCKET_OPTIONS_VALIDATE_ADDRESS_LRU);

this[kSetHandle](
new QuicSocketHandle(
Expand Down
2 changes: 1 addition & 1 deletion src/quic/node_quic_default_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ bool DefaultApplication::ReceiveStreamData(
BaseObjectPtr<QuicStream> stream = session()->FindStream(stream_id);
if (!stream) {
// Shutdown the stream explicitly if the session is being closed.
if (session()->is_gracefully_closing()) {
if (session()->is_graceful_closing()) {
ngtcp2_conn_shutdown_stream(
session()->connection(),
stream_id,
Expand Down
2 changes: 1 addition & 1 deletion src/quic/node_quic_http3_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ BaseObjectPtr<QuicStream> Http3Application::FindOrCreateStream(
int64_t stream_id) {
BaseObjectPtr<QuicStream> stream = session()->FindStream(stream_id);
if (!stream) {
if (session()->is_gracefully_closing()) {
if (session()->is_graceful_closing()) {
nghttp3_conn_close_stream(connection(), stream_id, NGTCP2_ERR_CLOSING);
return {};
}
Expand Down
43 changes: 17 additions & 26 deletions src/quic/node_quic_session-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ void QuicCryptoContext::Keylog(const char* line) {
void QuicCryptoContext::OnClientHelloDone() {
// Continue the TLS handshake when this function exits
// otherwise it will stall and fail.
TLSHandshakeScope handshake(this, &in_client_hello_);
TLSHandshakeScope handshake_scope(
this,
[&]() { set_in_client_hello(false); });

// Disable the callback at this point so we don't loop continuously
session_->state_[IDX_QUIC_SESSION_STATE_CLIENT_HELLO_ENABLED] = 0;
}
Expand All @@ -129,8 +132,8 @@ void QuicCryptoContext::ResumeHandshake() {
// For 0RTT, this sets the TLS session data from the given buffer.
bool QuicCryptoContext::set_session(crypto::SSLSessionPointer session) {
if (side_ == NGTCP2_CRYPTO_SIDE_CLIENT && session != nullptr) {
early_data_ =
SSL_SESSION_get_max_early_data(session.get()) == 0xffffffffUL;
set_early_data(
SSL_SESSION_get_max_early_data(session.get()) == 0xffffffffUL);
}
return crypto::SetTLSSession(ssl_, std::move(session));
}
Expand Down Expand Up @@ -186,7 +189,7 @@ std::string QuicCryptoContext::selected_alpn() const {

bool QuicCryptoContext::early_data() const {
return
(early_data_ &&
(is_early_data() &&
SSL_get_early_data_status(ssl_.get()) == SSL_EARLY_DATA_ACCEPTED) ||
SSL_get_max_early_data(ssl_.get()) == 0xffffffffUL;
}
Expand Down Expand Up @@ -300,13 +303,13 @@ void QuicSession::ExtendOffset(size_t amount) {

// Copies the local transport params into the given struct for serialization.
void QuicSession::GetLocalTransportParams(ngtcp2_transport_params* params) {
CHECK(!is_flag_set(QUICSESSION_FLAG_DESTROYED));
CHECK(!is_destroyed());
ngtcp2_conn_get_local_transport_params(connection(), params);
}

// Gets the QUIC version negotiated for this QuicSession
uint32_t QuicSession::negotiated_version() const {
CHECK(!is_flag_set(QUICSESSION_FLAG_DESTROYED));
CHECK(!is_destroyed());
return ngtcp2_conn_get_negotiated_version(connection());
}

Expand All @@ -328,7 +331,7 @@ void QuicSession::HandshakeConfirmed() {
}

bool QuicSession::is_handshake_completed() const {
DCHECK(!is_flag_set(QUICSESSION_FLAG_DESTROYED));
DCHECK(!is_destroyed());
return ngtcp2_conn_get_handshake_completed(connection());
}

Expand All @@ -342,7 +345,7 @@ void QuicSession::InitApplication() {
// immediately closed without attempting to send any additional data to
// the peer. All existing streams are abandoned and closed.
void QuicSession::OnIdleTimeout() {
if (!is_flag_set(QUICSESSION_FLAG_DESTROYED)) {
if (!is_destroyed()) {
state_[IDX_QUIC_SESSION_STATE_IDLE_TIMEOUT] = 1;
Debug(this, "Idle timeout");
SilentClose();
Expand All @@ -359,7 +362,7 @@ void QuicSession::GetConnectionCloseInfo() {

// Removes the given connection id from the QuicSession.
void QuicSession::RemoveConnectionID(const QuicCID& cid) {
if (!is_flag_set(QUICSESSION_FLAG_DESTROYED))
if (!is_destroyed())
DisassociateCID(cid);
}

Expand Down Expand Up @@ -440,24 +443,12 @@ SessionTicketAppData::Status QuicSession::GetSessionTicketAppData(
return application_->GetSessionTicketAppData(app_data, flag);
}

bool QuicSession::is_gracefully_closing() const {
return is_flag_set(QUICSESSION_FLAG_GRACEFUL_CLOSING);
}

bool QuicSession::is_destroyed() const {
return is_flag_set(QUICSESSION_FLAG_DESTROYED);
}

bool QuicSession::is_stateless_reset() const {
return is_flag_set(QUICSESSION_FLAG_STATELESS_RESET);
}

bool QuicSession::is_server() const {
return crypto_context_->side() == NGTCP2_CRYPTO_SIDE_SERVER;
}

void QuicSession::StartGracefulClose() {
set_flag(QUICSESSION_FLAG_GRACEFUL_CLOSING);
set_graceful_closing();
RecordTimestamp(&QuicSessionStats::closing_at);
}

Expand Down Expand Up @@ -511,15 +502,15 @@ bool QuicSession::SendPacket(
}

void QuicSession::set_local_address(const ngtcp2_addr* addr) {
DCHECK(!is_flag_set(QUICSESSION_FLAG_DESTROYED));
DCHECK(!is_destroyed());
ngtcp2_conn_set_local_addr(connection(), addr);
}

// Set the transport parameters received from the remote peer
void QuicSession::set_remote_transport_params() {
DCHECK(!is_flag_set(QUICSESSION_FLAG_DESTROYED));
DCHECK(!is_destroyed());
ngtcp2_conn_get_remote_transport_params(connection(), &transport_params_);
set_flag(QUICSESSION_FLAG_HAS_TRANSPORT_PARAMS);
set_transport_params_set();
}

void QuicSession::StopIdleTimer() {
Expand All @@ -537,7 +528,7 @@ void QuicSession::StopRetransmitTimer() {
// parameter is an array of versions supported by the remote peer.
void QuicSession::VersionNegotiation(const uint32_t* sv, size_t nsv) {
CHECK(!is_server());
if (!is_flag_set(QUICSESSION_FLAG_DESTROYED))
if (!is_destroyed())
listener()->OnVersionNegotiation(NGTCP2_PROTO_VER, sv, nsv);
}

Expand Down
Loading

0 comments on commit b5bf5bb

Please sign in to comment.