mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-13 16:35:35 +08:00
Merge branch 'gitee-master' into gitlab-upstream
This commit is contained in:
commit
3a04a8dc4a
11
harmony/api9/acl_one/.gitignore
vendored
Normal file
11
harmony/api9/acl_one/.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
/node_modules
|
||||
/oh_modules
|
||||
/local.properties
|
||||
/.idea
|
||||
**/build
|
||||
/.hvigor
|
||||
.cxx
|
||||
/.clangd
|
||||
/.clang-format
|
||||
/.clang-tidy
|
||||
**/.test
|
10
harmony/api9/acl_one/AppScope/app.json5
Normal file
10
harmony/api9/acl_one/AppScope/app.json5
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"app": {
|
||||
"bundleName": "com.example.acl_one",
|
||||
"vendor": "example",
|
||||
"versionCode": 1000000,
|
||||
"versionName": "1.0.0",
|
||||
"icon": "$media:app_icon",
|
||||
"label": "$string:app_name"
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "app_name",
|
||||
"value": "acl_one"
|
||||
}
|
||||
]
|
||||
}
|
BIN
harmony/api9/acl_one/AppScope/resources/base/media/app_icon.png
Normal file
BIN
harmony/api9/acl_one/AppScope/resources/base/media/app_icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.6 KiB |
27
harmony/api9/acl_one/build-profile.json5
Normal file
27
harmony/api9/acl_one/build-profile.json5
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"app": {
|
||||
"signingConfigs": [],
|
||||
"compileSdkVersion": 9,
|
||||
"compatibleSdkVersion": 9,
|
||||
"products": [
|
||||
{
|
||||
"name": "default",
|
||||
"signingConfig": "default",
|
||||
}
|
||||
]
|
||||
},
|
||||
"modules": [
|
||||
{
|
||||
"name": "entry",
|
||||
"srcPath": "./entry",
|
||||
"targets": [
|
||||
{
|
||||
"name": "default",
|
||||
"applyToProducts": [
|
||||
"default"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
6
harmony/api9/acl_one/entry/.gitignore
vendored
Normal file
6
harmony/api9/acl_one/entry/.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
/node_modules
|
||||
/oh_modules
|
||||
/.preview
|
||||
/build
|
||||
/.cxx
|
||||
/.test
|
19
harmony/api9/acl_one/entry/build-profile.json5
Normal file
19
harmony/api9/acl_one/entry/build-profile.json5
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"apiType": 'stageMode',
|
||||
"buildOption": {
|
||||
"externalNativeOptions": {
|
||||
"path": "./src/main/cpp/CMakeLists.txt",
|
||||
"arguments": "",
|
||||
"cppFlags": "",
|
||||
}
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"name": "default",
|
||||
"runtimeOS": "HarmonyOS"
|
||||
},
|
||||
{
|
||||
"name": "ohosTest",
|
||||
}
|
||||
]
|
||||
}
|
2
harmony/api9/acl_one/entry/hvigorfile.ts
Normal file
2
harmony/api9/acl_one/entry/hvigorfile.ts
Normal file
@ -0,0 +1,2 @@
|
||||
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
|
||||
export { hapTasks } from '@ohos/hvigor-ohos-plugin';
|
12
harmony/api9/acl_one/entry/oh-package.json5
Normal file
12
harmony/api9/acl_one/entry/oh-package.json5
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "entry",
|
||||
"version": "1.0.0",
|
||||
"description": "Please describe the basic information.",
|
||||
"main": "",
|
||||
"author": "",
|
||||
"license": "",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/libentry.so": "file:./src/main/cpp/types/libentry"
|
||||
}
|
||||
}
|
61
harmony/api9/acl_one/entry/src/main/cpp/CMakeLists.txt
Normal file
61
harmony/api9/acl_one/entry/src/main/cpp/CMakeLists.txt
Normal file
@ -0,0 +1,61 @@
|
||||
# the minimum version of CMake.
|
||||
cmake_minimum_required(VERSION 3.4.1)
|
||||
project(acl_one)
|
||||
|
||||
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
include_directories(${NATIVERENDER_ROOT_PATH}
|
||||
${NATIVERENDER_ROOT_PATH}/include)
|
||||
|
||||
set(acl_home ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../..)
|
||||
set(acl_c_path ${acl_home}/lib_acl)
|
||||
set(acl_c_include ${acl_c_path}/include)
|
||||
set(pro_c_path ${acl_home}/lib_protocol)
|
||||
set(pro_c_include ${acl_home}/lib_protocol/include)
|
||||
set(acl_cpp_path ${acl_home}/lib_acl_cpp)
|
||||
set(acl_cpp_include ${acl_cpp_path}/include)
|
||||
|
||||
include_directories(
|
||||
${acl_c_include}
|
||||
${pro_c_include}
|
||||
${acl_cpp_include}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
add_definitions("-fvisibility=hidden -fvisibility-inlines-hidden")
|
||||
add_definitions("-Os")
|
||||
add_definitions("-DNDEBUG")
|
||||
add_definitions("-fvisibility=hidden")
|
||||
add_definitions("-fvisibility-inlines-hidden")
|
||||
add_definitions("-fdata-sections -ffunction-sections")
|
||||
|
||||
set(mysrc ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set(mysources
|
||||
${mysrc}
|
||||
)
|
||||
|
||||
foreach (iter ${mysources})
|
||||
aux_source_directory(${iter} mylib_src)
|
||||
endforeach()
|
||||
|
||||
add_library(entry SHARED ${mylib_src})
|
||||
#add_library(entry SHARED hello.cpp)
|
||||
|
||||
add_subdirectory(${acl_c_path} acl)
|
||||
add_subdirectory(${pro_c_path} protocol)
|
||||
add_subdirectory(${acl_cpp_path} acl_cpp)
|
||||
|
||||
set(lib_acl_path ${acl_home}/harmony/lib/${OHOS_ARCH})
|
||||
set(libacl ${lib_acl_path}/libacl.a)
|
||||
set(libacl_cpp ${lib_acl_path}/libacl_cpp.a)
|
||||
set(libprotocol ${lib_acl_path}/libprotocol.a)
|
||||
|
||||
set(lib_all ${libacl_cpp} ${libprotocol} ${libacl} -lz)
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "-shared -O3 -flto")
|
||||
|
||||
find_library(
|
||||
hilog-lib
|
||||
hilog_ndk.z
|
||||
)
|
||||
target_link_libraries(entry PUBLIC libace_napi.z.so ${lib_all} ${hilog-lib} libace_napi.z.so libc++.a)
|
||||
#target_link_libraries(entry PUBLIC libace_napi.z.so ${lib_all} ${hilog-lib} libc++.a)
|
159
harmony/api9/acl_one/entry/src/main/cpp/hello.cpp
Normal file
159
harmony/api9/acl_one/entry/src/main/cpp/hello.cpp
Normal file
@ -0,0 +1,159 @@
|
||||
#include <napi/native_api.h>
|
||||
#include <hilog/log.h>
|
||||
#include "mystdafx.h"
|
||||
#include "logger.h"
|
||||
|
||||
class http_thread : public acl::thread {
|
||||
public:
|
||||
http_thread(const char* url) : url_(url) {}
|
||||
~http_thread() = default;
|
||||
|
||||
const acl::string& result() const {
|
||||
return body_;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::string url_;
|
||||
acl::string body_;
|
||||
|
||||
// @override
|
||||
void *run() override {
|
||||
url_get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool url_get() {
|
||||
const char* addr = "110.242.68.4|80";
|
||||
addr = "www.baidu.com:80";
|
||||
// addr = "110.242.68.3";
|
||||
acl::http_request req(addr, 5);
|
||||
req.request_header().set_url("/")
|
||||
.set_host("www.baidu.com");
|
||||
|
||||
if (!req.request(nullptr, 0)) {
|
||||
log_info("%s: http request error, addr=%s", __func__, addr);
|
||||
return false;
|
||||
}
|
||||
|
||||
int http_status = req.http_status();
|
||||
acl::string buf;
|
||||
buf.format("http status=%d", http_status);
|
||||
log_info( "%s: url_get", __func__, buf.c_str());
|
||||
|
||||
if (!req.get_body(body_)) {
|
||||
log_error("%s: get http body error!", __func__);
|
||||
return false;
|
||||
}
|
||||
|
||||
int f = AI_ADDRCONFIG;
|
||||
struct sockaddr sa;
|
||||
log_info("%s: http response body: %s", __func__, body_.c_str());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
static napi_value HttpGet(napi_env env, napi_callback_info info)
|
||||
{
|
||||
log_info("%s: Started!", __func__);
|
||||
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = { nullptr };
|
||||
|
||||
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||
|
||||
char url[256];
|
||||
size_t len;
|
||||
napi_status res = napi_get_value_string_utf8(env, args[0], url, sizeof(url), &len);
|
||||
if (res != napi_ok) {
|
||||
log_info("%s: napi_get_value_string_utf8 error=%d", __func__, res);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
log_info( "%s: url=%s", __func__, url);
|
||||
|
||||
#if 0
|
||||
napi_value result;
|
||||
if (napi_create_string_utf8(env, url, strlen(url), &result) != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
#else
|
||||
http_thread http(url);
|
||||
http.set_detachable(false);
|
||||
http.start();
|
||||
http.wait();
|
||||
|
||||
const acl::string& body = http.result();
|
||||
if (body.empty()) {
|
||||
log_info("%s: http resply body empty!", __func__);
|
||||
return nullptr;
|
||||
}
|
||||
napi_value result;
|
||||
res = napi_create_string_utf8(env, body.c_str(), body.size(), &result);
|
||||
if (res != napi_ok) {
|
||||
log_info("%s: napi_create_string_utf8 error=%d", __func__, res);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
log_info("%s: At last, http body length=%zd", __func__, body.size());
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
static napi_value Add(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = 2;
|
||||
napi_value args[2] = {nullptr};
|
||||
|
||||
napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
|
||||
|
||||
napi_valuetype valuetype0;
|
||||
napi_typeof(env, args[0], &valuetype0);
|
||||
|
||||
napi_valuetype valuetype1;
|
||||
napi_typeof(env, args[1], &valuetype1);
|
||||
|
||||
double value0;
|
||||
napi_get_value_double(env, args[0], &value0);
|
||||
|
||||
double value1;
|
||||
napi_get_value_double(env, args[1], &value1);
|
||||
|
||||
napi_value sum;
|
||||
napi_create_double(env, value0 + value1, &sum);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
EXTERN_C_START
|
||||
static napi_value Init(napi_env env, napi_value exports)
|
||||
{
|
||||
acl::acl_cpp_init();
|
||||
log_open();
|
||||
|
||||
const char* version = acl_version();
|
||||
log_info("%s: Acl version=%s", __func__, version);
|
||||
|
||||
napi_property_descriptor desc[] = {
|
||||
{ "HttpGet", nullptr, HttpGet, nullptr, nullptr, nullptr, napi_default, nullptr },
|
||||
{ "Add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
return exports;
|
||||
}
|
||||
EXTERN_C_END
|
||||
|
||||
static napi_module demoModule = {
|
||||
.nm_version =1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = Init,
|
||||
.nm_modname = "entry",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
|
||||
{
|
||||
napi_module_register(&demoModule);
|
||||
}
|
126
harmony/api9/acl_one/entry/src/main/cpp/logger.cpp
Normal file
126
harmony/api9/acl_one/entry/src/main/cpp/logger.cpp
Normal file
@ -0,0 +1,126 @@
|
||||
//
|
||||
// Created by zhengshuxin on 2019/8/22.
|
||||
//
|
||||
|
||||
#include "mystdafx.h"
|
||||
#include <napi/native_api.h>
|
||||
#include <hilog/log.h>
|
||||
#include "logger.h"
|
||||
|
||||
#define LOG_ON
|
||||
|
||||
void log_info(const char* fmt, ...)
|
||||
{
|
||||
const char* ver = acl_version();
|
||||
char tag[256];
|
||||
snprintf(tag, sizeof(tag), "acl-%s", ver);
|
||||
acl::string buf;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
buf.vformat(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "tag", "%{public}s", buf.c_str());
|
||||
}
|
||||
|
||||
void log_error(const char* fmt, ...)
|
||||
{
|
||||
const char* ver = acl_version();
|
||||
char tag[256];
|
||||
snprintf(tag, sizeof(tag), "acl-%s", ver);
|
||||
acl::string buf;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
buf.vformat(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_DOMAIN, tag, "%{public}s", buf.c_str());
|
||||
}
|
||||
|
||||
void log_warn(const char* fmt, ...)
|
||||
{
|
||||
const char* ver = acl_version();
|
||||
char tag[256];
|
||||
snprintf(tag, sizeof(tag), "acl-%s", ver);
|
||||
acl::string buf;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
buf.vformat(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, tag, "%{public}s", buf.c_str());
|
||||
}
|
||||
|
||||
void log_fatal(const char* fmt, ...)
|
||||
{
|
||||
const char* ver = acl_version();
|
||||
char tag[256];
|
||||
snprintf(tag, sizeof(tag), "acl-%s", ver);
|
||||
acl::string buf;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
buf.vformat(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
OH_LOG_Print(LOG_APP, LOG_FATAL, LOG_DOMAIN, tag, "%{public}s", buf.c_str());
|
||||
}
|
||||
|
||||
void log_debug(const char* fmt, ...)
|
||||
{
|
||||
const char* ver = acl_version();
|
||||
char tag[256];
|
||||
snprintf(tag, sizeof(tag), "acl-%s", ver);
|
||||
acl::string buf;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
buf.vformat(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_DOMAIN, tag, "%{public}s", buf.c_str());
|
||||
}
|
||||
|
||||
static int open_callback(const char*, void*) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static write_fn s_write_callback;
|
||||
|
||||
static void write_callback(void*, const char* str)
|
||||
{
|
||||
log_info("%s", str);
|
||||
}
|
||||
|
||||
static void log_callback(void* ctx, const char* fmt, va_list ap) {
|
||||
static acl::string* buf = nullptr;
|
||||
if (buf == nullptr) {
|
||||
buf = new acl::string(512);
|
||||
}
|
||||
buf->vformat(fmt, ap);
|
||||
s_write_callback(ctx, buf->c_str());
|
||||
}
|
||||
|
||||
static void init_once() {
|
||||
s_write_callback = write_callback;
|
||||
acl_msg_register(open_callback, nullptr, log_callback, nullptr);
|
||||
acl_msg_open("dummy.txt", "dummy");
|
||||
}
|
||||
|
||||
static acl_pthread_once_t s_init_once = ACL_PTHREAD_ONCE_INIT;
|
||||
|
||||
void log_open()
|
||||
{
|
||||
if (acl_pthread_once(&s_init_once, init_once) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
//log_info("call logger_hook ok");
|
||||
}
|
||||
|
||||
void log_close()
|
||||
{
|
||||
}
|
16
harmony/api9/acl_one/entry/src/main/cpp/logger.h
Normal file
16
harmony/api9/acl_one/entry/src/main/cpp/logger.h
Normal file
@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by zhengshuxin on 2019/8/26.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef void (*write_fn)(void *, const char *);
|
||||
|
||||
void log_info(const char *fmt, ...);
|
||||
void log_error(const char *fmt, ...);
|
||||
void log_warn(const char *fmt, ...);
|
||||
void log_fatal(const char *fmt, ...);
|
||||
void log_debug(const char *fmt, ...);
|
||||
|
||||
void log_open();
|
||||
void log_close();
|
4
harmony/api9/acl_one/entry/src/main/cpp/mystdafx.h
Normal file
4
harmony/api9/acl_one/entry/src/main/cpp/mystdafx.h
Normal file
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib_acl.h"
|
||||
#include "acl_cpp/lib_acl.hpp"
|
2
harmony/api9/acl_one/entry/src/main/cpp/types/libentry/index.d.ts
vendored
Normal file
2
harmony/api9/acl_one/entry/src/main/cpp/types/libentry/index.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
export const Add: (a: number, b: number) => number;
|
||||
export const HttpGet: (url: string) => string;
|
@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "libentry.so",
|
||||
"types": "./index.d.ts",
|
||||
"version": "",
|
||||
"description": "Please describe the basic information."
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export default class StyleConstants {
|
||||
/**
|
||||
* Full the width.
|
||||
*/
|
||||
static readonly FULL_WIDTH: string = '100%';
|
||||
|
||||
/**
|
||||
* Full the height.
|
||||
*/
|
||||
static readonly FULL_HEIGHT: string = '100%';
|
||||
|
||||
/**
|
||||
* Web height.
|
||||
*/
|
||||
static readonly WEB_HEIGHT: string = '83%';
|
||||
|
||||
/**
|
||||
* Button width.
|
||||
*/
|
||||
static readonly BUTTON_WIDTH: string = '90%';
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
import UIAbility from '@ohos.app.ability.UIAbility';
|
||||
import hilog from '@ohos.hilog';
|
||||
import window from '@ohos.window';
|
||||
|
||||
export default class EntryAbility extends UIAbility {
|
||||
onCreate(want, launchParam) {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
|
||||
}
|
||||
|
||||
onWindowStageCreate(windowStage: window.WindowStage) {
|
||||
// Main window is created, set main page for this ability
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
|
||||
|
||||
windowStage.loadContent('pages/Index', (err, data) => {
|
||||
if (err.code) {
|
||||
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
||||
return;
|
||||
}
|
||||
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
|
||||
});
|
||||
}
|
||||
|
||||
onWindowStageDestroy() {
|
||||
// Main window is destroyed, release UI related resources
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
|
||||
}
|
||||
|
||||
onForeground() {
|
||||
// Ability has brought to foreground
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
|
||||
}
|
||||
|
||||
onBackground() {
|
||||
// Ability has back to background
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
|
||||
}
|
||||
};
|
62
harmony/api9/acl_one/entry/src/main/ets/pages/Index.ets
Normal file
62
harmony/api9/acl_one/entry/src/main/ets/pages/Index.ets
Normal file
@ -0,0 +1,62 @@
|
||||
import hilog from '@ohos.hilog';
|
||||
import testNapi from 'libentry.so'
|
||||
import worker from '@ohos.worker';
|
||||
import StyleConstant from '../common/constant/StyleConstant';
|
||||
import taskpool from '@ohos.taskpool';
|
||||
|
||||
@Concurrent
|
||||
function httpGet(url: string) : string {
|
||||
const body: string = testNapi.HttpGet(url);
|
||||
return body;
|
||||
}
|
||||
|
||||
@Concurrent
|
||||
function Add(num1, num2) : number {
|
||||
return testNapi.Add(num1, num2);
|
||||
}
|
||||
|
||||
async function asyncTask() : Promise<void> {
|
||||
try {
|
||||
let task = new taskpool.Task(Add, 10, 100);
|
||||
let num = await taskpool.execute(task);
|
||||
console.info('------------------------------>Add Result: ', num);
|
||||
} catch (e) {
|
||||
console.error("----------------------------->Taskpool execute Add error: " + e);
|
||||
}
|
||||
|
||||
try {
|
||||
let url: string = "http://www.baidu.com/";
|
||||
let task = new taskpool.Task(httpGet, url);
|
||||
let body = await taskpool.execute(task);
|
||||
if (body != null) {
|
||||
//console.info('----------------------------->body:' + String(body));
|
||||
console.info('----------------------------->body ok!');
|
||||
} else {
|
||||
console.info("------------------------------>body null");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("----------------------------->Taskpool execute HttpGet error: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
struct Index {
|
||||
@State message: string = 'Hello World'
|
||||
|
||||
build() {
|
||||
Row() {
|
||||
Column() {
|
||||
Text(this.message)
|
||||
.fontSize(50)
|
||||
.fontWeight(FontWeight.Bold)
|
||||
.onClick(() => {
|
||||
asyncTask();
|
||||
//hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.Add(20, 30));
|
||||
})
|
||||
}
|
||||
.width('100%')
|
||||
}
|
||||
.height('100%')
|
||||
}
|
||||
}
|
48
harmony/api9/acl_one/entry/src/main/module.json5
Normal file
48
harmony/api9/acl_one/entry/src/main/module.json5
Normal file
@ -0,0 +1,48 @@
|
||||
{
|
||||
"module": {
|
||||
"name": "entry",
|
||||
"type": "entry",
|
||||
"description": "$string:module_desc",
|
||||
"mainElement": "EntryAbility",
|
||||
"deviceTypes": [
|
||||
"phone",
|
||||
"tablet"
|
||||
],
|
||||
"requestPermissions":[
|
||||
{
|
||||
"name": "ohos.permission.INTERNET",
|
||||
"usedScene": {
|
||||
"abilities": [
|
||||
"EntryAbility"
|
||||
],
|
||||
"when": "inuse"
|
||||
}
|
||||
}
|
||||
],
|
||||
"deliveryWithInstall": true,
|
||||
"installationFree": false,
|
||||
"pages": "$profile:main_pages",
|
||||
"abilities": [
|
||||
{
|
||||
"name": "EntryAbility",
|
||||
"srcEntry": "./ets/entryability/EntryAbility.ts",
|
||||
"description": "$string:EntryAbility_desc",
|
||||
"icon": "$media:icon",
|
||||
"label": "$string:EntryAbility_label",
|
||||
"startWindowIcon": "$media:icon",
|
||||
"startWindowBackground": "$color:start_window_background",
|
||||
"exported": true,
|
||||
"skills": [
|
||||
{
|
||||
"entities": [
|
||||
"entity.system.home"
|
||||
],
|
||||
"actions": [
|
||||
"action.system.home"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "start_window_background",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
{
|
||||
"float": [
|
||||
{
|
||||
"name": "text_input_width",
|
||||
"value": "300"
|
||||
},
|
||||
{
|
||||
"name": "button_font_size",
|
||||
"value": "16"
|
||||
},
|
||||
{
|
||||
"name": "button_height",
|
||||
"value": "40"
|
||||
},
|
||||
{
|
||||
"name": "text_input_height",
|
||||
"value": "40"
|
||||
},
|
||||
{
|
||||
"name": "default_fontSize",
|
||||
"value": "30"
|
||||
},
|
||||
{
|
||||
"name": "image_height",
|
||||
"value": "30"
|
||||
},
|
||||
{
|
||||
"name": "image_width",
|
||||
"value": "30"
|
||||
},
|
||||
{
|
||||
"name": "border_radius",
|
||||
"value": "28"
|
||||
},
|
||||
{
|
||||
"name": "default_row_height",
|
||||
"value": "56"
|
||||
},
|
||||
{
|
||||
"name": "default_margin",
|
||||
"value": "12"
|
||||
},
|
||||
{
|
||||
"name": "default_padding",
|
||||
"value": "12"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "module description"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "description"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_label",
|
||||
"value": "label"
|
||||
}
|
||||
]
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 6.6 KiB |
@ -0,0 +1,5 @@
|
||||
{
|
||||
"src": [
|
||||
"pages/Index"
|
||||
]
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "module description"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "description"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_label",
|
||||
"value": "label"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "模块描述"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "description"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_label",
|
||||
"value": "label"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
import hilog from '@ohos.hilog';
|
||||
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
|
||||
|
||||
export default function abilityTest() {
|
||||
describe('ActsAbilityTest', function () {
|
||||
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
||||
beforeAll(function () {
|
||||
// Presets an action, which is performed only once before all test cases of the test suite start.
|
||||
// This API supports only one parameter: preset action function.
|
||||
})
|
||||
beforeEach(function () {
|
||||
// Presets an action, which is performed before each unit test case starts.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: preset action function.
|
||||
})
|
||||
afterEach(function () {
|
||||
// Presets a clear action, which is performed after each unit test case ends.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: clear action function.
|
||||
})
|
||||
afterAll(function () {
|
||||
// Presets a clear action, which is performed after all test cases of the test suite end.
|
||||
// This API supports only one parameter: clear action function.
|
||||
})
|
||||
it('assertContain',0, function () {
|
||||
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
|
||||
let a = 'abc'
|
||||
let b = 'b'
|
||||
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
|
||||
expect(a).assertContain(b)
|
||||
expect(a).assertEqual(a)
|
||||
})
|
||||
})
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
import IndexTest from './Index.test'
|
||||
import abilityTest from './Ability.test'
|
||||
|
||||
export default function testsuite() {
|
||||
abilityTest()
|
||||
IndexTest()
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
import UIAbility from '@ohos.app.ability.UIAbility';
|
||||
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
|
||||
import hilog from '@ohos.hilog';
|
||||
import { Hypium } from '@ohos/hypium';
|
||||
import testsuite from '../test/List.test';
|
||||
import window from '@ohos.window';
|
||||
|
||||
export default class TestAbility extends UIAbility {
|
||||
onCreate(want, launchParam) {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate');
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:'+ JSON.stringify(launchParam) ?? '');
|
||||
var abilityDelegator: any
|
||||
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
|
||||
var abilityDelegatorArguments: any
|
||||
abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!');
|
||||
Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite)
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy');
|
||||
}
|
||||
|
||||
onWindowStageCreate(windowStage: window.WindowStage) {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate');
|
||||
windowStage.loadContent('testability/pages/Index', (err, data) => {
|
||||
if (err.code) {
|
||||
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
||||
return;
|
||||
}
|
||||
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s',
|
||||
JSON.stringify(data) ?? '');
|
||||
});
|
||||
}
|
||||
|
||||
onWindowStageDestroy() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy');
|
||||
}
|
||||
|
||||
onForeground() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground');
|
||||
}
|
||||
|
||||
onBackground() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground');
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
import hilog from '@ohos.hilog';
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
struct Index {
|
||||
aboutToAppear() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear');
|
||||
}
|
||||
@State message: string = 'Hello World'
|
||||
build() {
|
||||
Row() {
|
||||
Column() {
|
||||
Text(this.message)
|
||||
.fontSize(50)
|
||||
.fontWeight(FontWeight.Bold)
|
||||
Button() {
|
||||
Text('next page')
|
||||
.fontSize(20)
|
||||
.fontWeight(FontWeight.Bold)
|
||||
}.type(ButtonType.Capsule)
|
||||
.margin({
|
||||
top: 20
|
||||
})
|
||||
.backgroundColor('#0D9FFB')
|
||||
.width('35%')
|
||||
.height('5%')
|
||||
.onClick(()=>{
|
||||
})
|
||||
}
|
||||
.width('100%')
|
||||
}
|
||||
.height('100%')
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
import hilog from '@ohos.hilog';
|
||||
import TestRunner from '@ohos.application.testRunner';
|
||||
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
|
||||
|
||||
var abilityDelegator = undefined
|
||||
var abilityDelegatorArguments = undefined
|
||||
|
||||
async function onAbilityCreateCallback() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback');
|
||||
}
|
||||
|
||||
async function addAbilityMonitorCallback(err: any) {
|
||||
hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? '');
|
||||
}
|
||||
|
||||
export default class OpenHarmonyTestRunner implements TestRunner {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
onPrepare() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare ');
|
||||
}
|
||||
|
||||
async onRun() {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run');
|
||||
abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
|
||||
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
|
||||
var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'
|
||||
let lMonitor = {
|
||||
abilityName: testAbilityName,
|
||||
onAbilityCreate: onAbilityCreateCallback,
|
||||
};
|
||||
abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
|
||||
var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName
|
||||
var debug = abilityDelegatorArguments.parameters['-D']
|
||||
if (debug == 'true')
|
||||
{
|
||||
cmd += ' -D'
|
||||
}
|
||||
hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd);
|
||||
abilityDelegator.executeShellCommand(cmd,
|
||||
(err: any, d: any) => {
|
||||
hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? '');
|
||||
hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? '');
|
||||
hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? '');
|
||||
})
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end');
|
||||
}
|
||||
}
|
37
harmony/api9/acl_one/entry/src/ohosTest/module.json5
Normal file
37
harmony/api9/acl_one/entry/src/ohosTest/module.json5
Normal file
@ -0,0 +1,37 @@
|
||||
{
|
||||
"module": {
|
||||
"name": "entry_test",
|
||||
"type": "feature",
|
||||
"description": "$string:module_test_desc",
|
||||
"mainElement": "TestAbility",
|
||||
"deviceTypes": [
|
||||
"phone",
|
||||
"tablet"
|
||||
],
|
||||
"deliveryWithInstall": true,
|
||||
"installationFree": false,
|
||||
"pages": "$profile:test_pages",
|
||||
"abilities": [
|
||||
{
|
||||
"name": "TestAbility",
|
||||
"srcEntry": "./ets/testability/TestAbility.ets",
|
||||
"description": "$string:TestAbility_desc",
|
||||
"icon": "$media:icon",
|
||||
"label": "$string:TestAbility_label",
|
||||
"exported": true,
|
||||
"startWindowIcon": "$media:icon",
|
||||
"startWindowBackground": "$color:start_window_background",
|
||||
"skills": [
|
||||
{
|
||||
"actions": [
|
||||
"action.system.home"
|
||||
],
|
||||
"entities": [
|
||||
"entity.system.home"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "start_window_background",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_test_desc",
|
||||
"value": "test ability description"
|
||||
},
|
||||
{
|
||||
"name": "TestAbility_desc",
|
||||
"value": "the test ability"
|
||||
},
|
||||
{
|
||||
"name": "TestAbility_label",
|
||||
"value": "test label"
|
||||
}
|
||||
]
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 6.6 KiB |
@ -0,0 +1,5 @@
|
||||
{
|
||||
"src": [
|
||||
"testability/pages/Index"
|
||||
]
|
||||
}
|
6
harmony/api9/acl_one/hvigor/hvigor-config.json5
Normal file
6
harmony/api9/acl_one/hvigor/hvigor-config.json5
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"hvigorVersion": "2.4.2",
|
||||
"dependencies": {
|
||||
"@ohos/hvigor-ohos-plugin": "2.4.2"
|
||||
}
|
||||
}
|
2
harmony/api9/acl_one/hvigor/hvigor-wrapper.js
Normal file
2
harmony/api9/acl_one/hvigor/hvigor-wrapper.js
Normal file
File diff suppressed because one or more lines are too long
2
harmony/api9/acl_one/hvigorfile.ts
Normal file
2
harmony/api9/acl_one/hvigorfile.ts
Normal file
@ -0,0 +1,2 @@
|
||||
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
|
||||
export { appTasks } from '@ohos/hvigor-ohos-plugin';
|
48
harmony/api9/acl_one/hvigorw
Normal file
48
harmony/api9/acl_one/hvigorw
Normal file
@ -0,0 +1,48 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Hvigor startup script, version 1.0.0
|
||||
#
|
||||
# Required ENV vars:
|
||||
# ------------------
|
||||
# NODE_HOME - location of a Node home dir
|
||||
# or
|
||||
# Add /usr/local/nodejs/bin to the PATH environment variable
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
HVIGOR_APP_HOME=$(dirname $(readlink -f $0))
|
||||
HVIGOR_WRAPPER_SCRIPT=${HVIGOR_APP_HOME}/hvigor/hvigor-wrapper.js
|
||||
warn() {
|
||||
echo ""
|
||||
echo -e "\033[1;33m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
|
||||
}
|
||||
|
||||
error() {
|
||||
echo ""
|
||||
echo -e "\033[1;31m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
|
||||
}
|
||||
|
||||
fail() {
|
||||
error "$@"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Determine node to start hvigor wrapper script
|
||||
if [ -n "${NODE_HOME}" ];then
|
||||
EXECUTABLE_NODE="${NODE_HOME}/bin/node"
|
||||
if [ ! -x "$EXECUTABLE_NODE" ];then
|
||||
fail "ERROR: NODE_HOME is set to an invalid directory,check $NODE_HOME\n\nPlease set NODE_HOME in your environment to the location where your nodejs installed"
|
||||
fi
|
||||
else
|
||||
EXECUTABLE_NODE="node"
|
||||
which ${EXECUTABLE_NODE} > /dev/null 2>&1 || fail "ERROR: NODE_HOME is not set and not 'node' command found in your path"
|
||||
fi
|
||||
|
||||
# Check hvigor wrapper script
|
||||
if [ ! -r "$HVIGOR_WRAPPER_SCRIPT" ];then
|
||||
fail "ERROR: Couldn't find hvigor/hvigor-wrapper.js in ${HVIGOR_APP_HOME}"
|
||||
fi
|
||||
|
||||
# start hvigor-wrapper script
|
||||
exec "${EXECUTABLE_NODE}" \
|
||||
"${HVIGOR_WRAPPER_SCRIPT}" "$@"
|
64
harmony/api9/acl_one/hvigorw.bat
Normal file
64
harmony/api9/acl_one/hvigorw.bat
Normal file
@ -0,0 +1,64 @@
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Hvigor startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
set WRAPPER_MODULE_PATH=%APP_HOME%\hvigor\hvigor-wrapper.js
|
||||
set NODE_EXE=node.exe
|
||||
|
||||
goto start
|
||||
|
||||
:start
|
||||
@rem Find node.exe
|
||||
if defined NODE_HOME goto findNodeFromNodeHome
|
||||
|
||||
%NODE_EXE% --version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: NODE_HOME is not set and no 'node' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the NODE_HOME variable in your environment to match the
|
||||
echo location of your NodeJs installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findNodeFromNodeHome
|
||||
set NODE_HOME=%NODE_HOME:"=%
|
||||
set NODE_EXE_PATH=%NODE_HOME%/%NODE_EXE%
|
||||
|
||||
if exist "%NODE_EXE_PATH%" goto execute
|
||||
echo.
|
||||
echo ERROR: NODE_HOME is not set and no 'node' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the NODE_HOME variable in your environment to match the
|
||||
echo location of your NodeJs installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:execute
|
||||
@rem Execute hvigor
|
||||
"%NODE_EXE%" %WRAPPER_MODULE_PATH% %*
|
||||
|
||||
if "%ERRORLEVEL%" == "0" goto hvigorwEnd
|
||||
|
||||
:fail
|
||||
exit /b 1
|
||||
|
||||
:hvigorwEnd
|
||||
if "%OS%" == "Windows_NT" endlocal
|
||||
|
||||
:end
|
13
harmony/api9/acl_one/oh-package-lock.json5
Normal file
13
harmony/api9/acl_one/oh-package-lock.json5
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"lockfileVersion": 1,
|
||||
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
|
||||
"specifiers": {
|
||||
"@ohos/hypium@1.0.6": "@ohos/hypium@1.0.6"
|
||||
},
|
||||
"packages": {
|
||||
"@ohos/hypium@1.0.6": {
|
||||
"resolved": "https://repo.harmonyos.com/ohpm/@ohos/hypium/-/hypium-1.0.6.tgz",
|
||||
"integrity": "sha512-bb3DWeWhYrFqj9mPFV3yZQpkm36kbcK+YYaeY9g292QKSjOdmhEIQR2ULPvyMsgSR4usOBf5nnYrDmaCCXirgQ=="
|
||||
}
|
||||
}
|
||||
}
|
13
harmony/api9/acl_one/oh-package.json5
Normal file
13
harmony/api9/acl_one/oh-package.json5
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "acl_one",
|
||||
"version": "1.0.0",
|
||||
"description": "Please describe the basic information.",
|
||||
"main": "",
|
||||
"author": "",
|
||||
"license": "",
|
||||
"dependencies": {
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ohos/hypium": "1.0.6"
|
||||
}
|
||||
}
|
@ -28,6 +28,17 @@ if (CMAKE_SYSTEM_NAME MATCHES "Android")
|
||||
add_definitions("-Wno-unused-command-line-argument")
|
||||
string(APPEND CMAKE_C_FLAGS "-Qunused-arguments")
|
||||
set(UNIX_OS true)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
add_definitions("-DANDROID")
|
||||
add_definitions("-DACL_OHOS")
|
||||
add_definitions("-O3 -flto")
|
||||
add_definitions("-DHAS_ATOMIC")
|
||||
add_definitions("-DACL_CLIENT_ONLY")
|
||||
add_definitions("-fdata-sections -ffunction-sections")
|
||||
add_definitions("-Wno-unused-command-line-argument")
|
||||
add_definitions("-Wno-c99-extensions")
|
||||
string(APPEND CMAKE_C_FLAGS "-Qunused-arguments")
|
||||
set(UNIX_OS true)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
|
||||
add_definitions("-O2")
|
||||
set(UNIX_OS true)
|
||||
@ -161,6 +172,8 @@ endif()
|
||||
|
||||
if (CMAKE_SYSTEM_NAME MATCHES "Android")
|
||||
set(lib_output_path ${CMAKE_CURRENT_SOURCE_DIR}/../android/lib/${ANDROID_ABI})
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
set(lib_output_path ${CMAKE_CURRENT_SOURCE_DIR}/../harmony/lib/${OHOS_ARCH})
|
||||
else()
|
||||
set(lib_output_path ${PROJECT_BINARY_DIR}/../lib)
|
||||
endif()
|
||||
@ -186,6 +199,9 @@ if (NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND ACL_BUILD_SHARED
|
||||
set(sys_ldflags "-shared -flto")
|
||||
endif()
|
||||
target_compile_options(acl_static PRIVATE -fvisibility=hidden)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
set(sys_ldflags "-shared -flto")
|
||||
target_compile_options(acl_static PRIVATE -fvisibility=hidden)
|
||||
elseif (${UNIX_OS})
|
||||
set(sys_ldflags "-shared -lpthread -ldl")
|
||||
target_compile_options(acl_static PRIVATE -fvisibility=hidden)
|
||||
|
@ -155,6 +155,8 @@ struct addrinfo *acl_host_addrinfo2(const char *addr, int type, int family)
|
||||
hints.ai_socktype = type;
|
||||
#ifdef ACL_MACOSX
|
||||
hints.ai_flags = AI_DEFAULT;
|
||||
#elif defined(ACL_OHOS)
|
||||
hints.ai_flags = 0;
|
||||
#elif defined(ACL_ANDROID)
|
||||
hints.ai_flags = AI_ADDRCONFIG;
|
||||
#elif defined(ACL_WINDOWS)
|
||||
|
@ -217,6 +217,7 @@ static ACL_SOCKET connect_one(const struct addrinfo *peer,
|
||||
#endif
|
||||
return ACL_SOCKET_INVALID;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
@ -284,6 +285,8 @@ static struct addrinfo *resolve_addr(const char *name, const char *service)
|
||||
hints.ai_flags = 0;
|
||||
#elif defined(ACL_MACOSX)
|
||||
hints.ai_flags = AI_DEFAULT;
|
||||
#elif defined(ACL_OHOS)
|
||||
hints.ai_flags = 0;
|
||||
#elif defined(ACL_ANDROID)
|
||||
hints.ai_flags = AI_ADDRCONFIG;
|
||||
#elif defined(ACL_WINDOWS)
|
||||
@ -298,8 +301,8 @@ static struct addrinfo *resolve_addr(const char *name, const char *service)
|
||||
return res0;
|
||||
}
|
||||
|
||||
acl_msg_error("%s(%d), %s: getaddrinfo error %s, peer=%s",
|
||||
__FILE__, __LINE__, __FUNCTION__, gai_strerror(err), name);
|
||||
acl_msg_error("%s(%d), %s: getaddrinfo error(%d) %s, peer=%s",
|
||||
__FILE__, __LINE__, __FUNCTION__, err, gai_strerror(err), name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -363,10 +366,14 @@ static int parse_addr(const char *addr, struct addr_res *res)
|
||||
|
||||
res->peer_res0 = try_numeric_addr(res->peer_family, peer,
|
||||
res->peer_port, &res->peer_buf, &res->peer_in);
|
||||
if (res->peer_res0 == NULL && (res->peer_res0 =
|
||||
resolve_addr(peer, res->peer_port)) == NULL) {
|
||||
if (res->peer_res0 == NULL) {
|
||||
res->peer_res0 = resolve_addr(peer, res->peer_port);
|
||||
if (res->peer_res0 == NULL) {
|
||||
acl_msg_error("%s(%d): resolve %s|%s error",
|
||||
__FUNCTION__, __LINE__, peer, res->peer_port);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (local != NULL) {
|
||||
/* First, check if the local is IP address. */
|
||||
|
@ -317,7 +317,7 @@ void acl_fhandle_close(ACL_FHANDLE *fs, int delay_timeout)
|
||||
if (fs->nrefer == 0) {
|
||||
acl_debug(__debug_section, 2)
|
||||
("%s: fpath: %s, when_free: %ld, now: %ld",
|
||||
myname, PATH(fs->fp), fs->when_free, now);
|
||||
myname, PATH(fs->fp), (long) fs->when_free, (long) now);
|
||||
__fhandle_close(fs);
|
||||
}
|
||||
iter = iter_next;
|
||||
|
@ -74,7 +74,7 @@ int acl_write_wait_ms(ACL_SOCKET fd, int timeout)
|
||||
end = time(NULL);
|
||||
acl_msg_error("%s(%d), %s: poll return 0, delay=%d, "
|
||||
"fd=%d, cost=%ld", __FILE__, __LINE__,
|
||||
myname, delay, fd, end - begin);
|
||||
myname, delay, fd, (long) ( end - begin));
|
||||
return -1;
|
||||
default:
|
||||
if (fds.revents & POLLNVAL) {
|
||||
|
@ -47,15 +47,17 @@ ACL_DBUF_POOL *acl_dbuf_pool_create(size_t block_size)
|
||||
memset(&info, 0, sizeof(SYSTEM_INFO));
|
||||
GetSystemInfo(&info);
|
||||
page_size = info.dwPageSize;
|
||||
if (page_size <= 0)
|
||||
if (page_size <= 0) {
|
||||
page_size = 4096;
|
||||
}
|
||||
#else
|
||||
page_size = 4096;
|
||||
#endif
|
||||
|
||||
size = (block_size / (size_t) page_size) * (size_t) page_size;
|
||||
if (size < (size_t) page_size)
|
||||
if (size < (size_t) page_size) {
|
||||
size = page_size;
|
||||
}
|
||||
|
||||
/* xxx: 为了尽量保证在调用 acl_mymalloc 分配内存时为内存页的整数倍,
|
||||
* 需要减去 sizeof(ACL_DBUF) 和 16 字节,其中 16 字节是 acl_mymalloc
|
||||
@ -78,7 +80,7 @@ ACL_DBUF_POOL *acl_dbuf_pool_create(size_t block_size)
|
||||
pool->head = (ACL_DBUF*) pool->buf;
|
||||
pool->head->next = NULL;
|
||||
pool->head->keep = 1;
|
||||
pool->head->used = 0;
|
||||
pool->head->used = 1;
|
||||
pool->head->size = size;
|
||||
pool->head->addr = pool->head->buf;
|
||||
pool->count = 1;
|
||||
@ -93,10 +95,13 @@ void acl_dbuf_pool_destroy(ACL_DBUF_POOL *pool)
|
||||
while (iter) {
|
||||
tmp = iter;
|
||||
iter = iter->next;
|
||||
if ((char*) tmp == pool->buf)
|
||||
if ((char*) tmp == pool->buf) {
|
||||
break;
|
||||
if (tmp->size > pool->block_size)
|
||||
}
|
||||
if (tmp->size > pool->block_size) {
|
||||
pool->huge--;
|
||||
}
|
||||
|
||||
#ifdef USE_VALLOC
|
||||
free(tmp);
|
||||
#else
|
||||
|
@ -145,23 +145,6 @@ int acl_socket_close(ACL_SOCKET fd)
|
||||
int acl_socket_read(ACL_SOCKET fd, void *buf, size_t size,
|
||||
int timeout, ACL_VSTREAM *fp, void *arg acl_unused)
|
||||
{
|
||||
#if 0
|
||||
WSABUF wsaData;
|
||||
DWORD dwBytes = 0;
|
||||
DWORD flags = 0;
|
||||
int ret;
|
||||
|
||||
wsaData.len = (u_long) size;
|
||||
wsaData.buf = (char*) buf;
|
||||
ret = WSARecv(fd, &wsaData, 1, &dwBytes, &flags, NULL, NULL);
|
||||
if (ret == SOCKET_ERROR) {
|
||||
return -1;
|
||||
}
|
||||
if (dwBytes == 0) {
|
||||
return -1;
|
||||
}
|
||||
return dwBytes;
|
||||
#else
|
||||
if (fp != NULL && fp->read_ready) {
|
||||
fp->read_ready = 0;
|
||||
} else if (timeout > 0) {
|
||||
@ -174,7 +157,7 @@ int acl_socket_read(ACL_SOCKET fd, void *buf, size_t size,
|
||||
}
|
||||
}
|
||||
|
||||
# if defined(_WIN32) || defined(_WIN64)
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
# ifdef SYS_WSA_API
|
||||
WSABUF wsabuf;
|
||||
wsabuf.buf = buf;
|
||||
@ -184,15 +167,15 @@ int acl_socket_read(ACL_SOCKET fd, void *buf, size_t size,
|
||||
# else
|
||||
return __sys_recv(fd, buf, (int) size, 0);
|
||||
# endif
|
||||
# else
|
||||
#else
|
||||
return __sys_recv(fd, buf, (int) size, 0);
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
|
||||
int acl_socket_write(ACL_SOCKET fd, const void *buf, size_t size,
|
||||
int timeout, ACL_VSTREAM *fp acl_unused, void *arg acl_unused)
|
||||
{
|
||||
#ifdef ACL_WRITE_FIRST
|
||||
int ret, error;
|
||||
|
||||
ret = __sys_send(fd, buf, (int) size, 0);
|
||||
@ -207,13 +190,14 @@ int acl_socket_write(ACL_SOCKET fd, const void *buf, size_t size,
|
||||
|
||||
error = acl_last_error();
|
||||
|
||||
#if ACL_EWOULDBLOCK == ACL_EAGAIN
|
||||
# if ACL_EWOULDBLOCK == ACL_EAGAIN
|
||||
if (error != ACL_EWOULDBLOCK) {
|
||||
#else
|
||||
# else
|
||||
if (error != ACL_EWOULDBLOCK && error != ACL_EAGAIN) {
|
||||
#endif
|
||||
# endif
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ACL_WRITEABLE_CHECK
|
||||
if (fp != NULL && ACL_VSTREAM_IS_MS(fp)) {
|
||||
@ -225,8 +209,10 @@ int acl_socket_write(ACL_SOCKET fd, const void *buf, size_t size,
|
||||
}
|
||||
|
||||
return __sys_send(fd, buf, (int) size, 0);
|
||||
#else
|
||||
#elif defined(ACL_WRITE_FIRST)
|
||||
return ret;
|
||||
#else
|
||||
# error "One of ACL_WRITE_FIRST or ACL_WRITEABLE_CHECK must be defined at least!"
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -459,6 +445,7 @@ int acl_socket_read(ACL_SOCKET fd, void *buf, size_t size,
|
||||
int acl_socket_write(ACL_SOCKET fd, const void *buf, size_t size,
|
||||
int timeout, ACL_VSTREAM *fp acl_unused, void *arg acl_unused)
|
||||
{
|
||||
#ifdef ACL_WRITE_FIRST
|
||||
int ret, error;
|
||||
|
||||
ret = (int) __sys_write(fd, buf, size);
|
||||
@ -472,15 +459,17 @@ int acl_socket_write(ACL_SOCKET fd, const void *buf, size_t size,
|
||||
|
||||
error = acl_last_error();
|
||||
|
||||
#if ACL_EWOULDBLOCK == ACL_EAGAIN
|
||||
# if ACL_EWOULDBLOCK == ACL_EAGAIN
|
||||
if (error != ACL_EWOULDBLOCK) {
|
||||
#else
|
||||
# else
|
||||
if (error != ACL_EWOULDBLOCK && error != ACL_EAGAIN) {
|
||||
#endif
|
||||
# endif
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ACL_WRITEABLE_CHECK
|
||||
if (timeout > 0) {
|
||||
if (fp != NULL && ACL_VSTREAM_IS_MS(fp)) {
|
||||
if (acl_write_wait_ms(fd, timeout) < 0) {
|
||||
return -1;
|
||||
@ -488,16 +477,20 @@ int acl_socket_write(ACL_SOCKET fd, const void *buf, size_t size,
|
||||
} else if (acl_write_wait(fd, timeout) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
ret = __sys_write(fd, buf, size);
|
||||
#endif
|
||||
|
||||
return __sys_write(fd, buf, size);
|
||||
#elif defined(ACL_WRITE_FIRST)
|
||||
return ret;
|
||||
#else
|
||||
# error "One of ACL_WRITE_FIRST or ACL_WRITEABLE_CHECK must be defined at least!"
|
||||
#endif
|
||||
}
|
||||
|
||||
int acl_socket_writev(ACL_SOCKET fd, const struct iovec *vec, int count,
|
||||
int timeout, ACL_VSTREAM *fp acl_unused, void *arg acl_unused)
|
||||
{
|
||||
#ifdef ACL_WRITE_FIRST
|
||||
int ret, error;
|
||||
|
||||
ret = (int) __sys_writev(fd, vec, count);
|
||||
@ -511,23 +504,26 @@ int acl_socket_writev(ACL_SOCKET fd, const struct iovec *vec, int count,
|
||||
|
||||
error = acl_last_error();
|
||||
|
||||
#if ACL_EWOULDBLOCK == ACL_EAGAIN
|
||||
# if ACL_EWOULDBLOCK == ACL_EAGAIN
|
||||
if (error != ACL_EWOULDBLOCK) {
|
||||
#else
|
||||
# else
|
||||
if (error != ACL_EWOULDBLOCK && error != ACL_EAGAIN) {
|
||||
#endif
|
||||
# endif
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ACL_WRITEABLE_CHECK
|
||||
if (acl_write_wait(fd, timeout) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = __sys_writev(fd, vec, count);
|
||||
#endif
|
||||
|
||||
return __sys_writev(fd, vec, count);
|
||||
#elif defined(ACL_WRITE_FIRST)
|
||||
return ret;
|
||||
#else
|
||||
# error "One of ACL_WRITE_FIRST or ACL_WRITEABLE_CHECK must be defined at least!"
|
||||
#endif
|
||||
}
|
||||
|
||||
#else
|
||||
|
@ -25,6 +25,15 @@ if(CMAKE_SYSTEM_NAME MATCHES "Android")
|
||||
add_definitions("-fdata-sections -ffunction-sections")
|
||||
string(APPEND CMAKE_CXX_FLAGS "-Qunused-arguments")
|
||||
set(UNIX_OS true)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
add_definitions("-O3 -flto")
|
||||
add_definitions("-DANDROID")
|
||||
add_definitions("-DACL_CPP_LOG_SKIP_FILE")
|
||||
add_definitions("-fdata-sections -ffunction-sections")
|
||||
add_definitions("-Wno-unused-command-line-argument")
|
||||
add_definitions("-Wno-c99-extensions")
|
||||
string(APPEND CMAKE_CXX_FLAGS "-Qunused-arguments")
|
||||
set(UNIX_OS true)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
|
||||
# add_definitions("-Wno-invalid-source-encoding")
|
||||
add_definitions("-O2")
|
||||
@ -225,6 +234,8 @@ endif()
|
||||
|
||||
if (CMAKE_SYSTEM_NAME MATCHES "Android")
|
||||
set(lib_output_path ${CMAKE_CURRENT_SOURCE_DIR}/../android/lib/${ANDROID_ABI})
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
set(lib_output_path ${CMAKE_CURRENT_SOURCE_DIR}/../harmony/lib/${OHOS_ARCH})
|
||||
else()
|
||||
set(lib_output_path ${PROJECT_BINARY_DIR}/../lib)
|
||||
endif()
|
||||
@ -251,6 +262,10 @@ if (NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND ACL_BUILD_SHARED
|
||||
endif()
|
||||
target_compile_options(acl_cpp_static PRIVATE
|
||||
-fvisibility=hidden -fvisibility-inlines-hidden)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
set(sys_ldflags "-shared -flto -lz")
|
||||
target_compile_options(acl_cpp_static PRIVATE
|
||||
-fvisibility=hidden -fvisibility-inlines-hidden)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
|
||||
set(sys_ldflags "-shared -lz -liconv -lpthread -ldl")
|
||||
target_compile_options(acl_cpp_static PRIVATE
|
||||
|
@ -5,8 +5,7 @@
|
||||
|
||||
struct ACL_DBUF_POOL;
|
||||
|
||||
namespace acl
|
||||
{
|
||||
namespace acl {
|
||||
|
||||
/**
|
||||
* 会话类的内存链管理类,该类仅提供内存分配函数,在整个类对象被析构时该内存链
|
||||
@ -14,13 +13,18 @@ namespace acl
|
||||
* 该类实际上是封装了 lib_acl 中的 ACL_DBUF_POOL 结构及方法
|
||||
*/
|
||||
|
||||
class ACL_CPP_API dbuf_pool // : public noncopyable
|
||||
{
|
||||
#if 0
|
||||
#ifndef ACL_DBUF_HOOK_NEW
|
||||
# define ACL_DBUF_HOOK_NEW
|
||||
#endif
|
||||
#endif
|
||||
|
||||
class ACL_CPP_API dbuf_pool { // : public noncopyable
|
||||
public:
|
||||
/**
|
||||
* 该类对象必须动态创建
|
||||
*/
|
||||
dbuf_pool();
|
||||
dbuf_pool(size_t nblock = 2);
|
||||
|
||||
/**
|
||||
* 该类对象必须要动态创建,所以隐藏了析构函数,使用者需要调用 destroy
|
||||
@ -28,6 +32,7 @@ public:
|
||||
*/
|
||||
void destroy();
|
||||
|
||||
#ifdef ACL_DBUF_HOOK_NEW
|
||||
/**
|
||||
* 重载 new/delete 操作符,使 dbuf_pool 对象本身也创建在内存池上,
|
||||
* 从而减少了 malloc/free 的次数
|
||||
@ -36,10 +41,11 @@ public:
|
||||
*/
|
||||
static void *operator new(size_t size, size_t nblock = 2);
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
# if defined(_WIN32) || defined(_WIN64)
|
||||
static void operator delete(void* ptr, size_t);
|
||||
#endif
|
||||
# endif
|
||||
static void operator delete(void* ptr);
|
||||
#endif // ACL_DBUF_HOOK_NEW
|
||||
|
||||
/**
|
||||
* 重置内存池的状态以便于重复使用该内存池对象
|
||||
@ -158,8 +164,7 @@ class dbuf_guard;
|
||||
/**
|
||||
* 在会话内存池对象上分配的对象基础类
|
||||
*/
|
||||
class ACL_CPP_API dbuf_obj //: public noncopyable
|
||||
{
|
||||
class ACL_CPP_API dbuf_obj { //: public noncopyable
|
||||
public:
|
||||
/**
|
||||
* 构造函数
|
||||
@ -207,8 +212,7 @@ private:
|
||||
* 会话内存池管理器,由该类对象管理 dbuf_pool 对象及在其上分配的对象,当该类
|
||||
* 对象销毁时,dbuf_pool 对象及在上面均被释放。
|
||||
*/
|
||||
class ACL_CPP_API dbuf_guard // : public noncopyable
|
||||
{
|
||||
class ACL_CPP_API dbuf_guard { // : public noncopyable
|
||||
public:
|
||||
/**
|
||||
* 构造函数
|
||||
|
@ -117,12 +117,28 @@ public:
|
||||
*/
|
||||
bool push_ssl_ctx(SSL_CTX* ctx);
|
||||
|
||||
/**
|
||||
* 在设置读写超时时,是否使用 setsockopt()
|
||||
* @param yes {bool} 如果为 true 则使用 setsockopt 设置读写超时,否则
|
||||
* 使用 acl_read_wait/acl_write_wait 检查超时情景.
|
||||
*/
|
||||
void use_sockopt_timeout(bool yes);
|
||||
|
||||
/**
|
||||
* 是否需要使用 setsockopt() 设置网络超时时间.
|
||||
* @return {bool}
|
||||
*/
|
||||
bool is_sockopt_timeout() const {
|
||||
return sockopt_timeout_;
|
||||
}
|
||||
|
||||
private:
|
||||
bool server_side_;
|
||||
SSL_CTX* ssl_ctx_; // The default SSL_CTX.
|
||||
token_tree* ssl_ctx_table_; // Holding the map of host/SSL_CTX.
|
||||
std::set<SSL_CTX*> ssl_ctxes_; // Holding all ctx just for freeing.
|
||||
int timeout_;
|
||||
bool sockopt_timeout_;
|
||||
string crt_file_;
|
||||
unsigned status_;
|
||||
|
||||
|
@ -40,7 +40,11 @@ int main(int argc, char* argv[])
|
||||
|
||||
acl::log::stdout_open(true);
|
||||
|
||||
#ifdef ACL_DBUF_HOOK_NEW
|
||||
acl::dbuf_pool* dbuf = new (100) acl::dbuf_pool;
|
||||
#else
|
||||
acl::dbuf_pool* dbuf = new acl::dbuf_pool(100);
|
||||
#endif
|
||||
|
||||
for (int i = 0; i < 102400; i++)
|
||||
dbuf->dbuf_alloc(10);
|
||||
|
@ -66,15 +66,15 @@ int main(int argc, char* argv[])
|
||||
|
||||
acl::sslbase_conf* ssl_conf = NULL;
|
||||
if (libpath.find("mbedtls")) {
|
||||
ssl_conf = new acl::mbedtls_conf(false);
|
||||
acl::mbedtls_conf::set_libpath(libpath.c_str());
|
||||
ssl_conf = new acl::mbedtls_conf(false);
|
||||
if (!acl::mbedtls_conf::load()) {
|
||||
printf("load %s error\r\n", libpath.c_str());
|
||||
return 1;
|
||||
}
|
||||
} else if (libpath.find("polarssl")) {
|
||||
ssl_conf = new acl::polarssl_conf;
|
||||
acl::polarssl_conf::set_libpath(libpath);
|
||||
ssl_conf = new acl::polarssl_conf;
|
||||
if (!acl::polarssl_conf::load()) {
|
||||
printf("load %s error\r\n", libpath.c_str());
|
||||
return 1;
|
||||
|
@ -8,11 +8,11 @@ if [ $os == 'Darwin' ]; then
|
||||
echo ""
|
||||
./https_request -f "../libmbedtls_all.dylib" -s echo.websocket.org:443 -S
|
||||
else
|
||||
./https_request -f "../libmbedcrypto.so;../libmbedx509.so;../libmbedtls.so" -s www.sina.com.cn:443 -S
|
||||
./https_request -f "../libmbedtls_all.so" -s www.sina.com.cn:443 -S
|
||||
echo ""
|
||||
./https_request -f "../libmbedcrypto.so;../libmbedx509.so;../libmbedtls.so" -s www.baidu.com:443 -S
|
||||
./https_request -f "../libmbedtls_all.so" -s www.baidu.com:443 -S
|
||||
echo ""
|
||||
./https_request -f "../libmbedcrypto.so;../libmbedx509.so;../libmbedtls.so" -s echo.websocket.org:443 -S
|
||||
./https_request -f "../libmbedtls_all.so" -s echo.websocket.org:443 -S
|
||||
fi
|
||||
|
||||
# ./https_request -f "/usr/local/lib64/libcrypto.so;/usr/local/lib64/libssl.so" -s www.baidu.com:443 -H www.baidu.com -U / -n 1 -S
|
||||
|
@ -41,7 +41,12 @@ void redis_command::init(void)
|
||||
addr_[0] = 0;
|
||||
|
||||
#define REDIS_DBUF_NBLOCK 1
|
||||
|
||||
#ifdef ACL_DBUF_HOOK_NEW
|
||||
dbuf_ = new (REDIS_DBUF_NBLOCK) dbuf_pool();
|
||||
#else
|
||||
dbuf_ = new dbuf_pool(REDIS_DBUF_NBLOCK);
|
||||
#endif
|
||||
}
|
||||
|
||||
redis_command::redis_command(void)
|
||||
|
@ -7,12 +7,21 @@
|
||||
namespace acl
|
||||
{
|
||||
|
||||
dbuf_pool::dbuf_pool(void)
|
||||
dbuf_pool::dbuf_pool(size_t nblock /* = 2 */)
|
||||
{
|
||||
#ifdef ACL_DBUF_HOOK_NEW
|
||||
(void) nblock;
|
||||
#else
|
||||
pool_ = acl_dbuf_pool_create(4096 * nblock);
|
||||
mysize_ = sizeof(dbuf_pool);
|
||||
#endif
|
||||
}
|
||||
|
||||
dbuf_pool::~dbuf_pool(void)
|
||||
{
|
||||
#ifndef ACL_DBUF_HOOK_NEW
|
||||
acl_dbuf_pool_destroy(pool_);
|
||||
#endif
|
||||
}
|
||||
|
||||
void dbuf_pool::destroy(void)
|
||||
@ -20,6 +29,8 @@ void dbuf_pool::destroy(void)
|
||||
delete this;
|
||||
}
|
||||
|
||||
#ifdef ACL_DBUF_HOOK_NEW
|
||||
|
||||
void *dbuf_pool::operator new(size_t size, size_t nblock /* = 2 */)
|
||||
{
|
||||
if (nblock == 0) {
|
||||
@ -38,13 +49,13 @@ void *dbuf_pool::operator new(size_t size, size_t nblock /* = 2 */)
|
||||
return dbuf;
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
# if defined(_WIN32) || defined(_WIN64)
|
||||
void dbuf_pool::operator delete(void* ptr, size_t)
|
||||
{
|
||||
dbuf_pool* dbuf = (dbuf_pool*) ptr;
|
||||
acl_dbuf_pool_destroy(dbuf->pool_);
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
void dbuf_pool::operator delete(void* ptr)
|
||||
{
|
||||
@ -52,6 +63,8 @@ void dbuf_pool::operator delete(void* ptr)
|
||||
acl_dbuf_pool_destroy(dbuf->pool_);
|
||||
}
|
||||
|
||||
#endif // ACL_DBUF_HOOK_NEW
|
||||
|
||||
bool dbuf_pool::dbuf_reset(size_t reserve /* = 0 */)
|
||||
{
|
||||
return acl_dbuf_pool_reset(pool_, mysize_ + reserve) == 0;
|
||||
@ -129,7 +142,11 @@ dbuf_guard::dbuf_guard(acl::dbuf_pool* dbuf, size_t capacity /* = 500 */)
|
||||
, size_(0)
|
||||
{
|
||||
if (dbuf == NULL) {
|
||||
#ifdef DBUF_HOOK_NEW
|
||||
dbuf_ = dbuf_internal_ = new (nblock_) acl::dbuf_pool;
|
||||
#else
|
||||
dbuf_ = dbuf_internal_ = new acl::dbuf_pool(nblock_);
|
||||
#endif
|
||||
} else {
|
||||
dbuf_ = dbuf;
|
||||
dbuf_internal_ = NULL;
|
||||
@ -143,7 +160,11 @@ dbuf_guard::dbuf_guard(size_t nblock /* = 2 */, size_t capacity /* = 500 */)
|
||||
, incr_(500)
|
||||
, size_(0)
|
||||
{
|
||||
#ifdef DBUF_HOOK_NEW
|
||||
dbuf_ = dbuf_internal_ = new (nblock_) acl::dbuf_pool;
|
||||
#else
|
||||
dbuf_ = dbuf_internal_ = new acl::dbuf_pool(nblock_);
|
||||
#endif
|
||||
init(capacity);
|
||||
}
|
||||
|
||||
|
@ -550,6 +550,7 @@ openssl_conf::openssl_conf(bool server_side /* false */, int timeout /* 30 */)
|
||||
, ssl_ctx_(NULL)
|
||||
, ssl_ctx_table_(NULL)
|
||||
, timeout_(timeout)
|
||||
, sockopt_timeout_(false)
|
||||
{
|
||||
#ifdef HAS_OPENSSL
|
||||
// Init OpenSSL globally, and the dynamic libs will be loaded
|
||||
@ -590,6 +591,12 @@ openssl_conf::~openssl_conf(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
void openssl_conf::use_sockopt_timeout(bool yes)
|
||||
{
|
||||
sockopt_timeout_ = yes;
|
||||
}
|
||||
|
||||
|
||||
SSL_CTX* openssl_conf::create_ssl_ctx(void)
|
||||
{
|
||||
#ifdef HAS_OPENSSL
|
||||
|
@ -305,24 +305,66 @@ bool openssl_io::on_close(bool alive)
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool set_sock_timeout(ACL_SOCKET fd, int opt, int timeout)
|
||||
{
|
||||
if (timeout <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(__APPLE__) || defined(_WIN32) || defined(_WIN64)
|
||||
timeout *= 1000; // From seconds to millisecond.
|
||||
if (setsockopt(fd, SOL_SOCKET, opt, &timeout, sizeof(timeout)) < 0) {
|
||||
logger_error("setsockopt error=%s, timeout=%d, opt=%d, fd=%d",
|
||||
last_serror(), timeout, opt, (int) fd);
|
||||
return false;
|
||||
}
|
||||
#else // Must be Linux.
|
||||
struct timeval tm;
|
||||
tm.tv_sec = timeout;
|
||||
tm.tv_usec = 0;
|
||||
|
||||
if (setsockopt(fd, SOL_SOCKET, opt, &tm, sizeof(tm)) < 0) {
|
||||
logger_error("setsockopt error=%s, timeout=%d, opt=%d, fd=%d",
|
||||
last_serror(), timeout, opt, (int) fd);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
int openssl_io::read(void* buf, size_t len)
|
||||
{
|
||||
#ifdef HAS_OPENSSL
|
||||
size_t nbytes = 0;
|
||||
char* ptr = (char*) buf;
|
||||
int timeout = nblock_ ? 0 : this->stream_->rw_timeout;
|
||||
int timeo = nblock_ ? 0 : this->stream_->rw_timeout;
|
||||
ACL_SOCKET fd = ACL_VSTREAM_SOCK(this->stream_);
|
||||
bool opt = conf_.is_sockopt_timeout();
|
||||
|
||||
if (timeo > 0 && opt && !set_sock_timeout(fd, SO_RCVTIMEO, timeo)) {
|
||||
logger_error("Can't set read timeout, fd=%d", fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//#define DEBUG_READ_TIMEOUT
|
||||
|
||||
while (len > 0) {
|
||||
//time_t begin = time(NULL);
|
||||
if (acl_read_wait(fd, timeout) < 0) {
|
||||
//time_t end = time(NULL);
|
||||
//logger_error("acl_read_wait error=%s, fd=%d, cost=%ld",
|
||||
// last_serror(), (int) fd, (long) (end - begin));
|
||||
#ifdef DEBUG_READ_TIMEOUT
|
||||
time_t begin = time(NULL);
|
||||
#endif
|
||||
if (timeo > 0 && !opt && acl_read_wait(fd, timeo) < 0) {
|
||||
#ifdef DEBUG_READ_TIMEOUT
|
||||
time_t end = time(NULL);
|
||||
logger_error("read_wait error=%s, fd=%d, cost=%ld, ttl=%d",
|
||||
last_serror(), (int) fd, (long) (end - begin), timeo);
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = __ssl_read(ssl_, ptr, (int) len);
|
||||
|
||||
this->stream_->read_ready = 0;
|
||||
|
||||
if (ret > 0) {
|
||||
nbytes += ret;
|
||||
ptr += ret;
|
||||
@ -331,37 +373,36 @@ int openssl_io::read(void* buf, size_t len)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (nbytes > 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
this->stream_->read_ready = 0;
|
||||
|
||||
int err = __ssl_get_error(ssl_, ret);
|
||||
|
||||
switch (err) {
|
||||
case SSL_ERROR_WANT_READ:
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
if (nblock_) {
|
||||
return ACL_VSTREAM_EOF;
|
||||
}
|
||||
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
|
||||
if (nblock_ || opt) {
|
||||
break;
|
||||
case SSL_ERROR_ZERO_RETURN:
|
||||
case SSL_ERROR_SYSCALL:
|
||||
default:
|
||||
}
|
||||
} else {
|
||||
// SSL_ERROR_ZERO_RETURN, SSL_ERROR_SYSCALL, others.
|
||||
|
||||
/*
|
||||
if (ERR_peek_error() == 0) {
|
||||
}
|
||||
*/
|
||||
return ACL_VSTREAM_EOF;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
if (len == 0) {
|
||||
this->stream_->read_ready = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
return nbytes > 0 ? (int) nbytes : ACL_VSTREAM_EOF;
|
||||
return nbytes > 0 ? (int) nbytes : -1;
|
||||
#else
|
||||
(void) buf;
|
||||
(void) len;
|
||||
@ -375,12 +416,18 @@ int openssl_io::send(const void* buf, size_t len)
|
||||
#ifdef HAS_OPENSSL
|
||||
size_t nbytes = 0;
|
||||
char* ptr = (char*) buf;
|
||||
int timeout = nblock_ ? 0 : this->stream_->rw_timeout;
|
||||
int timeo = nblock_ ? 0 : this->stream_->rw_timeout;
|
||||
ACL_SOCKET fd = ACL_VSTREAM_SOCK(this->stream_);
|
||||
bool opt = conf_.is_sockopt_timeout();
|
||||
|
||||
if (timeo > 0 && opt && !set_sock_timeout(fd, SO_SNDTIMEO, timeo)) {
|
||||
logger_error("Can't set send timeout, fd=%d", fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (len > 0) {
|
||||
//time_t begin = time(NULL);
|
||||
if (acl_write_wait(fd, timeout) < 0) {
|
||||
if (timeo > 0 && !opt && acl_write_wait(fd, timeo) < 0) {
|
||||
//time_t end = time(NULL);
|
||||
//logger_error("acl_write_wait error=%s, fd=%d, cost=%ld",
|
||||
// last_serror(), (int) fd, (long) (end - begin));
|
||||
@ -396,19 +443,28 @@ int openssl_io::send(const void* buf, size_t len)
|
||||
break;
|
||||
}
|
||||
|
||||
int err = __ssl_get_error(ssl_, ret);
|
||||
switch (err) {
|
||||
case SSL_ERROR_WANT_READ:
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
break;
|
||||
case SSL_ERROR_SYSCALL:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (nbytes > 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
return nbytes > 0 ? (int) nbytes : ACL_VSTREAM_EOF;
|
||||
int err = __ssl_get_error(ssl_, ret);
|
||||
|
||||
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
|
||||
if (nblock_ || opt) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// SSL_ERROR_ZERO_RETURN, SSL_ERROR_SYSCALL, others.
|
||||
|
||||
/*
|
||||
if (ERR_peek_error() == 0) {
|
||||
}
|
||||
*/
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return nbytes > 0 ? (int) nbytes : -1;
|
||||
#else
|
||||
(void) buf;
|
||||
(void) len;
|
||||
|
@ -274,36 +274,36 @@ FIBER_API void acl_fiber_schedule_stop(void);
|
||||
|
||||
/**
|
||||
* Let the current fiber sleep for a while
|
||||
* @param milliseconds {unsigned int} the milliseconds to sleep
|
||||
* @param milliseconds {size_t} the milliseconds to sleep
|
||||
* @return {unsigned int} the rest milliseconds returned after wakeup
|
||||
*/
|
||||
FIBER_API unsigned int acl_fiber_delay(unsigned int milliseconds);
|
||||
FIBER_API size_t acl_fiber_delay(size_t milliseconds);
|
||||
|
||||
/**
|
||||
* Let the current fiber sleep for a while
|
||||
* @param seconds {unsigned int} the seconds to sleep
|
||||
* @return {unsigned int} the rest seconds returned after wakeup
|
||||
* @param seconds {size_t} the seconds to sleep
|
||||
* @return {size_t} the rest seconds returned after wakeup
|
||||
*/
|
||||
FIBER_API unsigned int acl_fiber_sleep(unsigned int seconds);
|
||||
FIBER_API size_t acl_fiber_sleep(size_t seconds);
|
||||
|
||||
/**
|
||||
* Create one fiber timer
|
||||
* @param milliseconds {unsigned int} the timer wakeup milliseconds
|
||||
* @param milliseconds {size_t} the timer wakeup milliseconds
|
||||
* @param size {size_t} the virtual memory of the created fiber
|
||||
* @param fn {void (*)(ACL_FIBER*, void*)} the callback when fiber wakeup
|
||||
* @param ctx {void*} the second parameter of the callback fn
|
||||
* @return {ACL_FIBER*} the new created fiber returned
|
||||
*/
|
||||
FIBER_API ACL_FIBER* acl_fiber_create_timer(unsigned int milliseconds,
|
||||
FIBER_API ACL_FIBER* acl_fiber_create_timer(size_t milliseconds,
|
||||
size_t size, void (*fn)(ACL_FIBER*, void*), void* ctx);
|
||||
|
||||
/**
|
||||
* Reset the timer milliseconds time before the timer fiber wakeup
|
||||
* @param timer {ACL_FIBER*} the fiber created by acl_fiber_create_timer
|
||||
* @param milliseconds {unsigned int} the new timer wakeup milliseconds
|
||||
* @param milliseconds {size_t} the new timer wakeup milliseconds
|
||||
* @return {int} return 0 if rest timer success, else return -1 if failed
|
||||
*/
|
||||
FIBER_API int acl_fiber_reset_timer(ACL_FIBER* timer, unsigned int milliseconds);
|
||||
FIBER_API int acl_fiber_reset_timer(ACL_FIBER* timer, size_t milliseconds);
|
||||
|
||||
/**
|
||||
* Set the DNS service addr
|
||||
|
@ -104,12 +104,18 @@ int timer_cache_remove(TIMER_CACHE *cache, long long expire, RING *entry)
|
||||
}
|
||||
|
||||
if (entry->parent != &node->ring) {
|
||||
// Maybe the fiber has been append to the other ring.
|
||||
if (ring_size(&node->ring) == 0) {
|
||||
timer_cache_free_node(cache, node);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Detach the fiber from the current timer node.
|
||||
ring_detach(entry);
|
||||
|
||||
if (ring_size(&node->ring) == 0) {
|
||||
// If the timer node is empty, just free it now.
|
||||
timer_cache_free_node(cache, node);
|
||||
}
|
||||
return 1;
|
||||
|
@ -199,6 +199,9 @@ struct FILE_EVENT {
|
||||
#define EVENT_SENDMSG (unsigned) (1 << 28)
|
||||
#endif // HAS_IO_URING
|
||||
|
||||
#define EVENT_SO_RCVTIMEO (unsigned) (1 << 29)
|
||||
#define EVENT_SO_SNDTIMEO (unsigned) (1 << 30)
|
||||
|
||||
event_proc *r_proc;
|
||||
event_proc *w_proc;
|
||||
#ifdef HAS_POLL
|
||||
@ -230,7 +233,7 @@ struct FILE_EVENT {
|
||||
int flags;
|
||||
} recv_ctx;
|
||||
|
||||
#if defined(IO_URING_HAS_RECVFROM)
|
||||
# if defined(IO_URING_HAS_RECVFROM)
|
||||
struct {
|
||||
char *buf;
|
||||
unsigned len;
|
||||
@ -238,7 +241,7 @@ struct FILE_EVENT {
|
||||
struct sockaddr *src_addr;
|
||||
socklen_t *addrlen;
|
||||
} recvfrom_ctx;
|
||||
#endif
|
||||
# endif
|
||||
|
||||
struct {
|
||||
struct msghdr *msg;
|
||||
@ -265,7 +268,7 @@ struct FILE_EVENT {
|
||||
int flags;
|
||||
} send_ctx;
|
||||
|
||||
#if defined(IO_URING_HAS_SENDTO)
|
||||
# if defined(IO_URING_HAS_SENDTO)
|
||||
struct {
|
||||
const void *buf;
|
||||
unsigned len;
|
||||
@ -273,7 +276,7 @@ struct FILE_EVENT {
|
||||
const struct sockaddr *dest_addr;
|
||||
socklen_t addrlen;
|
||||
} sendto_ctx;
|
||||
#endif
|
||||
# endif
|
||||
|
||||
struct {
|
||||
const struct msghdr *msg;
|
||||
@ -287,9 +290,9 @@ struct FILE_EVENT {
|
||||
socklen_t len;
|
||||
} peer;
|
||||
|
||||
#ifdef HAS_STATX
|
||||
# ifdef HAS_STATX
|
||||
struct statx *statxbuf;
|
||||
#endif
|
||||
# endif
|
||||
char *path;
|
||||
} var;
|
||||
|
||||
@ -297,11 +300,12 @@ struct FILE_EVENT {
|
||||
struct IO_URING_CTX writer_ctx;
|
||||
struct __kernel_timespec rts;
|
||||
struct __kernel_timespec wts;
|
||||
|
||||
#endif // HAS_IO_URING
|
||||
|
||||
int r_timeout;
|
||||
int w_timeout;
|
||||
|
||||
#endif
|
||||
|
||||
ACL_FIBER_SEM* mbox_wsem; // Used in sync_waiter_wakeup.c
|
||||
|
||||
#ifdef HAS_IOCP
|
||||
@ -322,7 +326,8 @@ struct FILE_EVENT {
|
||||
socklen_t len;
|
||||
} peer;
|
||||
} var;
|
||||
#endif
|
||||
#endif // HAS_IOCP
|
||||
|
||||
short refer;
|
||||
short busy;
|
||||
#define EVENT_BUSY_NONE (0)
|
||||
|
@ -190,7 +190,7 @@ static int event_uring_add_write(EVENT_URING *ep, FILE_EVENT *fe)
|
||||
|
||||
TRY_SUBMMIT(ep);
|
||||
} else if (fe->mask & EVENT_POLLOUT) {
|
||||
add_write_wait(ep, fe, fe->r_timeout);
|
||||
add_write_wait(ep, fe, fe->w_timeout);
|
||||
} else if (fe->mask & EVENT_CONNECT) {
|
||||
non_blocking(fe->fd, 1);
|
||||
struct io_uring_sqe *sqe = io_uring_get_sqe(&ep->ring);
|
||||
|
@ -472,7 +472,10 @@ static void fiber_signal(ACL_FIBER *fiber, int signum, int sync)
|
||||
|
||||
// Just only wakeup the suspended fiber.
|
||||
if (fiber->status == FIBER_STATUS_SUSPEND) {
|
||||
#if 0
|
||||
// The fiber will be detached at first in acl_fiber_ready.
|
||||
ring_detach(&fiber->me); // This is safety!
|
||||
#endif
|
||||
|
||||
acl_fiber_ready(fiber);
|
||||
|
||||
@ -526,10 +529,14 @@ void fiber_exit(int exit_code)
|
||||
void acl_fiber_ready(ACL_FIBER *fiber)
|
||||
{
|
||||
if (fiber->status != FIBER_STATUS_EXITING) {
|
||||
#if 0
|
||||
if (fiber->status == FIBER_STATUS_READY) {
|
||||
ring_detach(&fiber->me);
|
||||
}
|
||||
|
||||
#else
|
||||
// Detache the other binding before such as timer binding.
|
||||
ring_detach(&fiber->me);
|
||||
#endif
|
||||
fiber->status = FIBER_STATUS_READY;
|
||||
assert(__thread_fiber);
|
||||
ring_prepend(&__thread_fiber->ready, &fiber->me);
|
||||
|
@ -129,7 +129,7 @@ int fiber_wait_read(FILE_EVENT *fe);
|
||||
int fiber_wait_write(FILE_EVENT *fe);
|
||||
|
||||
EVENT *fiber_io_event(void);
|
||||
void fiber_timer_add(ACL_FIBER *fiber, unsigned milliseconds);
|
||||
void fiber_timer_add(ACL_FIBER *fiber, size_t milliseconds);
|
||||
int fiber_timer_del(ACL_FIBER *fiber);
|
||||
|
||||
FILE_EVENT *fiber_file_open(socket_t fd);
|
||||
|
@ -172,14 +172,14 @@ static long long fiber_io_stamp(void)
|
||||
return event_get_stamp(ev);
|
||||
}
|
||||
|
||||
void fiber_timer_add(ACL_FIBER *fiber, unsigned milliseconds)
|
||||
void fiber_timer_add(ACL_FIBER *fiber, size_t milliseconds)
|
||||
{
|
||||
EVENT *ev = fiber_io_event();
|
||||
long long now = event_get_stamp(ev);
|
||||
TIMER_CACHE_NODE *timer;
|
||||
|
||||
fiber->when = now + milliseconds;
|
||||
ring_detach(&fiber->me);
|
||||
ring_detach(&fiber->me); // Detch the previous binding.
|
||||
timer_cache_add(__thread_fiber->ev_timer, fiber->when, &fiber->me);
|
||||
|
||||
/* Compute the event waiting interval according the timers' head */
|
||||
@ -223,6 +223,11 @@ static void wakeup_timers(TIMER_CACHE *timers, long long now)
|
||||
// Set the flag that the fiber wakeuped for the
|
||||
// timer's arriving.
|
||||
fb->flag |= FIBER_F_TIMER;
|
||||
|
||||
// The fb->me was be appended in fiber_timer_add, and
|
||||
// we detatch fb->me from timer node and append it to
|
||||
// the ready ring in acl_fiber_ready.
|
||||
ring_detach(&fb->me);
|
||||
acl_fiber_ready(fb);
|
||||
}
|
||||
|
||||
@ -318,7 +323,7 @@ void fiber_io_clear(void)
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int acl_fiber_delay(unsigned int milliseconds)
|
||||
size_t acl_fiber_delay(size_t milliseconds)
|
||||
{
|
||||
long long now;
|
||||
ACL_FIBER *fiber;
|
||||
@ -343,13 +348,20 @@ unsigned int acl_fiber_delay(unsigned int milliseconds)
|
||||
// Clear the flag been set in wakeup_timers.
|
||||
fiber->flag &= ~FIBER_F_TIMER;
|
||||
|
||||
if (acl_fiber_killed(fiber)) {
|
||||
// If been killed, the fiber must has been detatched from the
|
||||
// timer node in acl_fiber_signal(); We call fiber_timer_del
|
||||
// here in order to try to free the timer node.
|
||||
fiber_timer_del(fiber);
|
||||
}
|
||||
|
||||
ev = fiber_io_event();
|
||||
now = event_get_stamp(ev);
|
||||
if (now <= fiber->when) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (unsigned int) (now - fiber->when);
|
||||
return (size_t) (now - fiber->when);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
@ -370,7 +382,7 @@ static void fiber_timer_callback(ACL_FIBER *fiber, void *ctx)
|
||||
break;
|
||||
}
|
||||
|
||||
acl_fiber_delay((unsigned int) left);
|
||||
acl_fiber_delay((size_t) left);
|
||||
|
||||
now = fiber_io_stamp();
|
||||
if (fiber->when <= now) {
|
||||
@ -383,7 +395,7 @@ static void fiber_timer_callback(ACL_FIBER *fiber, void *ctx)
|
||||
fiber_exit(0);
|
||||
}
|
||||
|
||||
ACL_FIBER *acl_fiber_create_timer(unsigned int milliseconds, size_t size,
|
||||
ACL_FIBER *acl_fiber_create_timer(size_t milliseconds, size_t size,
|
||||
void (*fn)(ACL_FIBER *, void *), void *ctx)
|
||||
{
|
||||
long long when;
|
||||
@ -403,7 +415,7 @@ ACL_FIBER *acl_fiber_create_timer(unsigned int milliseconds, size_t size,
|
||||
return fiber;
|
||||
}
|
||||
|
||||
int acl_fiber_reset_timer(ACL_FIBER *fiber, unsigned int milliseconds)
|
||||
int acl_fiber_reset_timer(ACL_FIBER *fiber, size_t milliseconds)
|
||||
{
|
||||
// The previous timer with the fiber must be removed first.
|
||||
int ret = fiber_timer_del(fiber);
|
||||
@ -417,7 +429,7 @@ int acl_fiber_reset_timer(ACL_FIBER *fiber, unsigned int milliseconds)
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int acl_fiber_sleep(unsigned int seconds)
|
||||
size_t acl_fiber_sleep(size_t seconds)
|
||||
{
|
||||
return acl_fiber_delay(seconds * 1000) / 1000;
|
||||
}
|
||||
@ -473,6 +485,10 @@ int fiber_wait_read(FILE_EVENT *fe)
|
||||
WAITER_INC(__thread_fiber->event);
|
||||
}
|
||||
|
||||
if (fe->mask & EVENT_SO_RCVTIMEO && fe->r_timeout > 0) {
|
||||
fiber_timer_add(curr, fe->r_timeout);
|
||||
}
|
||||
|
||||
acl_fiber_switch();
|
||||
|
||||
fe->fiber_r->wstatus &= ~FIBER_WAIT_READ;
|
||||
@ -483,9 +499,23 @@ int fiber_wait_read(FILE_EVENT *fe)
|
||||
}
|
||||
|
||||
if (acl_fiber_canceled(curr)) {
|
||||
// If the IO has been canceled, we should try to remove the
|
||||
// IO read event, because the wakeup process wasn't from
|
||||
// read_callback normally.
|
||||
event_del_read(__thread_fiber->event, fe);
|
||||
acl_fiber_set_error(curr->errnum);
|
||||
return -1;
|
||||
} else if (curr->flag & FIBER_F_TIMER) {
|
||||
// If the IO reading timeout set in setsockopt.
|
||||
// Clear FIBER_F_TIMER flag been set in wakeup_timers.
|
||||
curr->flag &= ~FIBER_F_TIMER;
|
||||
event_del_read(__thread_fiber->event, fe);
|
||||
|
||||
acl_fiber_set_errno(curr, FIBER_EAGAIN);
|
||||
acl_fiber_set_error(FIBER_EAGAIN);
|
||||
return -1;
|
||||
}
|
||||
// else: the IO read event should has been removed in read_callback.
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -530,6 +560,11 @@ int fiber_wait_write(FILE_EVENT *fe)
|
||||
WAITER_INC(__thread_fiber->event);
|
||||
}
|
||||
|
||||
|
||||
if (fe->mask & EVENT_SO_SNDTIMEO && fe->w_timeout > 0) {
|
||||
fiber_timer_add(curr, fe->w_timeout);
|
||||
}
|
||||
|
||||
acl_fiber_switch();
|
||||
|
||||
fe->fiber_w->wstatus &= ~FIBER_WAIT_WRITE;
|
||||
@ -540,8 +575,16 @@ int fiber_wait_write(FILE_EVENT *fe)
|
||||
}
|
||||
|
||||
if (acl_fiber_canceled(curr)) {
|
||||
event_del_write(__thread_fiber->event, fe);
|
||||
acl_fiber_set_error(curr->errnum);
|
||||
return -1;
|
||||
} else if (curr->flag & FIBER_F_TIMER) {
|
||||
curr->flag &= ~FIBER_F_TIMER;
|
||||
event_del_write(__thread_fiber->event, fe);
|
||||
|
||||
acl_fiber_set_errno(curr, FIBER_EAGAIN);
|
||||
acl_fiber_set_error(FIBER_EAGAIN);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -31,9 +31,9 @@ void file_event_init(FILE_EVENT *fe, socket_t fd)
|
||||
memset(&fe->var, 0, sizeof(fe->var));
|
||||
memset(&fe->reader_ctx, 0, sizeof(fe->reader_ctx));
|
||||
memset(&fe->writer_ctx, 0, sizeof(fe->writer_ctx));
|
||||
#endif
|
||||
fe->r_timeout = -1;
|
||||
fe->w_timeout = -1;
|
||||
#endif
|
||||
|
||||
#ifdef HAS_IOCP
|
||||
fe->rbuf = NULL;
|
||||
|
@ -207,6 +207,7 @@ static void poll_event_clean(EVENT *ev, POLL_EVENT *pe)
|
||||
CLR_READWAIT(pfd->fe);
|
||||
#ifdef HAS_IO_URING
|
||||
pfd->fe->mask &= ~EVENT_POLLIN;
|
||||
pfd->fe->r_timeout = -1;
|
||||
#endif
|
||||
event_del_read(ev, pfd->fe);
|
||||
pfd->fe->fiber_r = NULL;
|
||||
@ -215,6 +216,7 @@ static void poll_event_clean(EVENT *ev, POLL_EVENT *pe)
|
||||
CLR_WRITEWAIT(pfd->fe);
|
||||
#ifdef HAS_IO_URING
|
||||
pfd->fe->mask &= ~EVENT_POLLOUT;
|
||||
pfd->fe->w_timeout = -1;
|
||||
#endif
|
||||
event_del_write(ev, pfd->fe);
|
||||
pfd->fe->fiber_w = NULL;
|
||||
|
@ -484,18 +484,20 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
return acl_fiber_connect(sockfd, addr, addrlen);
|
||||
}
|
||||
|
||||
#ifdef CREAT_TIMER_FIBER
|
||||
|
||||
typedef struct TIMEOUT_CTX {
|
||||
ACL_FIBER *fiber;
|
||||
int sockfd;
|
||||
unsigned id;
|
||||
} TIMEOUT_CTX;
|
||||
|
||||
static void fiber_timeout(ACL_FIBER *fiber UNUSED, void *ctx)
|
||||
static void read_timeout(ACL_FIBER *fiber UNUSED, void *ctx)
|
||||
{
|
||||
TIMEOUT_CTX *tc = (TIMEOUT_CTX*) ctx;
|
||||
FILE_EVENT *fe = fiber_file_get(tc->sockfd);
|
||||
|
||||
// we must check the fiber carefully here.
|
||||
// We must check the fiber carefully here.
|
||||
if (fe == NULL || tc->fiber != fe->fiber_r
|
||||
|| tc->fiber->fid != fe->fiber_r->fid) {
|
||||
|
||||
@ -503,10 +505,9 @@ static void fiber_timeout(ACL_FIBER *fiber UNUSED, void *ctx)
|
||||
return;
|
||||
}
|
||||
|
||||
// we can kill the fiber only if the fiber is waiting
|
||||
// We can kill the fiber only if the fiber is waiting
|
||||
// for readable ore writable of IO process.
|
||||
if (fe->fiber_r->wstatus & (FIBER_WAIT_READ | FIBER_WAIT_WRITE)) {
|
||||
|
||||
if (fe->fiber_r->wstatus & FIBER_WAIT_READ) {
|
||||
tc->fiber->errnum = FIBER_EAGAIN;
|
||||
acl_fiber_signal(tc->fiber, SIGINT);
|
||||
}
|
||||
@ -514,11 +515,41 @@ static void fiber_timeout(ACL_FIBER *fiber UNUSED, void *ctx)
|
||||
mem_free(ctx);
|
||||
}
|
||||
|
||||
static void send_timeout(ACL_FIBER *fiber UNUSED, void *ctx)
|
||||
{
|
||||
TIMEOUT_CTX *tc = (TIMEOUT_CTX*) ctx;
|
||||
FILE_EVENT *fe = fiber_file_get(tc->sockfd);
|
||||
|
||||
// We must check the fiber carefully here.
|
||||
if (fe == NULL || tc->fiber != fe->fiber_w
|
||||
|| tc->fiber->fid != fe->fiber_w->fid) {
|
||||
|
||||
mem_free(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
// We can kill the fiber only if the fiber is waiting
|
||||
// for readable ore writable of IO process.
|
||||
if (fe->fiber_w->wstatus & FIBER_WAIT_READ) {
|
||||
tc->fiber->errnum = FIBER_EAGAIN;
|
||||
acl_fiber_signal(tc->fiber, SIGINT);
|
||||
}
|
||||
|
||||
mem_free(ctx);
|
||||
}
|
||||
|
||||
#endif // CREAT_TIMER_FIBER
|
||||
|
||||
int setsockopt(int sockfd, int level, int optname,
|
||||
const void *optval, socklen_t optlen)
|
||||
{
|
||||
size_t val;
|
||||
#ifdef CREAT_TIMER_FIBER
|
||||
TIMEOUT_CTX *ctx;
|
||||
#else
|
||||
FILE_EVENT *fe;
|
||||
ACL_FIBER *curr;
|
||||
#endif
|
||||
const struct timeval *tm;
|
||||
|
||||
if (sys_setsockopt == NULL) {
|
||||
@ -561,11 +592,54 @@ int setsockopt(int sockfd, int level, int optname,
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef CREAT_TIMER_FIBER
|
||||
ctx = (TIMEOUT_CTX*) mem_malloc(sizeof(TIMEOUT_CTX));
|
||||
ctx->fiber = acl_fiber_running();
|
||||
ctx->sockfd = sockfd;
|
||||
acl_fiber_create_timer((unsigned) val * 1000, 64000, fiber_timeout, ctx);
|
||||
|
||||
if (optname == SO_RCVTIMEO) {
|
||||
val *= 1000;
|
||||
acl_fiber_create_timer(val, 4096, read_timeout, ctx);
|
||||
return 0;
|
||||
} else if (optname == SO_SNDTIMEO) {
|
||||
val *= 1000;
|
||||
acl_fiber_create_timer(val, 4096, send_timeout, ctx);
|
||||
return 0;
|
||||
} else {
|
||||
msg_error("Invalid optname=%d", optname);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
curr = acl_fiber_running();
|
||||
fe = fiber_file_open(sockfd);
|
||||
|
||||
if (val <= 0) {
|
||||
fiber_timer_del(curr);
|
||||
if (optname == SO_RCVTIMEO) {
|
||||
fe->mask &= ~EVENT_SO_RCVTIMEO;
|
||||
fe->r_timeout = -1;
|
||||
} else if (optname == SO_SNDTIMEO) {
|
||||
fe->mask &= ~EVENT_SO_SNDTIMEO;
|
||||
fe->w_timeout = -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
val *= 1000;
|
||||
if (optname == SO_RCVTIMEO) {
|
||||
fe->mask |= EVENT_SO_RCVTIMEO;
|
||||
fe->r_timeout = val;
|
||||
} else if (optname == SO_SNDTIMEO) {
|
||||
fe->mask |= EVENT_SO_SNDTIMEO;
|
||||
fe->w_timeout = val;
|
||||
} else {
|
||||
msg_fatal("%s: Invalid optname=%d", __FUNCTION__, optname);
|
||||
}
|
||||
|
||||
// We just set the flags here, and the timer will be add in
|
||||
// fiber_wait_read or fiber_wait_write.
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -73,10 +73,12 @@ static void wakeup_waiter(SYNC_TIMER *timer UNUSED, SYNC_OBJ *obj)
|
||||
|
||||
if (obj->delay < 0) {
|
||||
// No timer has been set if delay < 0,
|
||||
ring_detach(&obj->fb->me); // Safety detatch me from others.
|
||||
acl_fiber_ready(obj->fb);
|
||||
} else if (fiber_timer_del(obj->fb) == 1) {
|
||||
// Wakeup the waiting fiber before the timer arrives,
|
||||
// just remove it from the timer.
|
||||
ring_detach(&obj->fb->me); // Safety detatch me from others.
|
||||
acl_fiber_ready(obj->fb);
|
||||
}
|
||||
// else: The fiber has been awakened by the timer.
|
||||
|
@ -202,10 +202,10 @@ public:
|
||||
|
||||
/**
|
||||
* 使当前运行的协程休眠指定毫秒数
|
||||
* @param milliseconds {unsigned int} 指定要休眠的毫秒数
|
||||
* @return {unsigned int} 本协程休眠后再次被唤醒后剩余的毫秒数
|
||||
* @param milliseconds {size_t} 指定要休眠的毫秒数
|
||||
* @return {size_t} 本协程休眠后再次被唤醒后剩余的毫秒数
|
||||
*/
|
||||
static unsigned int delay(unsigned int milliseconds);
|
||||
static size_t delay(size_t milliseconds);
|
||||
|
||||
/**
|
||||
* 获得处于存活状态的协程数量
|
||||
|
@ -102,7 +102,7 @@ void fiber::ready(fiber& f)
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int fiber::delay(unsigned int milliseconds)
|
||||
size_t fiber::delay(size_t milliseconds)
|
||||
{
|
||||
return acl_fiber_delay(milliseconds);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include "fiber/libfiber.h"
|
||||
|
||||
static int __stack_size = 320000;
|
||||
static int __rw_timeout = 0;
|
||||
static int __rw_timeout = 5;
|
||||
static int __echo_data = 0;
|
||||
static int __setsockopt_timeout = 0;
|
||||
|
||||
@ -32,6 +32,8 @@ static void echo_client(ACL_FIBER *fiber acl_unused, void *ctx)
|
||||
#endif
|
||||
printf("%s: setsockopt error: %s\r\n",
|
||||
__FUNCTION__, acl_last_serror());
|
||||
} else {
|
||||
printf("%s: setsockopt ok for readtimeout\r\n", __FUNCTION__);
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,6 +80,7 @@ static void fiber_accept(ACL_FIBER *fiber acl_unused, void *ctx)
|
||||
break;
|
||||
}
|
||||
|
||||
#if 1
|
||||
ret = acl_vstream_gets(cstream, buf, sizeof(buf) - 1);
|
||||
if (ret == ACL_VSTREAM_EOF) {
|
||||
printf("get first line error\r\n");
|
||||
@ -88,6 +91,7 @@ static void fiber_accept(ACL_FIBER *fiber acl_unused, void *ctx)
|
||||
acl_vstream_close(cstream);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
//printf("accept one, fd: %d\r\n", ACL_VSTREAM_SOCK(cstream));
|
||||
acl_fiber_create(echo_client, cstream, __stack_size);
|
||||
@ -183,6 +187,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
__listen_fiber = acl_fiber_create(fiber_accept, sstream, 327680);
|
||||
|
||||
if (0)
|
||||
acl_fiber_create(fiber_sleep, NULL, 327680);
|
||||
|
||||
printf("call fiber_schedule\r\n");
|
||||
|
@ -54,7 +54,7 @@ static void http_client(ACL_FIBER *fiber, const char* addr)
|
||||
}
|
||||
|
||||
if (i < 1) {
|
||||
if (body.size() < 100) {
|
||||
if (body.size() < 1000) {
|
||||
printf(">>>fiber-%d: body: %s\r\n",
|
||||
acl_fiber_id(fiber), body.c_str());
|
||||
} else {
|
||||
|
@ -1,3 +1,4 @@
|
||||
include ../Makefile_cpp.in
|
||||
#EXTLIBS = -L../../../lib/linux64 -lpolarssl
|
||||
#EXTLIBS = -L../../../test/libmm -lmm -Wl,-rpath,../../../test/libmm
|
||||
PROG = httpd
|
||||
|
@ -9,6 +9,7 @@ static int __rw_timeout = 10;
|
||||
static acl::string __ssl_crt("./ssl_crt.pem");
|
||||
static acl::string __ssl_key("./ssl_key.pem");
|
||||
static acl::sslbase_conf* __ssl_conf;
|
||||
static bool __shared_stack = false;
|
||||
|
||||
static void http_server(ACL_FIBER *, void *ctx)
|
||||
{
|
||||
@ -80,6 +81,14 @@ static void fiber_accept(ACL_FIBER *, void *ctx)
|
||||
{
|
||||
const char* addr = (const char* ) ctx;
|
||||
acl::server_socket server;
|
||||
ACL_FIBER_ATTR attr;
|
||||
|
||||
acl_fiber_attr_init(&attr);
|
||||
|
||||
if (__shared_stack) {
|
||||
acl_fiber_attr_setstacksize(&attr, 4096);
|
||||
acl_fiber_attr_setsharestack(&attr, 1);
|
||||
}
|
||||
|
||||
ssl_init(*__ssl_conf, __ssl_crt, __ssl_key);
|
||||
|
||||
@ -99,8 +108,12 @@ static void fiber_accept(ACL_FIBER *, void *ctx)
|
||||
|
||||
client->set_rw_timeout(__rw_timeout);
|
||||
printf("accept one: %d\r\n", client->sock_handle());
|
||||
if (__shared_stack) {
|
||||
acl_fiber_create2(&attr, http_server, client);
|
||||
} else {
|
||||
acl_fiber_create(http_server, client, STACK_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
exit (0);
|
||||
}
|
||||
@ -108,6 +121,7 @@ static void fiber_accept(ACL_FIBER *, void *ctx)
|
||||
static void usage(const char* procname)
|
||||
{
|
||||
printf("usage: %s -h [help]\r\n"
|
||||
" -S [use shared stack]\r\n"
|
||||
" -l ssl_lib_path\r\n"
|
||||
" -s listen_addr\r\n"
|
||||
" -r rw_timeout\r\n"
|
||||
@ -128,7 +142,7 @@ int main(int argc, char *argv[])
|
||||
acl::acl_cpp_init();
|
||||
acl::log::stdout_open(true);
|
||||
|
||||
while ((ch = getopt(argc, argv, "hs:r:c:k:l:")) > 0) {
|
||||
while ((ch = getopt(argc, argv, "hs:r:c:k:l:S")) > 0) {
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
@ -148,6 +162,9 @@ int main(int argc, char *argv[])
|
||||
case 'l':
|
||||
libpath = optarg;
|
||||
break;
|
||||
case 'S':
|
||||
__shared_stack =true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
2
lib_fiber/samples/setsockopt/Makefile
Normal file
2
lib_fiber/samples/setsockopt/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
include ../Makefile.in
|
||||
PROG = server
|
73
lib_fiber/samples/setsockopt/echo_client.c
Normal file
73
lib_fiber/samples/setsockopt/echo_client.c
Normal file
@ -0,0 +1,73 @@
|
||||
#include "stdafx.h"
|
||||
#include "echo_client.h"
|
||||
|
||||
static void set_timeout(ACL_VSTREAM *cstream, int rw_timeout)
|
||||
{
|
||||
#if defined(__APPLE__) || defined(_WIN32) || defined(_WIN64)
|
||||
if (setsockopt(ACL_VSTREAM_SOCK(cstream), SOL_SOCKET,
|
||||
SO_RCVTIMEO, &rw_timeout, sizeof(rw_timeout)) < 0) {
|
||||
#else
|
||||
struct timeval tm;
|
||||
tm.tv_sec = rw_timeout;
|
||||
tm.tv_usec = 0;
|
||||
if (setsockopt(ACL_VSTREAM_SOCK(cstream), SOL_SOCKET,
|
||||
SO_RCVTIMEO, &tm, sizeof(tm)) < 0) {
|
||||
#endif
|
||||
printf("%s: setsockopt error: %s\r\n",
|
||||
__FUNCTION__, acl_last_serror());
|
||||
}
|
||||
}
|
||||
|
||||
void echo_client(ACL_VSTREAM *cstream, int rw_timeout, int echo_data)
|
||||
{
|
||||
char buf[8192];
|
||||
int ret, count = 0, ecnt = 0;
|
||||
|
||||
#define SOCK ACL_VSTREAM_SOCK
|
||||
|
||||
if (rw_timeout > 0) {
|
||||
set_timeout(cstream, rw_timeout);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
time_t begin = time(NULL);
|
||||
ret = acl_vstream_read(cstream, buf, sizeof(buf) - 1);
|
||||
time_t end = time(NULL);
|
||||
|
||||
if (ret == ACL_VSTREAM_EOF) {
|
||||
if (errno == EAGAIN && ++ecnt <= 3) {
|
||||
printf("EAGAIN, try again, fd: %d, cost=%ld\r\n",
|
||||
SOCK(cstream), end - begin);
|
||||
continue;
|
||||
}
|
||||
printf("read error: %s, fd: %d, count: %d, timeout count=%d\r\n",
|
||||
acl_last_serror(), SOCK(cstream), count, ecnt);
|
||||
break;
|
||||
}
|
||||
buf[ret] = 0;
|
||||
//printf("gets line: %s", buf);
|
||||
|
||||
#if 1
|
||||
if (rw_timeout >= 5) {
|
||||
rw_timeout = 2;
|
||||
set_timeout(cstream, rw_timeout);
|
||||
printf(">>reset read timeout to %d\r\n", rw_timeout);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!echo_data) {
|
||||
count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (acl_vstream_writen(cstream, buf, ret) == ACL_VSTREAM_EOF) {
|
||||
printf("write error, fd: %d\r\n", SOCK(cstream));
|
||||
break;
|
||||
}
|
||||
|
||||
count++;
|
||||
//printf("count=%d\n", count);
|
||||
}
|
||||
|
||||
acl_vstream_close(cstream);
|
||||
}
|
6
lib_fiber/samples/setsockopt/echo_client.h
Normal file
6
lib_fiber/samples/setsockopt/echo_client.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef __ECHO_CLIENT_INCLUDE_H__
|
||||
#define __ECHO_CLIENT_INCLUDE_H__
|
||||
|
||||
void echo_client(ACL_VSTREAM *cstream, int rw_timeout, int echo_data);
|
||||
|
||||
#endif
|
128
lib_fiber/samples/setsockopt/main.c
Normal file
128
lib_fiber/samples/setsockopt/main.c
Normal file
@ -0,0 +1,128 @@
|
||||
#include "stdafx.h"
|
||||
#include "echo_client.h"
|
||||
|
||||
static int __stack_size = 320000;
|
||||
static int __rw_timeout = 5;
|
||||
static int __echo_data = 0;
|
||||
|
||||
static void *thread_client(void *ctx)
|
||||
{
|
||||
ACL_VSTREAM *cstream = (ACL_VSTREAM*) ctx;
|
||||
echo_client(cstream, __rw_timeout, __echo_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void thread_server_run(ACL_VSTREAM *sstream)
|
||||
{
|
||||
acl_pthread_attr_t attr;
|
||||
|
||||
acl_pthread_attr_init(&attr);
|
||||
acl_pthread_attr_setdetachstate(&attr, 1);
|
||||
|
||||
while (1) {
|
||||
ACL_VSTREAM *cstream = acl_vstream_accept(sstream, NULL, 0);
|
||||
if (sstream == NULL) {
|
||||
printf("acl_vstream_accept error %s\r\n",
|
||||
acl_last_serror());
|
||||
break;
|
||||
}
|
||||
|
||||
acl_pthread_t tid;
|
||||
acl_pthread_create(&tid, &attr, thread_client, cstream);
|
||||
}
|
||||
}
|
||||
|
||||
static void fiber_client(ACL_FIBER *fiber acl_unused, void *ctx)
|
||||
{
|
||||
ACL_VSTREAM *cstream = (ACL_VSTREAM *) ctx;
|
||||
echo_client(cstream, __rw_timeout, __echo_data);
|
||||
}
|
||||
|
||||
static void fiber_accept(ACL_FIBER *fiber acl_unused, void *ctx)
|
||||
{
|
||||
ACL_VSTREAM *sstream = (ACL_VSTREAM *) ctx;
|
||||
|
||||
for (;;) {
|
||||
ACL_VSTREAM *cstream = acl_vstream_accept(sstream, NULL, 0);
|
||||
if (cstream == NULL) {
|
||||
printf("acl_vstream_accept error %s\r\n",
|
||||
acl_last_serror());
|
||||
break;
|
||||
}
|
||||
|
||||
acl_fiber_create(fiber_client, cstream, __stack_size);
|
||||
}
|
||||
|
||||
acl_vstream_close(sstream);
|
||||
}
|
||||
|
||||
static void fiber_server_run(ACL_VSTREAM *sstream)
|
||||
{
|
||||
acl_fiber_create(fiber_accept, sstream, 327680);
|
||||
|
||||
printf("call fiber_schedule\r\n");
|
||||
acl_fiber_schedule();
|
||||
}
|
||||
|
||||
static void usage(const char *procname)
|
||||
{
|
||||
printf("usage: %s -h [help]\r\n"
|
||||
" -s listen_addr\r\n"
|
||||
" -r rw_timeout [default: 5]\r\n"
|
||||
" -z stack_size\r\n"
|
||||
" -T [if running in thread mode, default: no]\r\n"
|
||||
" -W [if echo data, default: no]\r\n", procname);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char addr[64];
|
||||
ACL_VSTREAM *sstream;
|
||||
int ch, qlen = 128, thread_mode = 0;
|
||||
|
||||
acl_msg_stdout_enable(1);
|
||||
acl_fiber_msg_stdout_enable(1);
|
||||
|
||||
snprintf(addr, sizeof(addr), "%s", "127.0.0.1:9002");
|
||||
|
||||
while ((ch = getopt(argc, argv, "hs:r:Wz:T")) > 0) {
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
case 's':
|
||||
snprintf(addr, sizeof(addr), "%s", optarg);
|
||||
break;
|
||||
case 'r':
|
||||
__rw_timeout = atoi(optarg);
|
||||
break;
|
||||
case 'W':
|
||||
__echo_data = 1;
|
||||
break;
|
||||
case 'z':
|
||||
__stack_size = atoi(optarg);
|
||||
break;
|
||||
case 'T':
|
||||
thread_mode = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sstream = acl_vstream_listen(addr, qlen);
|
||||
if (sstream == NULL) {
|
||||
printf("acl_vstream_listen error %s\r\n", acl_last_serror());
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("listen %s ok\r\n", addr);
|
||||
|
||||
if (thread_mode) {
|
||||
thread_server_run(sstream);
|
||||
} else {
|
||||
fiber_server_run(sstream);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
11
lib_fiber/samples/setsockopt/stdafx.h
Normal file
11
lib_fiber/samples/setsockopt/stdafx.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef __STDAFX_INCLUDE_H__
|
||||
#define __STDAFX_INCLUDE_H__
|
||||
|
||||
#include "lib_acl.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "fiber/libfiber.h"
|
||||
|
||||
#endif
|
@ -5,7 +5,7 @@
|
||||
|
||||
#define STACK_SIZE 128000
|
||||
|
||||
static int __rw_timeout = 0;
|
||||
static int __rw_timeout = -1;
|
||||
static int __conn_timeout = 0;
|
||||
static int __max_fibers = 100;
|
||||
static int __left_fibers = 100;
|
||||
@ -23,7 +23,7 @@ static bool ssl_init(acl::socket_stream& conn)
|
||||
{
|
||||
assert(__ssl_conf);
|
||||
|
||||
acl::sslbase_io* ssl = __ssl_conf->open(false, false);
|
||||
acl::sslbase_io* ssl = __ssl_conf->create(false);
|
||||
|
||||
if (conn.setup_hook(ssl) == ssl) {
|
||||
printf("setup_hook error\r\n");
|
||||
@ -32,7 +32,7 @@ static bool ssl_init(acl::socket_stream& conn)
|
||||
}
|
||||
|
||||
if (!__check_ssl) {
|
||||
printf("ssl handshake_ok\r\n");
|
||||
printf("ssl handshake ok, no check ssl\r\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ static bool ssl_init(acl::socket_stream& conn)
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("ssl handshake_ok\r\n");
|
||||
printf("ssl handshake ok\r\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -78,12 +78,18 @@ static void run(const char* addr)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!conn.gets(buf, false)) {
|
||||
if (!conn.gets(buf, true)) {
|
||||
printf("gets error: %s\r\n", acl::last_serror());
|
||||
break;
|
||||
}
|
||||
|
||||
if (i < 5) {
|
||||
printf(">>>gets: %s\r\n", buf.c_str());
|
||||
}
|
||||
|
||||
buf.clear();
|
||||
__total_count++;
|
||||
//sleep(8);
|
||||
}
|
||||
|
||||
printf("close one connection: %d, %s\r\n",
|
||||
@ -202,11 +208,26 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
} else if (libpath.find("polarssl") != NULL) {
|
||||
acl::polarssl_conf::set_libpath(libpath);
|
||||
if (!acl::polarssl_conf::load()) {
|
||||
if (acl::polarssl_conf::load()) {
|
||||
__ssl_conf = new acl::polarssl_conf;
|
||||
} else {
|
||||
printf("load %s error\r\n", libpath.c_str());
|
||||
}
|
||||
} else if (libpath.find("crypto") != NULL) {
|
||||
const std::vector<acl::string>& libs = libpath.split2("; \t");
|
||||
if (libs.size() != 2) {
|
||||
printf("invalid libpath=%s\r\n", libpath.c_str());
|
||||
return 1;
|
||||
}
|
||||
acl::openssl_conf::set_libpath(libs[0], libs[1]);
|
||||
if (acl::openssl_conf::load()) {
|
||||
__ssl_conf = new acl::openssl_conf;
|
||||
} else {
|
||||
printf("load %s error\r\n", libpath.c_str());
|
||||
}
|
||||
} else {
|
||||
printf("invalid libpath=%s\r\n", libpath.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
gettimeofday(&__begin, NULL);
|
||||
|
@ -4,5 +4,5 @@ os=$(echo `uname -s`)
|
||||
if [ $os == "Darwin" ]; then
|
||||
./client -l "../libmbedcrypto.dylib;../libmbedx509.dylib;../libmbedtls.dylib" -c 200 -n 10000
|
||||
else
|
||||
./client -l "../libmbedcrypto.so;../libmbedx509.so;../libmbedtls.so" -c 200 -n 10000
|
||||
./client -l "/usr/local/lib64/libcrypto.so; /usr/local/lib64/libssl.so" -c 200 -n 10000
|
||||
fi
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#define STACK_SIZE 128000
|
||||
|
||||
static int __rw_timeout = 0;
|
||||
static int __rw_timeout = 5;
|
||||
static acl::string __ssl_crt("./ssl_crt.pem");
|
||||
static acl::string __ssl_key("./ssl_key.pem");
|
||||
static acl::sslbase_conf *__ssl_conf = NULL;
|
||||
@ -44,15 +44,29 @@ static void echo_fiber(ACL_FIBER *, void *ctx)
|
||||
|
||||
printf("ssl handshake_ok\r\n");
|
||||
|
||||
acl::string buf;
|
||||
char buf[2048];
|
||||
size_t n = 0, eagain = 0;
|
||||
|
||||
while (true) {
|
||||
if (!conn->gets(buf, false)) {
|
||||
printf("gets error: %s\r\n", acl::last_serror());
|
||||
int ret = conn->read(buf, sizeof(buf) - 1, false);
|
||||
if (ret < 0) {
|
||||
if (errno != EAGAIN) {
|
||||
printf(">>>read error: %s\r\n", acl::last_serror());
|
||||
break;
|
||||
}
|
||||
|
||||
if (conn->write(buf) == -1) {
|
||||
printf(">>>read timeout %zd times\r\n", ++eagain);
|
||||
if (eagain >= 3) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
buf[ret] = 0;
|
||||
|
||||
if (n++ < 5) {
|
||||
printf(">>>read cnt=%d, data=%s\r\n", ret, buf);
|
||||
}
|
||||
|
||||
if (conn->write(buf, ret) == -1) {
|
||||
printf("write error: %s\r\n", acl::last_serror());
|
||||
break;
|
||||
}
|
||||
@ -63,15 +77,31 @@ static void echo_fiber(ACL_FIBER *, void *ctx)
|
||||
delete conn;
|
||||
}
|
||||
|
||||
static acl::sslbase_conf* ssl_init(bool use_mbedtls, const acl::string& crt,
|
||||
enum ssl_type_t {
|
||||
ssl_type_openssl,
|
||||
ssl_type_mbedtls,
|
||||
ssl_type_polarssl,
|
||||
};
|
||||
|
||||
static acl::sslbase_conf* ssl_init(ssl_type_t type, const acl::string& crt,
|
||||
const acl::string& key)
|
||||
{
|
||||
acl::sslbase_conf* conf;
|
||||
if (use_mbedtls) {
|
||||
switch (type) {
|
||||
case ssl_type_openssl:
|
||||
conf = new acl::openssl_conf(true);
|
||||
break;
|
||||
case ssl_type_mbedtls:
|
||||
conf = new acl::mbedtls_conf(true);
|
||||
} else {
|
||||
break;
|
||||
case ssl_type_polarssl:
|
||||
conf = new acl::polarssl_conf;
|
||||
break;
|
||||
default:
|
||||
printf("Unknown ssl type=%d\r\n", (int) type);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
conf->enable_cache(1);
|
||||
|
||||
if (!conf->add_cert(crt)) {
|
||||
@ -180,14 +210,27 @@ int main(int argc, char *argv[])
|
||||
printf("load %s error\r\n", libpath.c_str());
|
||||
return 1;
|
||||
}
|
||||
__ssl_conf = ssl_init(true, __ssl_crt, __ssl_key);
|
||||
__ssl_conf = ssl_init(ssl_type_mbedtls, __ssl_crt, __ssl_key);
|
||||
} else if (libpath.find("polarssl") != NULL) {
|
||||
acl::polarssl_conf::set_libpath(libpath);
|
||||
if (!acl::polarssl_conf::load()) {
|
||||
printf("load %s error\r\n", libpath.c_str());
|
||||
return 1;
|
||||
}
|
||||
__ssl_conf = ssl_init(false, __ssl_crt, __ssl_key);
|
||||
__ssl_conf = ssl_init(ssl_type_polarssl, __ssl_crt, __ssl_key);
|
||||
} else if (libpath.find("crypto") != NULL) {
|
||||
const std::vector<acl::string>& libs = libpath.split2("; \t");
|
||||
if (libs.size() != 2) {
|
||||
printf("invalid libpath=%s\r\n", libpath.c_str());
|
||||
return 1;
|
||||
}
|
||||
acl::openssl_conf::set_libpath(libs[0], libs[1]);
|
||||
if (!acl::openssl_conf::load()) {
|
||||
printf("load %s error\r\n", libpath.c_str());
|
||||
return 1;
|
||||
}
|
||||
__ssl_conf = ssl_init(ssl_type_openssl, __ssl_crt, __ssl_key);
|
||||
((acl::openssl_conf*) __ssl_conf)->use_sockopt_timeout(true);
|
||||
} else {
|
||||
printf("invalid libpath=%s\r\n", libpath.c_str());
|
||||
return 1;
|
||||
|
@ -4,5 +4,5 @@ os=$(echo `uname -s`)
|
||||
if [ $os == "Darwin" ]; then
|
||||
./server -l "../libmbedcrypto.dylib;../libmbedx509.dylib;../libmbedtls.dylib" -c ./ssl_crt.pem -k ./ssl_key.pem
|
||||
else
|
||||
./server -l "../libmbedcrypto.so;../libmbedx509.so;../libmbedtls.so" -c ./ssl_crt.pem -k ./ssl_key.pem
|
||||
./server -l "/usr/local/lib64/libcrypto.so; /usr/local/lib64/libssl.so" -c ./ssl_crt.pem -k ./ssl_key.pem
|
||||
fi
|
||||
|
@ -5,7 +5,8 @@
|
||||
#include <unistd.h>
|
||||
#include "fiber/libfiber.h"
|
||||
|
||||
static int __rw_timeout = 0;
|
||||
static int __rw_timeout = 5;
|
||||
static time_t __last = 0;
|
||||
|
||||
typedef struct {
|
||||
ACL_FIBER *fiber;
|
||||
@ -16,17 +17,20 @@ typedef struct {
|
||||
static void io_timer(ACL_FIBER *fiber, void *ctx)
|
||||
{
|
||||
FIBER_TIMER *ft = (FIBER_TIMER *) ctx;
|
||||
time_t now = time(NULL);
|
||||
|
||||
assert(fiber == ft->timer);
|
||||
|
||||
acl_fiber_set_errno(ft->fiber, FIBER_ETIME);
|
||||
acl_fiber_keep_errno(ft->fiber, 1);
|
||||
|
||||
printf("timer-%d wakeup, set fiber-%d, errno: %d, %d\r\n",
|
||||
acl_fiber_id(fiber), acl_fiber_id(ft->fiber),
|
||||
FIBER_ETIME, acl_fiber_errno(ft->fiber));
|
||||
printf("timer-%d wakeup: cost %ld seconds, set fiber-%d, errno: %d, %d, %s\r\n",
|
||||
acl_fiber_id(fiber), now - __last, acl_fiber_id(ft->fiber),
|
||||
FIBER_ETIME, acl_fiber_errno(ft->fiber), acl_fiber_last_serror());
|
||||
|
||||
acl_fiber_ready(ft->fiber);
|
||||
__last = now;
|
||||
//acl_fiber_ready(ft->fiber);
|
||||
acl_fiber_signal(ft->fiber, SIGINT);
|
||||
}
|
||||
|
||||
static void echo_client(ACL_FIBER *fiber, void *ctx)
|
||||
@ -45,8 +49,11 @@ static void echo_client(ACL_FIBER *fiber, void *ctx)
|
||||
#define SOCK ACL_VSTREAM_SOCK
|
||||
|
||||
while (1) {
|
||||
__last = time(NULL);
|
||||
|
||||
printf("begin read\n");
|
||||
ret = acl_vstream_gets(cstream, buf, sizeof(buf) - 1);
|
||||
printf("read return: %d\r\n", ret);
|
||||
|
||||
if (ret == ACL_VSTREAM_EOF) {
|
||||
printf("fiber-%d, gets error: %s, %d, %d, fd: %d, "
|
||||
@ -54,21 +61,20 @@ static void echo_client(ACL_FIBER *fiber, void *ctx)
|
||||
acl_last_serror(), errno, acl_fiber_errno(fiber),
|
||||
SOCK(cstream), count);
|
||||
|
||||
if (errno != FIBER_ETIME)
|
||||
if (errno != FIBER_ETIME) {
|
||||
printf("Some error happened!\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (++ntimeout > 2)
|
||||
{
|
||||
if (++ntimeout > 2) {
|
||||
printf("too many timeout: %d\r\n", ntimeout);
|
||||
break;
|
||||
}
|
||||
|
||||
printf("ntimeout: %d\r\n", ntimeout);
|
||||
ft->timer = acl_fiber_create_timer(__rw_timeout * 1000,
|
||||
320000, io_timer, ft);
|
||||
printf("Timeout count: %d\r\n", ntimeout);
|
||||
}
|
||||
|
||||
acl_fiber_reset_timer(ft->timer, __rw_timeout * 1000);
|
||||
if (ret > 0) {
|
||||
buf[ret] = 0;
|
||||
//printf("gets line: %s", buf);
|
||||
|
||||
@ -76,8 +82,13 @@ static void echo_client(ACL_FIBER *fiber, void *ctx)
|
||||
printf("write error, fd: %d\r\n", SOCK(cstream));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
count++;
|
||||
|
||||
acl_fiber_clear(ft->fiber);
|
||||
acl_fiber_reset_timer(ft->timer, __rw_timeout * 1000);
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
acl_myfree(ft);
|
||||
|
@ -23,6 +23,14 @@ if (CMAKE_SYSTEM_NAME MATCHES "Android")
|
||||
add_definitions("-fdata-sections -ffunction-sections")
|
||||
string(APPEND CMAKE_C_FLAGS "-Qunused-arguments")
|
||||
set(UNIX_OS true)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
add_definitions("-DANDROID")
|
||||
add_definitions("-O3 -flto")
|
||||
add_definitions("-Wno-unused-command-line-argument")
|
||||
add_definitions("-fdata-sections -ffunction-sections")
|
||||
add_definitions("-Wno-c99-extensions")
|
||||
string(APPEND CMAKE_C_FLAGS "-Qunused-arguments")
|
||||
set(UNIX_OS true)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
|
||||
add_definitions("-O2")
|
||||
set(UNIX_OS true)
|
||||
@ -112,6 +120,8 @@ endif()
|
||||
|
||||
if (CMAKE_SYSTEM_NAME MATCHES "Android")
|
||||
set(lib_output_path ${CMAKE_CURRENT_SOURCE_DIR}/../android/lib/${ANDROID_ABI})
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
set(lib_output_path ${CMAKE_CURRENT_SOURCE_DIR}/../harmony/lib/${OHOS_ARCH})
|
||||
else()
|
||||
set(lib_output_path ${PROJECT_BINARY_DIR}/../lib)
|
||||
endif()
|
||||
@ -137,6 +147,9 @@ if (NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND ACL_BUILD_SHARED
|
||||
set(sys_ldflags "-shared -flto -lz")
|
||||
endif()
|
||||
target_compile_options(protocol_static PRIVATE -fvisibility=hidden)
|
||||
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
|
||||
set(sys_ldflags "-shared -flto -lz")
|
||||
target_compile_options(protocol_static PRIVATE -fvisibility=hidden)
|
||||
elseif (${UNIX_OS})
|
||||
set(sys_ldflags "-shared -lz -lpthread -ldl")
|
||||
target_compile_options(protocol_static PRIVATE -fvisibility=hidden)
|
||||
|
Loading…
Reference in New Issue
Block a user