Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Aug 6, 2024
1 parent f8b0749 commit 7305c44
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 13 deletions.
36 changes: 35 additions & 1 deletion hikyuu/utilities/http_client/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,43 @@ class HKU_UTILS_API HttpResponse final {
class HKU_UTILS_API HttpClient {
public:
HttpClient() = default;
explicit HttpClient(const std::string& url) : m_url(nng::url(url)) {};
explicit HttpClient(const std::string& url, int32_t timeout_ms = NNG_DURATION_DEFAULT)
: m_url(nng::url(url)), m_timeout_ms(timeout_ms) {};
virtual ~HttpClient();

HttpClient(const HttpClient&) = delete;
HttpClient& operator=(const HttpClient&) = delete;

HttpClient(HttpClient&& rhs)
: m_default_headers(std::move(rhs.m_default_headers)),
m_url(std::move(rhs.m_url)),
m_client(std::move(rhs.m_client)),
m_aio(std::move(rhs.m_aio)),
m_conn(std::move(rhs.m_conn)),
#if HKU_ENABLE_HTTP_CLIENT_SSL
m_tls_cfg(std::move(rhs.m_tls_cfg)),
m_ca_file(std::move(rhs.m_ca_file)),
#endif
m_timeout_ms(rhs.m_timeout_ms) {
}

HttpClient& operator=(HttpClient&& rhs) {
if (this != &rhs) {
m_default_headers = std::move(rhs.m_default_headers);
m_url = std::move(rhs.m_url);
m_client = (std::move(rhs.m_client));
m_aio = std::move(rhs.m_aio);
m_conn = std::move(rhs.m_conn);
#if HKU_ENABLE_HTTP_CLIENT_SSL
m_tls_cfg = std::move(rhs.m_tls_cfg);
m_ca_file = std::move(rhs.m_ca_file);
#endif
m_timeout_ms = rhs.m_timeout_ms;
rhs.m_timeout_ms = NNG_DURATION_DEFAULT;
}
return *this;
}

bool valid() const noexcept {
return m_url.valid();
}
Expand Down
39 changes: 39 additions & 0 deletions hikyuu/utilities/http_client/nng_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ class aio final {
}
}

aio(aio&& rhs) : m_aio(rhs.m_aio), m_timeout(rhs.m_timeout) {
rhs.m_aio = nullptr;
rhs.m_timeout = NNG_DURATION_DEFAULT;
}

aio& operator=(const aio&) = delete;

aio& operator=(aio&& rhs) {
if (this != &rhs) {
m_aio = rhs.m_aio;
m_timeout = rhs.m_timeout;
rhs.m_aio = nullptr;
rhs.m_timeout = NNG_DURATION_DEFAULT;
}
return *this;
}

bool valid() const noexcept {
return m_aio != nullptr;
}
Expand Down Expand Up @@ -257,6 +274,28 @@ class http_client final {
}
}

http_client(const http_client&) = delete;
http_client& operator=(const http_client&) = delete;

http_client(http_client&& rhs)
: m_client(rhs.m_client), m_aio(rhs.m_aio), m_tls_cfg(rhs.m_tls_cfg) {
rhs.m_client = nullptr;
rhs.m_aio = nullptr;
rhs.m_tls_cfg = nullptr;
}

http_client& operator=(http_client&& rhs) {
if (this != &rhs) {
m_client = rhs.m_client;
m_aio = rhs.m_aio;
m_tls_cfg = rhs.m_tls_cfg;
rhs.m_client = nullptr;
rhs.m_aio = nullptr;
rhs.m_tls_cfg = nullptr;
}
return *this;
}

void set_url(const nng::url& url) {
#if !HKU_ENABLE_HTTP_CLIENT_SSL
if (url.is_https()) {
Expand Down
7 changes: 2 additions & 5 deletions hikyuu/utilities/node/NodeServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ namespace hku {
class NodeServer {
CLASS_LOGGER_IMP(NodeServer)

private:
static constexpr size_t PARALLEL = 128;

public:
NodeServer() = default;
explicit NodeServer(const std::string& addr) : m_addr(addr) {}
Expand All @@ -58,7 +55,7 @@ class NodeServer {
m_handles[cmd] = std::move(handle);
}

void start() {
void start(size_t max_parrel = 128) {
CLS_CHECK(!m_addr.empty(), "You must set NodeServer's addr first!");

// 启动 node server
Expand All @@ -68,7 +65,7 @@ class NodeServer {
CLS_CHECK(0 == rv, "Failed listen node server socket ({})! {}", m_addr, nng_strerror(rv));
CLS_TRACE("channel lisenter server: {}", m_addr);

m_works.resize(PARALLEL);
m_works.resize(max_parrel);
for (size_t i = 0, total = m_works.size(); i < total; i++) {
Work* w = &m_works[i];
rv = nng_aio_alloc(&w->aio, _serverCallback, w);
Expand Down
6 changes: 4 additions & 2 deletions release.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# 版本发布说明

## 1.0.4 -
## 1.0.4 - 2024年8月6日

屏蔽 HttpClient 接收对端 Connect close 时的打印
1. 屏蔽 HttpClient 接收对端 Connect close 时的打印
2. HttpClient创建时增加参数直接指定超时时间
3. NodeServer start 增加参数自行指定最大并发数,默认128

## 1.0.3 - 2024年8月5日

Expand Down
22 changes: 17 additions & 5 deletions test/utilities/http_client/test_HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ TEST_CASE("test_HttpClient") {
cli.setTimeout(1);
CHECK_THROWS(cli.get("/ip"));

HttpClient cli2("http://httpbin.org/", 1);
CHECK_UNARY(cli2.valid());
CHECK_THROWS(cli2.get("/ip"));

// 正常访问 http
cli.setTimeout(-2);
auto res = cli.get("/ip");
Expand Down Expand Up @@ -71,9 +75,17 @@ TEST_CASE("test_HttpClient") {
HKU_INFO("{} {}", cli.url(), res.status());
#endif

cli.setUrl("http://api.vore.top");
res = cli.get("/api/IPdata", {{"ip", "10.0.0.1"}}, HttpHeaders());
auto data = res.json();
HKU_INFO("{}", data.dump());
CHECK_EQ(data["ipinfo"]["text"].get<std::string>(), "10.0.0.1");
try {
cli.setUrl("http://api.vore.top");
res = cli.get("/api/IPdata", {{"ip", "10.0.0.1"}}, HttpHeaders());
if (res.status() == 200) {
auto data = res.json();
HKU_INFO("{}", data.dump());
CHECK_EQ(data["ipinfo"]["text"].get<std::string>(), "10.0.0.1");
} else {
HKU_INFO("res status: {}, body: {}", res.status(), res.body());
}
} catch (const std::exception& e) {
HKU_INFO(e.what());
}
}

0 comments on commit 7305c44

Please sign in to comment.