mars:基于 OpenHarmony 的跨平台网络组件项目

可用于 OpenHarmony 应用开发中实现长连接、短连接等 socket 层网络请求,提升网络调优可控性。基于 Mars 原库 v1.3.0 适配,沿用原有用法特性,支持长/短链接管理、任务控制、日志等核心功能,方便开发者提升开发效率。【此简介由AI生成】

Branch2Tags14
FilesLast commitLast update
2 years ago
7 months ago
8 months ago
2 years ago
2 years ago
2 years ago
5 months ago
2 years ago
2 years ago
6 months ago
7 months ago
2 years ago
2 years ago
2 years ago
5 months ago
2 years ago
5 months ago
5 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
9 months ago
2 years ago

Mars

Overview

Mars is a cross-platform network component that implements persistent and non-persistent connections in network requests. It works on the socket layer and provides better performance in network optimization. Currently, Mars does not support HTTP. This library is adapted based on the original Mars version 1.3.0, enabling it to run on OpenHarmony while retaining its existing usage and features. It can significantly improve your development efficiency.

Effect

! Mars

Building and Running

To compile the Mars library, you need to add the OpenSSL dependency to mars\library\src\main\cpp and integrate the OpenSSL to the application HAP.

  1. Enable the support of x86_64 for cross compilation.

  2. Change the OpenSSL version number in the HPKBUILD file.

pkgver=OpenSSL_1_1_1t 
// Change the version number.
pkgver=OpenSSL_1_1_1w
  1. Download OpenSSL OpenSSL_1_1_1w, run the following command to obtain the SHA512 value, and replace the content in the SHA512SUM file.
sha512num openssl-OpenSSL_1_1_1w.tar.gz
  1. Add the openssl folder generated after compilation to the cpp directory. The openssl folder is generated in the usr folder in the lycium directory.

img.png

For Windows and Mac, integrate the patch and then build the project.

  1. Download the source code.
git clone https://gitcode.com/CPF-ApplicationTPC/mars.git --recurse-submodules
  1. Run the following commands in sequence to automatically integrate the patch:
chmod +x automatically_apply_mars_patch_files.sh

./automatically_apply_mars_patch_files.sh

The patch installation is complete.

How to Install

ohpm install @ohos/mars 

For details, see Installing an OpenHarmony HAR.

How to Use

For detailed information, please refer to Getting Started Guide

Step 1: Initialize the Mars component and import it to your project.

...
import { Mars, StnLogic, Xlog, BuildConfig } from '@ohos/Mars/';
import MarsServiceNative from '../wrapper/service/MarsServiceNative';
import MarsServiceProfile from '../wrapper/service/MarsServiceProfile';
import marsnapi from 'libmarsnapi.so';

...

Step 2: Call the corresponding APIs through marsnapi.

marsnapi.BaseEvent_onCreate();
}

Available APIs

For detailed information, please refer to Interface Detailed Description

  • StnLogic.setLonglinkSvrAddr(host: string, ports: number[], debugIP?: string): number - Sets the DEBUG IP address of a persistent connection.
  • StnLogic.StnLogic_setShortlinkSvrAddr(port: number, debugIP?: string): number - Sets the DEBUG IP address of a non-persistent connection.
  • StnLogic.setDebugIP(host: string, ip: string): number - Sets the DEBUG IP address, regardless of persistent and non-persistent connections.
  • StnLogic.startTask(task: StnLogic.Task): number - Starts a task.
  • StnLogic.stopTask(taskID: number): number - Stops a task.
  • StnLogic.hasTask(taskID: number): boolean - Checks whether a task exists.
  • StnLogic.redoTask(): number - Redoes all persistent and non-persistent connection tasks. Note that this API will cause a persistent connection to reconnect.
  • StnLogic.clearTask(): number - Stops and clears all unfinished tasks.
  • StnLogic.reset(): number - Stops and clears all unfinished tasks and performs initialization again.
  • StnLogic.resetAndInitEncoderVersion(packerEncoderVersion: number): number - Stops and clears all unfinished tasks, performs initialization again, and resets the encoder version.
  • StnLogic.setBackupIPs(host: string, ips: string[]): number - Sets the backup IP address, which is used when both the persistent and non-persistent connections are unavailable.
  • StnLogic.makesureLongLinkConnected(): number : Checks the status of the persistent connection. If the connection fails, the system will try to reconnect it.
  • StnLogic.setSignallingStrategy(period: number, keepTime: number): number - Sets the signaling keepalive policy.
  • StnLogic.keepSignalling(): number - Sends a signaling keepalive packet.
  • StnLogic.stopSignalling(): number - Stops signaling keepalive.
  • StnLogic.setClientVersion(clientVersion: number): number - Sets the client version to be added to the proprietary protocol header of the persistent connection.
  • StnLogic.getLoadLibraries(): Array - Obtains the loaded modules at the bottom layer.
  • StnLogic.req2Buf(taskID: number, userContext: any, channelSelect: number, host: string): object : Obtains the data sent by the upper layer.
  • StnLogic.buf2Resp(taskID: number, userContext: any, respBuffer: ArrayBuffer, channelSelect: number): number : Sends the received signaling response packet to the upper layer for parsing.
  • StnLogic.trafficData(send: number, recv: number): void - Reports the traffic consumed by signaling.
  • SdtLogic.setCallBack - Sets the signaling detection callback instance, which is used to notify the upper layer of the detection result.
  • SdtLogic.setHttpNetcheckCGI(_callBack: StnLogic.ICallBack): void - Sets a URI for HTTP connectivity detection.
  • Xlog.logWrite(logInfo: Xlog.XLoggerInfo, log: string) : any - Sets log writing mode 1.
  • Xlog.logWrite2(level: number, tag: string, filename: string, funcname: string, line: number, pid: number, tid: number, maintid: number, log: string): void - Sets log writing mode 2.
  • Xlog.native_logWrite2(logInstancePtr: number, level: number, tag: string, filename: string, funcname: string, line: number, pid: number, tid: number, maintid: number, log: string): any - Sets a custom log writing mode.
  • Xlog.setLogLevel(logInstancePtr: number): any - Sets the log level.
  • Xlog.getLogLevel(logInstancePtr: number): any - Obtains the log level.
  • Xlog.newXlogInstance(logConfig: Xlog.XLogConfig): any - Creates a single Xlog instance.
  • Xlog.releaseXlogInstance(nameprefix: string): any - Releases a single Xlog instance.

Constraints

Mars has been verified in the following versions:

  • DevEco Studio: 4.1Canary (4.1.3.414), OpenHarmony SDK: API 11 (4.1.0.55)
  • IDE: DevEco Studio NEXT Developer Preview2: 4.1.3.600, OpenHarmony SDK: API11 (4.1.0)

Directory Structure

|---- library  
|     |---- cpp  # Sample code
|           |---- mars # C++ library files
|     |---- ets
|           |---- sdt  
|                 |---- SdtLogic # sdt external APIs
|           |---- stn  
|                 |---- StnLogic # stn external APIs
|           |---- xlog # xlog external APIs
|                 |---- Xlog # xlog external APIs
|---- README.md  # Readme
|---- README_zh.md  # Readme

About obfuscation

  • Code obfuscation, please seeCode Obfuscation
  • If you want the mars library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:
-keep
./oh_modules/@ohos/mars

How to Contribute

If you find any problem when using Mars, submit an Issue](https://gitcode.com/CPF-ApplicationTPC/mars/issues) or a PR.

License

This project is licensed under the terms of the MIT License.

Introduction

可用于 OpenHarmony 应用开发中实现长连接、短连接等 socket 层网络请求,提升网络调优可控性。基于 Mars 原库 v1.3.0 适配,沿用原有用法特性,支持长/短链接管理、任务控制、日志等核心功能,方便开发者提升开发效率。【此简介由AI生成】

Customize your domain