chromium-cronet庫的編譯用於Android和ios平臺實現quic協議

 

chromium-cronet文檔 原文文檔寫的已經很清楚,最好還是參考官方文檔,避免由於版本原因導致的問題。

Cronet開發者文檔:https://developer.android.com/guide/topics/connectivity/cronet

博客中的git地址:https://github.com/bgylde/chromium-cornet

環境配置

  1. chromium源碼環境;
  2. 已經配置好相關環境,安裝好相關依賴;
  3. 這裏是在linux環境下對Android庫的編譯,在macos環境下會直接編譯爲ios平臺庫;

編譯開發和debug環境的Cronet庫

Android / IOS build

$ ./components/cronet/tools/cr_cronet.py gn --out_dir=out/Cronet

如果build主機是在linux環境下,build的是android的庫。如果build主機是macOS,build的是ios庫。

$ ninja -C out/Cronet cronet_package

編譯Cronet庫,最終文件可以在out/Cronet/cornet中尋找。

使用cronet庫

  1. 創建CronetEngine,最好整個應用使用一個CronetEngine,這裏可以理解爲OkHttpClient;
  2. 創建自己的線程池給Cronet使用,Cronet的網絡請求都會在線程池中,避免主線程阻塞;
  3. 實現回調UrlRequest.Callback,在UrlRequest調用start以後,網絡請求開始,之後產生請求回調;
public class UrlRequestCallback extends UrlRequest.Callback {

    private static final String TAG = "UrlRequestCallback";

    private long startTime;
    private ByteBuffer mByteBuffer = ByteBuffer.allocateDirect(102400);
    private ByteArrayOutputStream mBytesReceived = new ByteArrayOutputStream();
    private WritableByteChannel mReceiveChannel = Channels.newChannel(mBytesReceived);

    @Override
    public void onRedirectReceived(UrlRequest request, UrlResponseInfo info, String newLocationUrl) {
        LogUtils.i(TAG, "onRedirectReceived method called.");
        // You should call the request.followRedirect() method to continue
        // processing the request.
        request.followRedirect();
    }

    @Override
    public void onResponseStarted(UrlRequest request, UrlResponseInfo info) {
        LogUtils.i(TAG, "onResponseStarted method called.");
        // You should call the request.read() method before the request can be
        // further processed. The following instruction provides a ByteBuffer object
        // with a capacity of 102400 bytes to the read() method.
        //ByteBuffer byteBuffer = ByteBuffer.allocateDirect(102400);
        startTime = System.currentTimeMillis();
        request.read(mByteBuffer);
    }

    @Override
    public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) {
        LogUtils.i(TAG, "onReadCompleted method called.");
        // You should keep reading the request until there's no more data.

        try {
            byteBuffer.flip();
            mReceiveChannel.write(byteBuffer);
            byteBuffer.clear();
        } catch (IOException e) {
            LogUtils.e(TAG, e);
        }

        request.read(mByteBuffer);
    }

    @Override
    public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
        LogUtils.i(TAG, "onSucceeded method called: " + info.toString());
        LogUtils.d(TAG, "cost time: " + (System.currentTimeMillis() - startTime) + " ms");
        //byte[] bytes = mBytesReceived.toByteArray();
        String receivedData = null;
        try {
            receivedData = mBytesReceived.toString("GBK");
        } catch (UnsupportedEncodingException e) {
            LogUtils.e(TAG, e);
        }

        final String url = info.getUrl();
        final String text = "Completed " + url + " (" + info.getHttpStatusCode() + ")";
        //LogUtils.i(TAG, "text: " + text);
        //LogUtils.i(TAG, "receivedData: " + receivedData);
    }

    @Override
    public void onFailed(UrlRequest urlRequest, UrlResponseInfo urlResponseInfo, CronetException e) {
        //LogUtils.d(TAG, "url: " + urlResponseInfo.getUrl());
        LogUtils.e(TAG, "onFailed method called.", e);
    }

    @Override
    public void onCanceled(UrlRequest request, UrlResponseInfo info) {
        LogUtils.d(TAG, "onCanceled method called.");
    }
}
  • onRedirectReceived 顧名思義,是重定向的回調,這裏直接選擇繼續訪問重定向的地址,也可以調用UrlRequest的cancel方法,取消訪問;
  • onResponseStarted 從google文檔上面來看是請求完header以後開始請求body部分會回調這裏,每次請求只會回調一次;
  • onReadCompleted 這是讀取body一定數據時會回調這個方法,這裏request.read讀取的數據不一定會填滿緩衝區,請求生命週期中會有多次回調發生;
  • onSucceeded 最終請求成功回調,可以作爲數據處理階段;
  • onFailed 請求失敗回調,例如網絡不通,或者沒有網絡訪問權限之類的錯誤;
  • onCanceled 請求取消回調,只會在cancel後纔會回調,回調這個意味着整個請求完成;

https://ssl.gstatic.com/gb/images/qi2_00ed8ca1.png 實現quic訪問,可作爲測試地址。

 這是通過抓包看到的quic協議,進一步的性能對比就需要自己搭建一個支持quic協議的服務器。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章