io.netty.incubator.codec.quic.Quic Maven / Gradle / Ivy
/*
* Copyright 2020 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.incubator.codec.quic;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelOption;
import io.netty.util.AttributeKey;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.logging.InternalLogger;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public final class Quic {
@SuppressWarnings("unchecked")
static final Map.Entry, Object>[] EMPTY_OPTION_ARRAY = new Map.Entry[0];
@SuppressWarnings("unchecked")
static final Map.Entry, Object>[] EMPTY_ATTRIBUTE_ARRAY = new Map.Entry[0];
static final int MAX_DATAGRAM_SIZE = 1350;
static final int RESET_TOKEN_LEN = 16;
private static final Throwable UNAVAILABILITY_CAUSE;
static {
Throwable cause = null;
try {
String version = Quiche.quiche_version();
assert version != null;
} catch (Throwable error) {
cause = error;
}
UNAVAILABILITY_CAUSE = cause;
}
/**
* Return if the given QUIC version is supported.
*
* @param version the version.
* @return {@code true} if supported, {@code false} otherwise.
*/
public static boolean isVersionSupported(int version) {
return isAvailable() && Quiche.quiche_version_is_supported(version);
}
/**
* Returns {@code true} if and only if the QUIC implementation is usable on the running platform is available.
*
* @return {@code true} if this QUIC implementation can be used on the current platform, {@code false} otherwise.
*/
public static boolean isAvailable() {
return UNAVAILABILITY_CAUSE == null;
}
/**
* Ensure that QUIC implementation is usable on the running platform is available.
*
* @throws UnsatisfiedLinkError if unavailable
*/
public static void ensureAvailability() {
if (UNAVAILABILITY_CAUSE != null) {
throw (Error) new UnsatisfiedLinkError(
"failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
}
}
/**
* Returns the cause of unavailability.
*
* @return the cause if unavailable. {@code null} if available.
*/
@Nullable
public static Throwable unavailabilityCause() {
return UNAVAILABILITY_CAUSE;
}
static Map.Entry, Object>[] toOptionsArray(Map, Object> opts) {
return new HashMap<>(opts).entrySet().toArray(EMPTY_OPTION_ARRAY);
}
static Map.Entry, Object>[] toAttributesArray(Map, Object> attributes) {
return new LinkedHashMap<>(attributes).entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY);
}
private static void setAttributes(Channel channel, Map.Entry, Object>[] attrs) {
for (Map.Entry, Object> e: attrs) {
@SuppressWarnings("unchecked")
AttributeKey
© 2015 - 2024 Weber Informatics LLC | Privacy Policy