目录

Receiving Push Messages over a QUIC Long-Lived Connection

From version 26.0.0, the QUIC long-lived connection capability is supported.

The QUIC long-lived connection is implemented based on the Quick UDP Internet Connections (QUIC) protocol. Compared with the traditional TCP protocol, QUIC has core advantages in speed, flexibility, and stability. The QUIC long-lived connection is widely used in scenarios such as instant messaging, real-time push, and online collaboration, significantly improving communication efficiency and user experience. Remote Communication Kit provides the QUIC long-lived connection API to provide efficient and reliable device-cloud communication capabilities for developers.

In scenarios where messages are actively pushed from the server, the QUIC long-lived connection use head-of-line blocking-free multiplexing and require fewer handshakes, effectively addressing the head-of-line blocking and multiple handshake issues of traditional HTTP/1.1 and HTTP/2, thereby reducing network latency and resource consumption. In addition, the QUIC long-lived connection can maintain the connection persistency, reducing the frequency of connection establishment and disconnection and further improving communication efficiency.

Receiving Push Messages from the Server over a Long-Lived Connection

  1. Import the required modules in the .cpp file.

    #include "RemoteCommunicationKit/rcp_quic.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
  2. Add the following library dependency to the CMakeLists.txt file. For details, see Preparations for C API Development.

    librcp_quic.so
  3. Define global variables for QUIC connection and stream configuration.

    uint64_t g_StreamId = 0;
    uint64_t userData = 0;
    char *alpn = "h3";
    const char *serverName = "www.example.com"; // This is only an example. Replace it with the actual QUIC server address.
    uint64_t serverPort = 443; // This is only an example. Replace it with the actual port number.
  4. Define callbacks to register event listeners in subsequent steps. You can add the required service logic to the callbacks.

    // Called when the connection is established.
    void OnConnectedImpl(Rcp_QuicConn *conn, void *userObject) {
        (void)conn;
        if (userObject == NULL) {
            return;
        }
    }
    
    // Called when an error occurs.
    void OnErrorImpl(Rcp_QuicConn *conn, void *userObject, Rcp_QuicErrorCode errCode, const char *errDetail) {
        (void)conn;
        if (errCode == 0 || userObject == NULL || errDetail == NULL) {
            return;
        }
    }
    
    // Called when the connection is closed.
    void OnClosedImpl(Rcp_QuicConn *conn, void *userObject) {
        (void)conn;
        if (userObject == NULL) {
            return;
        }
    }
    
    // Called when data is received from the peer. The size of the received data must be returned.
    void OnReceiveDataImpl(Rcp_QuicConn *conn, void *userObject, uint64_t streamId,
                           const Rcp_QuicStreamData *streamData) {
        (void)conn;
        (void)streamId;
        (void)userObject;
        uint64_t totalBytes = 0;
        for (uint32_t i = 0; i < streamData->iovLen; i++) {
            totalBytes += streamData->iov[i].length;
        }
        return totalBytes;
    }
    
    // Called when a stream event is received.
    void onQuicStreamEventImpl(Rcp_QuicConn *conn, void *userObject, uint64_t streamId, Rcp_QuicErrorCode errCode,
                               const char *errDetail) {
        (void)conn;
        (void)userObject;
        (void)streamId;
        (void)errCode;
        (void)errDetail;
    }
    
    // Called when the peer creates a stream.
    void OnStreamInboundImpl(Rcp_QuicConn *conn, void *userObject, uint64_t streamId) {
        (void)conn;
        (void)userObject;
        g_StreamId = streamId;
    }
  5. Initialize the QUIC configuration items.

    // Create a QUIC session object to manage global resources.
    Rcp_QuicSession *session = HMS_Rcp_QuicCreateSession();
    // Create a QUIC connection object with the specified ALPN protocol and user object.
    Rcp_QuicConn *conn = HMS_Rcp_QuicConnCreate(alpn, &userData);
    // Set the callback to be triggered when the connection is established.
    HMS_Rcp_QuicConnSetOpt(conn, RCP_QUIC_CONN_ON_CONNECTED_FUNCTION, &OnConnectedImpl, 0);
    // Set the callback to be triggered when an error occurs.
    HMS_Rcp_QuicConnSetOpt(conn, RCP_QUIC_CONN_ON_ERROR_FUNCTION, &OnErrorImpl, 0);
    // Set the callback to be triggered when the connection is closed.
    HMS_Rcp_QuicConnSetOpt(conn, RCP_QUIC_CONN_ON_CLOSED_FUNCTION, &OnClosedImpl, 0);
    // Set the callback to be triggered when the server creates a stream.
    HMS_Rcp_QuicConnSetOpt(conn, RCP_QUIC_CONN_STREAM_INBOUND_FUNCTION, &OnStreamInboundImpl, 0);
  6. Connect to the server.

    HMS_Rcp_QuicConnConnect(session, conn, serverName, serverPort);
  7. When the server initiates a stream, the app receives the OnStreamInboundImpl callback. Set event listeners for the peer stream identified by the returned streamId.

    HMS_Rcp_QuicStreamSetOpt(conn, g_StreamId, RCP_QUIC_STREAM_EVENT_FUNCTION, &onQuicStreamEventImpl, 0);
    HMS_Rcp_QuicStreamSetOpt(conn, g_StreamId, RCP_QUIC_STREAM_DATA_FUNCTION, &OnReceiveDataImpl, 0);
  8. After the service is complete, close the connection and release the resources.

    // Close the stream.
    uint64_t appErr = 0; // Custom status code. The server can receive this status code when the stream is closed to determine the reason for the closure.
    HMS_Rcp_QuicConnStreamShutdown(conn, g_StreamId, RCP_QUIC_STREAM_SHUTDOWN_WRITE, appErr);
    HMS_Rcp_QuicConnStreamShutdown(conn, g_StreamId, RCP_QUIC_STREAM_SHUTDOWN_READ, appErr);
    
    // Destroy the connection object, close all streams, and release connection resources.
    if (conn != NULL) {
        HMS_Rcp_QuicConnDestroy(conn);
    }
    
    // Destroy the session object and release global QUIC resources.
    if (session != NULL) {
        HMS_Rcp_QuicDestroySession(session);
    }