Optimize and test for harmonyOS.

This commit is contained in:
shuxin   zheng 2024-04-27 16:41:39 +08:00
parent 687250daf7
commit 98f98a9e64
4 changed files with 116 additions and 48 deletions

View File

@ -8,12 +8,17 @@ public:
http_thread(const char* url) : url_(url) {}
~http_thread() = default;
const acl::string& result() const {
const acl::string& get_body() const {
return body_;
}
const acl::string& get_head() const {
return head_;;
}
protected:
std::string url_;
acl::string head_;
acl::string body_;
// @override
@ -23,9 +28,7 @@ public:
}
bool url_get() {
const char* addr = "110.242.68.4|80";
addr = "www.baidu.com:80";
// addr = "110.242.68.3";
const char* addr = "www.baidu.com:80";
acl::http_request req(addr, 5);
req.request_header().set_url("/")
.set_host("www.baidu.com");
@ -44,9 +47,8 @@ public:
log_error("%s: get http body error!", __func__);
return false;
}
int f = AI_ADDRCONFIG;
struct sockaddr sa;
req.get_client()->sprint_header(head_);
log_info("%s: http response body: %s", __func__, body_.c_str());
return true;
}
@ -71,25 +73,23 @@ static napi_value HttpGet(napi_env env, napi_callback_info info)
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__);
const acl::string& body = http.get_body();
acl::string head = http.get_head();
if (body.empty() || head.empty()) {
log_info("%s: http reply body or head empty!", __func__);
return nullptr;
}
head.format_append("\r\nBody length: %zd\r\n", body.length());
napi_value result;
res = napi_create_string_utf8(env, body.c_str(), body.size(), &result);
res = napi_create_string_utf8(env, head.c_str(), head.size(), &result);
if (res != napi_ok) {
log_info("%s: napi_create_string_utf8 error=%d", __func__, res);
return nullptr;
@ -97,7 +97,6 @@ static napi_value HttpGet(napi_env env, napi_callback_info info)
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)

View File

@ -1,13 +1,13 @@
import hilog from '@ohos.hilog';
//import hilog from '@ohos.hilog';
//import worker from '@ohos.worker';
//import StyleConstant from '../common/constant/StyleConstant';
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;
const res: string = testNapi.HttpGet(url);
return res
}
@Concurrent
@ -15,27 +15,28 @@ function Add(num1, num2) : number {
return testNapi.Add(num1, num2);
}
async function asyncTask() : Promise<void> {
async function asyncTask() : Promise<string> {
try {
let task = new taskpool.Task(Add, 10, 100);
let num = await taskpool.execute(task);
console.info('------------------------------>Add Result: ', num);
console.info('-->Add Result: ', num);
} catch (e) {
console.error("----------------------------->Taskpool execute Add error: " + 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!');
let res = await taskpool.execute(task);
if (res != null) {
return String(res);
} else {
console.info("------------------------------>body null");
console.info("-->res null");
return "";
}
} catch (e) {
console.error("----------------------------->Taskpool execute HttpGet error: " + e);
console.error("-->Taskpool execute HttpGet error: " + e);
return "";
}
}
@ -43,20 +44,79 @@ async function asyncTask() : Promise<void> {
@Component
struct Index {
@State message: string = 'Hello World'
@State result: string = ""
url: string = "http://www.baidu.com/"
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));
})
}
Row() {
Text("URL: ")
.fontSize(20)
.backgroundColor('#ff23d0c2')
.fontWeight(FontWeight.Bold)
.margin({ left: 10, right: 5 });
TextInput({ placeholder: this.url })
.backgroundColor(Color.White)
.fontSize(20)
.width('65%')
.border({ width: 1 })
.onChange((value: string) => {
this.url = value;
})
}
.width('100%')
.margin( { bottom: 20 });
Row() {
Text(this.result)
.width('90%')
.border({ width: 1 })
.fontSize(15)
.baselineOffset(0)
.padding(10)
.height(450)
.margin({ bottom: 50 })
.maxLines(50)
.copyOption(CopyOptions.LocalDevice)
}
Row() {
Button('下载')
.width('200vp')
.height('60vp')
.fontSize('16fp')
.fontWeight(FontWeight.Bold)
.borderRadius('20vp')
.margin('12vp')
.onClick(() => {
asyncTask().then((res) => {
this.result = res;
});
})
}
Row() {
Button('清空')
.width('200vp')
.height('60vp')
.fontSize('16fp')
.fontWeight(FontWeight.Bold)
.borderRadius('20vp')
.margin('12vp')
.onClick(() => {
this.result = "";
})
}
} // Column()
.width('100%')
}
.margin( { bottom: 50 });
} // Row()
.height('100%')
}
}
} // build
} // Index

View File

@ -1,7 +1,11 @@
#include "acl_stdafx.hpp"
#ifdef ACL_HAS_FLOCK_LOCK
# include <sys/file.h>
#endif
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/stdlib/locker.hpp"
# include "acl_cpp/stdlib/locker.hpp"
#endif
/*

View File

@ -1,8 +1,13 @@
#include "acl_stdafx.hpp"
#ifdef ACL_HAS_FLOCK_LOCK
# include <sys/file.h>
#endif
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/stdlib/log.hpp"
#include "acl_cpp/stdlib/util.hpp"
#include "acl_cpp/stream/fstream.hpp"
# include "acl_cpp/stdlib/log.hpp"
# include "acl_cpp/stdlib/util.hpp"
# include "acl_cpp/stream/fstream.hpp"
#endif
namespace acl {