[Huawei Developers] John Wo:
Overview

QUIC (Quick UDP Internet Connections) is a general-purpose transport layer network protocol.

QUIC is a new transport which reduces latency compared to that of TCP. On the surface, QUIC is very similar to TCP+TLS+HTTP/2 implemented on UDP. Because TCP is implemented in operating system kernels, and middlebox firmware, making significant changes to TCP is next to impossible.

QUIC reduces round trips by working with UDP. With great features like eliminating head of line blocking, loss recovery over UDP by using multiplexed connections and congestion control, the protocol has evolved and is being widely used.

Introduction of hQUIC

hQUIC provides the ability to fulfil our needs for lower packet loss in our applications, high performance in our network operations and reliable fast connection. It supports the gQUIC protocol and provides intelligent congestion control algorithms to avoid congestions in different network environments.

Advantages

Ease of use: Streamlines APIs and shields low-level network details.

High compatibility: Supports gQUIC and Cronet.

Better experience: Outperforms other protocols in poor network conditions

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20201106115400.83115768238662236319573294027774:50511119004726:2800:5894F0B124345482FEDD8CF7F2D6F3DC85EC9D6ACD97C74462D815DF251DA9DF.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20201106115400.83115768238662236319573294027774:50511119004726:2800:5894F0B124345482FEDD8CF7F2D6F3DC85EC9D6ACD97C74462D815DF251DA9DF.gif[/IMG]

What is Cronet?

Cronet is the networking stack of Chromium put into a library for use on mobile. This is the same networking stack that is used in the Chrome browser by over a billion people. It offers an easy-to-use, high performance, standards-compliant, and secure way to perform HTTP requests.

Cronet is a very well implemented and tested network stack that provides almost everything that a standard app needs from a network layer library, things like DNS, Cookies, SSL/TLS, HTTP(S), HTTP2, Proxy/PAC, OSCP, Web sockets, and F̶T̶P̶(soon to be removed). It also supports HTTP over QUIC, soon to be called HTTP3. This makes it a very attractive library to use as the network layer for your mobile app.

How TCP Works?

TCP allows for transmission of information in both directions. This means that computer systems that communicate over TCP can send and receive data at the same time, similar to a telephone conversation. The protocol uses segments (packets) as the basic units of data transmission.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20201106115313.83091858313723183545156014962082:50511119004726:2800:16C12C646ED6339ED678F8F068BA5AB5ED97203C0A69A7F302C9BA43E1F5545B.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20201106115313.83091858313723183545156014962082:50511119004726:2800:16C12C646ED6339ED678F8F068BA5AB5ED97203C0A69A7F302C9BA43E1F5545B.gif[/IMG]

The actual process for establishing a connection with the TCP protocol is as follows:

1. First, the requesting sender sends the server a SYN packet or segment (SYN stands for synchronize) with a unique, random number. This number ensures full transmission in the correct order (without duplicates).

2. If the receiver has received the segment, it agrees to the connection by returning a SYN-ACK packet (ACK stands for acknowledgement) including the client's sequence number plus 1. It also transmits its own sequence number to the client.

3. Finally, the sender acknowledges the receipt of the SYN-ACK segment by sending its own ACK packet, which in this case contains the receiver's sequence number plus 1. At the same time, the client can already begin transferring data to the receiver.

Prerequisite

1. A computer with Android Studio installed and able to access the Internet

2. A Huawei phone with HMS Core (APK) 5.0.0 or later installed for debugging the app

3. Java JDK (1.7 or later)

4. Android API (level 19 or higher)

5. EMUI 3.0 or later

6. HMS Core (APK) 5.0.0 or later

Integration process

1. Open the build.gradle file in the root directory of your Android Studio project.

buildscript {
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
google()
...
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
}
}
}

2. Open the build.gradle file in the app directory and add dependency on the SDK.

dependencies {
implementation 'com.huawei.hms:hquic-provider:5.0.0.300'
}

3. Initialise the service

HQUICManager.asyncInit(context, new HQUICManager.HQUICInitCallback() {
@Override
public void onSuccess() {
Log.i(TAG, "HQUICManager asyncInit success");
}

@Override
public void onFail(Exception e) {
Log.w(TAG, "HQUICManager asyncInit fail");
}
});

4. Initialise the Cronet Engine

CronetEngine.Builder builder = new CronetEngine.Builder(context);
builder.enableQuic(true);
builder.addQuicHint(getHost(url), DEFAULT_PORT, DEFAULT_ALTERNATEPORT);
cronetEngine = builder.build();

App Development

I will use APIs of the hQUIC SDK by developing a Speed Test app step by step.

I have created HQUICSericeProvider class in which I will create the service using hQuic APIs.

Let us implements the asynchronous initialization API.

public class HQUICServiceProvider {
private static final String TAG = "HQUICServiceProvider";
private static final int DEFAULT_PORT = 443;
private static final int DEFAULT_ALTERNATEPORT = 443;

private static Executor executor = Executors.newSingleThreadExecutor();

private static CronetEngine cronetEngine;
private Context context;
private UrlRequest.Callback callback;

public HQUICServiceProvider(Context context) {
this.context = context;
init();
}

public void init() {
HQUICManager.asyncInit(
context,
new HQUICManager.HQUICInitCallback() {
@Override
public void onSuccess() {
Log.i(TAG, "HQUICManager asyncInit success");
}

@Override
public void onFail(Exception e) {
Log.w(TAG, "HQUICManager asyncInit fail");
}
});
}

private CronetEngine createCronetEngine(String url) {
if (cronetEngine != null) {
return cronetEngine;
}
CronetEngine.Builder builder = new CronetEngine.Builder(context);
builder.enableQuic(true);
builder.addQuicHint(getHost(url), DEFAULT_PORT, DEFAULT_ALTERNATEPORT);
cronetEngine = builder.build();
return cronetEngine;
}

private UrlRequest buildRequest(String url, String method) {
CronetEngine cronetEngine = createCronetEngine(url);
UrlRequest.Builder requestBuilder =
cronetEngine.newUrlRequestBuilder(url, callback, executor).setHttpMethod(method);
UrlRequest request = requestBuilder.build();
return request;
}

public void sendRequest(String url, String method) {
Log.i(TAG, "callURL: url is " + url + "and method is " + method);
UrlRequest urlRequest = buildRequest(url, method);
if (null != urlRequest) {
urlRequest.start();
}
}

private String getHost(String url) {
String host = null;
try {
java.net.URL url1 = new java.net.URL(url);
host = url1.getHost();
} catch (MalformedURLException e) {
Log.e(TAG, "getHost: ", e);
}
return host;
}

public void setCallback(UrlRequest.Callback mCallback) {
callback = mCallback;
}
}

More details, you can visit https://forums.developer.huawei.com/forumPortal/en/topic/0203400466275620106