{"id":13346967,"url":"https://github.com/Tencent/mars","last_synced_at":"2025-03-12T08:31:17.266Z","repository":{"id":37359547,"uuid":"76222419","full_name":"Tencent/mars","owner":"Tencent","description":"Mars is a cross-platform network component  developed by WeChat.","archived":false,"fork":false,"pushed_at":"2024-07-26T09:18:53.000Z","size":415117,"stargazers_count":17207,"open_issues_count":456,"forks_count":3621,"subscribers_count":776,"default_branch":"master","last_synced_at":"2024-07-27T16:51:07.006Z","etag":null,"topics":["cross-platform","log","network","wechat"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tencent.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-12T04:39:54.000Z","updated_at":"2024-07-29T04:41:22.974Z","dependencies_parsed_at":"2024-07-29T04:58:06.565Z","dependency_job_id":null,"html_url":"https://github.com/Tencent/mars","commit_stats":{"total_commits":1527,"total_committers":59,"mean_commits":25.88135593220339,"dds":0.7432874918140144,"last_synced_commit":"f2a519d63a7a83fef97efdb3df796677f4e0898d"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tencent%2Fmars","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tencent%2Fmars/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tencent%2Fmars/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tencent%2Fmars/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tencent","download_url":"https://codeload.github.com/Tencent/mars/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243039682,"owners_count":20226134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-platform","log","network","wechat"],"created_at":"2024-07-29T20:01:30.550Z","updated_at":"2025-03-12T08:31:17.255Z","avatar_url":"https://github.com/Tencent.png","language":"C++","readme":"## Mars\n\n[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/Tencent/mars/blob/master/LICENSE)\n[![Release Version](https://img.shields.io/badge/release-1.2.3-red.svg)](https://github.com/Tencent/mars/releases)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/mars/pulls)\n[![WeChat Approved](https://img.shields.io/badge/Wechat_Approved-1.2.3-red.svg)](https://github.com/Tencent/mars/wiki)\n[![WeChat Approved](https://img.shields.io/badge/Platform(cmake)-%20iOS%20%7C%20OS%20X%20%7C%20Android(ndkr20)%20%7C%20Windows(vs2015)%20-brightgreen.svg)](https://github.com/Tencent/mars/wiki)\n\n(中文版本请参看[这里](#mars_cn))\n\nMars is a cross-platform infrastructure component developed by WeChat Mobile Team.\nIt has been proved to be effective by billions of WeChat users.\n\n1. Cross platform, easy to deploy if you are developing multi-platform or multi-business application.\n2. Suitable for small amount data transmission\n3. Mobile platform friendly, low power and traffic consumption\n4. A network solution fit for mobile application\n\n![mars](https://github.com/WeMobileDev/article/blob/master/assets/mars/mars.png?raw=true)\n\n* comm: common library, including socket, thread, message queue, coroutine, etc.\n* Xlog: a reliable log component with high-performance.\n* SDT: a network detection component.\n* STN: a signaling network component, the major part of Mars.\n\n## Samples\n\nStart with sample usage [here](https://github.com/Tencent/mars/wiki/Mars-sample-%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E).\n\n## Getting started\n\npython scripts only support python3.10 or higher\n\nChoose [Android](#android) or [iOS/OS X](#apple) or [Windows](#windows).\n\n### \u003ca name=\"android\"\u003e[Android](https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97)\u003c/a\u003e\n\nYou can use either [mars-wrapper](#wrapper) or [mars-core](#core). We recommend you to use mars-wrapper when you just want to build a sample or demo, while mars-core is preferred to be used in your APP.\n\n#### \u003ca name=\"wrapper\"\u003emars-wrapper\u003c/a\u003e\n\nAdd dependencies by adding the following lines to your app/build.gradle.\n\n```xml\ndependencies {\n    compile 'com.tencent.mars:mars-wrapper:1.2.5'\n}\n```\n\n**OR**\n\n#### \u003ca name=\"core\"\u003emars-core\u003c/a\u003e\n\nAdd dependencies by adding the following lines to your app/build.gradle.\n\n```xml\ndependencies {\n    compile 'com.tencent.mars:mars-core:1.2.5'\n}\n```\n**OR**\n#### \u003ca name=\"\"\u003emars-xlog\u003c/a\u003e\nIf you just want to user xlog, add dependencies by adding the following lines to your app/build.gradle.\nnote: xlog is included in mars-core and mars-wrapper.\n\n```xml\ndependencies {\n    compile 'com.tencent.mars:mars-xlog:1.2.5'\n}\n```\n\nIf you read here, make sure you have added dependencies of mars-wrapper, mars-core or mars-xlog.\n\n#### \u003ca name=\"Xlog\"\u003eXlog Init\u003c/a\u003e\n\nInitialize Xlog when your APP starts. Remember to use an exclusive folder to save the log files, no other files are acceptable in the folder since they would be removed by the cleansing function in Xlog automatically.\n\nWhen multiple processes is used in your app, make sure that each process owns its exclusive log file.\n\n```java\nSystem.loadLibrary(\"c++_shared\");\nSystem.loadLibrary(\"marsxlog\");\n\nfinal String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();\nfinal String logPath = SDCARD + \"/marssample/log\";\n\n// this is necessary, or may crash for SIGBUS\nfinal String cachePath = this.getFilesDir() + \"/xlog\"\n\n//init xlog\nXlog xlog = new Xlog();\nLog.setLogImp(xlog);\n\nif (BuildConfig.DEBUG) {\n    Log.setConsoleLogOpen(true);\n  \tLog.appenderOpen(Xlog.LEVEL_DEBUG, Xlog.AppednerModeAsync, \"\", logPath, logFileName, 0);\n} else {\n    Log.setConsoleLogOpen(false);\n  \tLog.appenderOpen(Xlog.LEVEL_INFO, Xlog.AppednerModeAsync, \"\", logPath, logFileName, 0);\n}\n\n```\n\nUninitialized Xlog when your app exits\n\n\n```java\nLog.appenderClose();\n```\n\n#### \u003ca name=\"STN\"\u003eSTN Init\u003c/a\u003e\n\nIf you add dependencies of mars-core to your project, you need to initialize and release STN.\nInitialize STN before you use it\n\n```java\n// set callback\nAppLogic.setCallBack(stub);\nStnLogic.setCallBack(stub);\nSdtLogic.setCallBack(stub);\n\n// Initialize the Mars PlatformComm\nMars.init(getApplicationContext(), new Handler(Looper.getMainLooper()));\n\n// Initialize the Mars\nStnLogic.setLonglinkSvrAddr(profile.longLinkHost(), profile.longLinkPorts());\nStnLogic.setShortlinkSvrAddr(profile.shortLinkPort());\nStnLogic.setClientVersion(profile.productID());\nMars.onCreate(true);\n\nBaseEvent.onForeground(true);\nStnLogic.makesureLongLinkConnected();\n```\nFirstly, you should call the setCallBack interface, and secondly, the Mars.init. Then, to initialize the Mars, there is to need to strictly follow the orders of the four commands. Finally, after Mars are initialized, onForeground and makesureLongLinkConnect can be called.\n\nDestroy STN or exit your app:\n\n```java\nMars.onDestroy();\n```\n\n#### \u003ca name=\"even\"\u003eEvent Change\u003c/a\u003e\n\nNetwork change:\n\n```java\nBaseEvent.onNetworkChange()\n```\n**********\nIf you add dependencies of mars-wrapper to your project, you just need initialize STN and no need uninitialized.\n\n```java\nMarsServiceProxy.init(this, getMainLooper(),null);\n```\n************\nNo matter which way of dependencies you used, you must pay attention to these.\n\nThe state (background or foreground) of the APP is changed:\n\n```java\nBaseEvent.onForeground(boolean);\n```\n\nThe account of the APP is changed:\n\n```java\nStnLogic.reset();\n```\n\nIf you want to modify the encryption algorithm of Xlog, the packer/unpacker of longlink/shortlink, or you want to define the other components by yourself, refer [here](https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97)\n\n### \u003ca name=\"apple\"\u003e[iOS/OS X](https://github.com/Tencent/mars/wiki/Mars-iOS%EF%BC%8FOS-X-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97)\u003c/a\u003e\nCompile\n\n```python\npython build_ios.py\n```\n\nor \n```python\npython build_osx.py\n```\n\n1. Add mars.framework as a dependency of your project.\n2. Rename files in mars/libraries/mars_android_sdk/jni with .rewriteme extension to .cc extension.\n3. Add header files in mars/libraries/mars_android_sdk/jni and source files from step 2 into your project.\n\n#### \u003ca name=\"Xlog\"\u003eXlog Init\u003c/a\u003e\n\nInitialize Xlog when your app starts. Remember to use an exclusive folder to save the log files, no other files are acceptable in the folder since they would be removed by the cleansing function in Xlog automatically.\n\n```cpp\nNSString* logPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@\"/log\"];\n\n// set do not backup for logpath\nconst char* attrName = \"com.apple.MobileBackup\";\nu_int8_t attrValue = 1;\nsetxattr([logPath UTF8String], attrName, \u0026attrValue, sizeof(attrValue), 0, 0);\n\n// init xlogger\n#if DEBUG\nxlogger_SetLevel(kLevelDebug);\nappender_set_console_log(true);\n#else\nxlogger_SetLevel(kLevelInfo);\nappender_set_console_log(false);\n#endif\nXLogConfig config;\nconfig.mode_ = kAppenderAsync;\nconfig.logdir_ = [logPath UTF8String];\nconfig.nameprefix_ = \"Test\";\nconfig.pub_key_ = \"\";\nconfig.compress_mode_ = kZlib;\nconfig.compress_level_ = 0;\nconfig.cachedir_ = \"\";\nconfig.cache_days_ = 0;\nappender_open(config);\n```\n\nClose xlog in function \"applicationWillTerminate\"\n\n\n```cpp\nappender_close();\n```\n\n#### \u003ca name=\"STN\"\u003eSTN Init\u003c/a\u003e\n\nInitialize STN before you use it:\n\n```objc\n- (void)setCallBack {\n    mars::stn::SetCallback(mars::stn::StnCallBack::Instance());\n    mars::app::SetCallback(mars::app::AppCallBack::Instance());\n}\n\n- (void) createMars {\n    mars::baseevent::OnCreate();\n}\n\n- (void)setClientVersion:(UInt32)clientVersion {\n    mars::stn::SetClientVersion(clientVersion);\n}\n\n- (void)setShortLinkDebugIP:(NSString *)IP port:(const unsigned short)port {\n    std::string ipAddress([IP UTF8String]);\n    mars::stn::SetShortlinkSvrAddr(port, ipAddress);\n}\n\n- (void)setShortLinkPort:(const unsigned short)port {\n    mars::stn::SetShortlinkSvrAddr(port);\n}\n\n- (void)setLongLinkAddress:(NSString *)string port:(const unsigned short)port debugIP:(NSString *)IP {\n    std::string ipAddress([string UTF8String]);\n    std::string debugIP([IP UTF8String]);\n    std::vector\u003cuint16_t\u003e ports;\n    ports.push_back(port);\n    mars::stn::SetLonglinkSvrAddr(ipAddress,ports,debugIP);\n}\n\n- (void)setLongLinkAddress:(NSString *)string port:(const unsigned short)port {\n    std::string ipAddress([string UTF8String]);\n    std::vector\u003cuint16_t\u003e ports;\n    ports.push_back(port);\n    mars::stn::SetLonglinkSvrAddr(ipAddress,ports);\n}\n\n- (void)reportEvent_OnForeground:(BOOL)isForeground {\n    mars::baseevent::OnForeground(isForeground);\n}\n\n- (void)makesureLongLinkConnect {\n    mars::stn::MakesureLonglinkConnected();\n}\n```\n\nFirstly, you should call the setCallBack interface, and secondly, the Mars.init. Then, to initialize the Mars, there is to need to strictly follow the orders of the four commands. Finally, after Mars are initialized, onForeground and makesureLongLinkConnect can be called.\n\nIf you want to destroy STN or exit App:\n\n```objc\n- (void)destroyMars {\n    mars::baseevent::OnDestroy();\n}\n```\n\n#### \u003ca name=\"even\"\u003eEvent Change\u003c/a\u003e\n\nWhen the App's state of background or foreground is changed:\n\n```objc\n- (void)reportEvent_OnForeground:(BOOL)isForeground {\n    mars::baseevent::OnForeground(isForeground);\n}\n```\n\nNetwork change:\n\n```objc\n- (void)reportEvent_OnNetworkChange {\n    mars::baseevent::OnNetworkChange();\n}\n```\n\n### \u003ca name=\"windows\"\u003e[Windows](https://github.com/Tencent/mars/wiki/Mars-Windows-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97)\u003c/a\u003e\nInstall Visual Studio 2015.\n\nCompile\n```python\npython build_windows.py\n```\n\n1. Add mars.lib as a dependency of your project.\n2. Rename files in mars/libraries/mars_android_sdk/jni with .rewriteme extension to .cc extension.\n3. Add header files in mars/libraries/mars_android_sdk/jni and source files from step 2 into your project.\n\n#### \u003ca name=\"Xlog\"\u003eXlog Init\u003c/a\u003e\n\nInitialize Xlog when your app starts. Remember to use an exclusive folder to save the log files, no other files are acceptable in the folder since they would be removed by the cleansing function in Xlog automatically.\n\n```cpp\nstd::string logPath = \"\"; //use your log path\nstd::string pubKey = \"\"; //use you pubkey for log encrypt\n\n// init xlog\n#if DEBUG\nxlogger_SetLevel(kLevelDebug);\nappender_set_console_log(true);\n#else\nxlogger_SetLevel(kLevelInfo);\nappender_set_console_log(false);\n#endif\nappender_open(kAppenderAsync, logPath.c_str(), \"Test\", 0, pubKey.c_str());\n```\n\nUninitialized xlog before your app exits\n\n\n```cpp\nappender_close();\n```\n\n#### \u003ca name=\"STN\"\u003eSTN Init\u003c/a\u003e\n\nInitialize STN before you use it:\n\n```cpp\nvoid setShortLinkDebugIP(const std::string\u0026 _ip, unsigned short _port)\n{\n\tmars::stn::SetShortlinkSvrAddr(_port, _ip);\n}\nvoid setShortLinkPort(unsigned short _port)\n{\n\tmars::stn::SetShortlinkSvrAddr(_port, \"\");\n}\nvoid setLongLinkAddress(const std::string\u0026 _ip, unsigned short _port, const std::string\u0026 _debug_ip)\n{\n\tvector\u003cuint16_t\u003e ports;\n\tports.push_back(_port);\n\tmars::stn::SetLonglinkSvrAddr(_ip, ports, _debug_ip);\n}\n\nvoid Init()\n{\n\tmars::stn::SetCallback(mars::stn::StnCallBack::Instance());\n\tmars::app::SetCallback(mars::app::AppCallBack::Instance());\n\tmars::baseevent::OnCreate();\n\n\t//todo\n\t//mars::stn::SetClientVersion(version);\n\t//setShortLinkDebugIP(...)\n\t//setLongLinkAddress(...)\n\n\tmars::baseevent::OnForeground(true);\n\tmars::stn::MakesureLonglinkConnected();\n}\n```\n\nFirstly, you should call the setCalBack interface, and secondly, the Mars.init. Then, to initialize the Mars, there is to need to strictly follow the orders of the four commands. Finally, after Mars are initialized, onForeground and makesureLongLinkConnect can be called.\n\nIf you want to destroy STN or exit App:\n\n```cpp\nmars::baseevent::OnDestroy();\n```\n\n## Support\n\nAny problem?\n\n1. Learn more from [mars/sample](https://github.com/Tencent/mars/tree/master/samples).\n2. Read the [source code](https://github.com/Tencent/mars/tree/master).\n3. Read the [wiki](https://github.com/Tencent/mars/wiki) or [FAQ](https://github.com/Tencent/mars/wiki/Mars-%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) for help.\n4. Contact us for help.\n\n## Contributing\n\nFor more information about contributing issues or pull requests, see our [Mars Contributing Guide](https://github.com/Tencent/mars/blob/master/CONTRIBUTING.md).\n\n## License\n\nMars is under the MIT license. See the [LICENSE](https://github.com/Tencent/mars/blob/master/LICENSE) file for details.\n\n------------------------------\n## \u003ca name=\"mars_cn\"\u003eMars\u003c/a\u003e\n\n[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/Tencent/mars/blob/master/LICENSE)\n[![Release Version](https://img.shields.io/badge/release-1.2.3-red.svg)](https://github.com/Tencent/mars/releases)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/mars/pulls)\n[![WeChat Approved](https://img.shields.io/badge/Wechat_Approved-1.2.3-red.svg)](https://github.com/Tencent/mars/wiki)\n[![WeChat Approved](https://img.shields.io/badge/Platform-%20iOS%20%7C%20OS%20X%20%7C%20Android%20-brightgreen.svg)](https://github.com/Tencent/mars/wiki)\n\nMars 是微信官方的跨平台跨业务的终端基础组件。\n\n\n![mars](https://github.com/WeMobileDev/article/blob/master/assets/mars/mars.png?raw=true)\n\n* comm：可以独立使用的公共库，包括 socket、线程、消息队列、协程等；\n* xlog：高可靠性高性能的运行期日志组件；\n* SDT： 网络诊断组件；\n* STN： 信令分发网络模块，也是 Mars 最主要的部分。\n\n## Samples\n\nsample 的使用请参考[这里](https://github.com/Tencent/mars/wiki/Mars-sample-%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E)。\n\n## Getting started\n\npython \u003e= 3,10\n接入 [Android](#android_cn) 或者 [iOS/OS X](#apple_cn) 或者 [Windows](#windows_cn) 。\n\n### \u003ca name=\"android_cn\"\u003e[Android](https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97)\u003c/a\u003e\n\ngradle 接入我们提供了两种接入方式：[mars-wrapper](#wrapper) 或者 [mars-core](#core)。如果你只是想做个 sample 推荐使用 mars-wrapper，可以快速开发；但是如果你想把 mars 用到你的 app 中的话，推荐使用 mars-core，可定制性更高。\n\n#### \u003ca name=\"wrapper\"\u003emars-wrapper\u003c/a\u003e\n\n在 app/build.gradle 中添加 mars-wrapper 的依赖：\n\n\n```xml\ndependencies {\n    compile 'com.tencent.mars:mars-wrapper:1.2.5'\n}\n```\n\n**或者**\n\n#### \u003ca name=\"core\"\u003emars-core\u003c/a\u003e\n\n在 app/build.gradle 中添加 mars-core 的依赖：\n\n\n```xml\ndependencies {\n    compile 'com.tencent.mars:mars-core:1.2.5'\n}\n```\n**或者**\n#### \u003ca name=\"\"\u003emars-xlog\u003c/a\u003e\n如果只想使用 xlog,可以只加 xlog 的依赖(mars-core,mars-wrapper 中都已经包括 xlog)：\n```xml\ndependencies {\n    compile 'com.tencent.mars:mars-xlog:1.2.5'\n}\n```\n接着往下操作之前，请先确保你已经添加了 mars-wrapper 或者 mars-core 或者 mars-xlog 的依赖\n\n#### \u003ca name=\"Xlog\"\u003eXlog Init\u003c/a\u003e\n\n在程序启动加载 Xlog 后紧接着初始化 Xlog。但要注意如果你的程序使用了多进程，不要把多个进程的日志输出到同一个文件中，保证每个进程独享一个日志文件。而且保存 log 的目录请使用单独的目录，不要存放任何其他文件防止被 xlog 自动清理功能误删。\n\n\n```java\nSystem.loadLibrary(\"c++_shared\");\nSystem.loadLibrary(\"marsxlog\");\n\nfinal String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();\nfinal String logPath = SDCARD + \"/marssample/log\";\n\n// this is necessary, or may crash for SIGBUS\nfinal String cachePath = this.getFilesDir() + \"/xlog\"\n\n//init xlog\nXlog.XLogConfig logConfig = new Xlog.XLogConfig();\nlogConfig.mode = Xlog.AppednerModeAsync;\nlogConfig.logdir = logPath;\nlogConfig.nameprefix = logFileName;\nlogConfig.pubkey = \"\";\nlogConfig.compressmode = Xlog.ZLIB_MODE;\nlogConfig.compresslevel = 0;\nlogConfig.cachedir = \"\";\nlogConfig.cachedays = 0;\nif (BuildConfig.DEBUG) {\n    logConfig.level = Xlog.LEVEL_VERBOSE;\n    Xlog.setConsoleLogOpen(true);\n} else {\n    logConfig.level = Xlog.LEVEL_INFO;\n    Xlog.setConsoleLogOpen(false);\n}\n\nLog.setLogImp(new Xlog());\n```\n\n程序退出时关闭日志：\n\n\n```java\nLog.appenderClose();\n```\n\n#### \u003ca name=\"STN\"\u003eSTN Init\u003c/a\u003e\n\n如果你是把 mars-core 作为依赖加入到你的项目中的话，你需要显式的初始化和反初始化 STN\n\n在使用 STN 之前进行初始化\n\n```java\n// set callback\nAppLogic.setCallBack(stub);\nStnLogic.setCallBack(stub);\nSdtLogic.setCallBack(stub);\n\n// Initialize the Mars PlatformComm\nMars.init(getApplicationContext(), new Handler(Looper.getMainLooper()));\n\n// Initialize the Mars\nStnLogic.setLonglinkSvrAddr(profile.longLinkHost(), profile.longLinkPorts());\nStnLogic.setShortlinkSvrAddr(profile.shortLinkPort());\nStnLogic.setClientVersion(profile.productID());\nMars.onCreate(true);\nBaseEvent.onForeground(true);\n\nStnLogic.makesureLongLinkConnected();\n```\n\n初始化顺序不一定要严格遵守上述代码的顺序，但在初始化时首先要调用 setCallBack 接口 (callback 文件的编写可以参考 demo)，再调用 Mars.init，最后再调用 onForeground 和 makesureLongLinkConnect，中间顺序可以随意更改。**注意：STN 默认是后台，所以初始化 STN 后需要主动调用一次**```BaseEvent.onForeground(true)```\n\n需要释放 STN 或者退出程序时:\n\n```java\nMars.onDestroy();\n```\n\n#### \u003ca name=\"even\"\u003eEvent Change\u003c/a\u003e\n\n网络切换时:\n\n```java\nBaseEvent.onNetworkChange()\n```\n**********\n如果你是把 mars-wrapper 作为依赖加入到你的项目中，你只需要显式的初始化 STN，不需要反初始化(因为 mars-wrapper 会进行反初始化)\n\n```java\nMarsServiceProxy.init(this, getMainLooper(),null);\n```\n************\n不管你是使用 mars-wrapper 还是 mars-core，你都需要特别注意以下事件：\n\n\n前后台切换:\n\n```java\nBaseEvent.onForeground(boolean);\n```\n\n应用的账号信息更改:\n\n```java\nStnLogic.reset();\n```\n\n如果你想修改 Xlog 的加密算法或者长短连的加解包部分甚至更改组件的其他部分，可以参考[这里](https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97)\n\n### \u003ca name=\"apple_cn\"\u003e[iOS/OS X](https://github.com/Tencent/mars/wiki/Mars-iOS%EF%BC%8FOS-X-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97)\u003c/a\u003e\n编译\n\n```\npython build_ios.py\n```\n\nor \n```python\npython build_osx.py\n```\n\n把 mars.framework 作为依赖加入到你的项目中，把mars/libraries/mars_android_sdk/jni 目录的后缀名为 rewriteme 的文件名删掉\".rewriteme\"和头文件一起加入到你的项目中。\n\n#### \u003ca name=\"Xlog\"\u003eXlog Init\u003c/a\u003e\n\n在程序启动加载 Xlog 后紧接着初始化 Xlog。但要注意保存 log 的目录请使用单独的目录，不要存放任何其他文件防止被 xlog 自动清理功能误删。\n\n\n```cpp\nNSString* logPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@\"/log\"];\n\n// set do not backup for logpath\nconst char* attrName = \"com.apple.MobileBackup\";\nu_int8_t attrValue = 1;\nsetxattr([logPath UTF8String], attrName, \u0026attrValue, sizeof(attrValue), 0, 0);\n\n// init xlogger\n#if DEBUG\nxlogger_SetLevel(kLevelDebug);\nappender_set_console_log(true);\n#else\nxlogger_SetLevel(kLevelInfo);\nappender_set_console_log(false);\n#endif\n\nXLogConfig config;\nconfig.mode_ = kAppenderAsync;\nconfig.logdir_ = [logPath UTF8String];\nconfig.nameprefix_ = \"Test\";\nconfig.pub_key_ = \"\";\nconfig.compress_mode_ = kZlib;\nconfig.compress_level_ = 0;\nconfig.cachedir_ = \"\";\nconfig.cache_days_ = 0;\nappender_open(config);\n```\n\n在函数 \"applicationWillTerminate\" 中反初始化 Xlog\n\n```cpp\nappender_close();\n```\n\n#### \u003ca name=\"STN\"\u003eSTN Init\u003c/a\u003e\n\n在你用 STN 之前初始化：\n\n```objc\n- (void)setCallBack {\n    mars::stn::SetCallback(mars::stn::StnCallBack::Instance());\n    mars::app::SetCallback(mars::app::AppCallBack::Instance());\n}\n\n- (void) createMars {\n    mars::baseevent::OnCreate();\n}\n\n- (void)setClientVersion:(UInt32)clientVersion {\n    mars::stn::SetClientVersion(clientVersion);\n}\n\n- (void)setShortLinkDebugIP:(NSString *)IP port:(const unsigned short)port {\n    std::string ipAddress([IP UTF8String]);\n    mars::stn::SetShortlinkSvrAddr(port, ipAddress);\n}\n\n- (void)setShortLinkPort:(const unsigned short)port {\n    mars::stn::SetShortlinkSvrAddr(port);\n}\n\n- (void)setLongLinkAddress:(NSString *)string port:(const unsigned short)port debugIP:(NSString *)IP {\n    std::string ipAddress([string UTF8String]);\n    std::string debugIP([IP UTF8String]);\n    std::vector\u003cuint16_t\u003e ports;\n    ports.push_back(port);\n    mars::stn::SetLonglinkSvrAddr(ipAddress,ports,debugIP);\n}\n\n- (void)setLongLinkAddress:(NSString *)string port:(const unsigned short)port {\n    std::string ipAddress([string UTF8String]);\n    std::vector\u003cuint16_t\u003e ports;\n    ports.push_back(port);\n    mars::stn::SetLonglinkSvrAddr(ipAddress,ports);\n}\n\n- (void)reportEvent_OnForeground:(BOOL)isForeground {\n    mars::baseevent::OnForeground(isForground);\n}\n\n- (void)makesureLongLinkConnect {\n    mars::stn::MakesureLonglinkConnected();\n}\n```\n\n初始化顺序不一定要严格遵守上述代码的顺序，但在初始化时首先要调用 setCallBack 接口 (callback 文件的编写可以参考 demo)，再调用 Mars.init，最后再调用 onForeground 和 makesureLongLinkConnect，中间顺序可以随意更改。**注意：STN 默认是后台，所以初始化 STN 后需要主动调用一次**```BaseEvent.onForeground(true)```\n\n\n需要释放 STN 或者退出程序时:\n\n```objc\n- (void)destroyMars {\n    mars::baseevent::OnDestroy();\n}\n```\n\n#### \u003ca name=\"even\"\u003eEvent Change\u003c/a\u003e\n\n前后台切换时:\n\n```objc\n- (void)reportEvent_OnForeground:(BOOL)isForeground {\n    mars::baseevent::OnForeground(isForeground);\n}\n```\n\n网络切换时：\n\n```objc\n- (void)reportEvent_OnNetworkChange {\n    mars::baseevent::OnNetworkChange();\n}\n```\n### \u003ca name=\"windows_cn\"\u003e[Windows](https://github.com/Tencent/mars/wiki/Mars-Windows-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97)\u003c/a\u003e\n安装Visual Studio 2015\n\n编译\n\n```python\npython build_windows.py\n```\n\n把 mars.lib作为依赖加入到你的项目中，把mars/libraries/mars_android_sdk/jni 目录的后缀名为 rewriteme 的文件名删掉\".rewriteme\"和头文件一起加入到你的项目中。\n\n#### \u003ca name=\"Xlog\"\u003eXlog Init\u003c/a\u003e\n\n在程序启动加载 Xlog 后紧接着初始化 Xlog。但要注意保存 log 的目录请使用单独的目录，不要存放任何其他文件防止被 xlog 自动清理功能误删。\n\n```cpp\nstd::string logPath = \"\"; //use your log path\nstd::string pubKey = \"\"; //use you pubkey for log encrypt\n\n// init xlog\n#if DEBUG\nxlogger_SetLevel(kLevelDebug);\nappender_set_console_log(true);\n#else\nxlogger_SetLevel(kLevelInfo);\nappender_set_console_log(false);\n#endif\nappender_open(kAppenderAsync, logPath.c_str(), \"Test\", 0,  pubKey.c_str());\n```\n\n在程序退出前反初始化 Xlog\n\n```cpp\nappender_close();\n```\n\n#### \u003ca name=\"STN\"\u003eSTN Init\u003c/a\u003e\n\n在你用 STN 之前初始化：\n\n```cpp\nvoid setShortLinkDebugIP(const std::string\u0026 _ip, unsigned short _port)\n{\n\tmars::stn::SetShortlinkSvrAddr(_port, _ip);\n}\nvoid setShortLinkPort(unsigned short _port)\n{\n\tmars::stn::SetShortlinkSvrAddr(_port, \"\");\n}\nvoid setLongLinkAddress(const std::string\u0026 _ip, unsigned short _port, const std::string\u0026 _debug_ip)\n{\n\tvector\u003cuint16_t\u003e ports;\n\tports.push_back(_port);\n\tmars::stn::SetLonglinkSvrAddr(_ip, ports, _debug_ip);\n}\n\nvoid Init()\n{\n\tmars::stn::SetCallback(mars::stn::StnCallBack::Instance());\n\tmars::app::SetCallback(mars::app::AppCallBack::Instance());\n\tmars::baseevent::OnCreate();\n\n\t//todo\n\t//mars::stn::SetClientVersion(version);\n\t//setShortLinkDebugIP(...)\n\t//setLongLinkAddress(...)\n\n\tmars::baseevent::OnForeground(true);\n\tmars::stn::MakesureLonglinkConnected();\n}\n```\n\n初始化顺序不一定要严格遵守上述代码的顺序，但在初始化时首先要调用 setCallBack 接口 (callback 文件的编写可以参考 demo)，再调用 Mars.init，最后再调用 onForeground 和 makesureLongLinkConnect，中间顺序可以随意更改。**注意：STN 默认是后台，所以初始化 STN 后需要主动调用一次**```BaseEvent.onForeground(true)```\n\n\n需要释放 STN 或者退出程序时:\n\n```cpp\nmars::baseevent::OnDestroy();\n```\n\n## Support\n\n还有其他问题？\n\n1. 参看 [mars/sample](https://github.com/Tencent/mars/tree/master/samples)；\n2. 阅读 [源码](https://github.com/Tencent/mars/tree/master)；\n3. 阅读 [wiki](https://github.com/Tencent/mars/wiki) 或者 [FAQ](https://github.com/Tencent/mars/wiki/Mars-%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)；\n4. 联系我们。\n\n## Contributing\n关于 Mars 分支管理、issue 以及 pr 规范，请阅读 [Mars Contributing Guide](https://github.com/Tencent/mars/blob/master/CONTRIBUTING.md)。\n\n## License\nMars 使用的 MIT 协议，详细请参考 [LICENSE](https://github.com/Tencent/mars/blob/master/LICENSE)。\n\n## 信息公示\n* SDK名称：Mars\n* 版本号：0.2.0\n* 开发者：深圳市腾讯计算机系统有限公司\n* 主要功能：Mars 是微信官方的跨平台跨业务的终端基础组件。支持iOS、macOS 和Android。Mars 针对移动网络的特点进行了优化，最终提供了高性能的网络功能。\n* [Mars 使用说明](https://github.com/Tencent/mars)\n* [Mars 个人信息保护规则](https://support.weixin.qq.com/cgi-bin/mmsupportacctnodeweb-bin/pages/L60iIjTc9G2A383f)","funding_links":[],"categories":["C++","Networking","后端开发框架及项目"],"sub_categories":["管理面板"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTencent%2Fmars","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTencent%2Fmars","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTencent%2Fmars/lists"}