Skip to content

Commit 5fd6163

Browse files
committed
fixed some
1 parent 11a8743 commit 5fd6163

File tree

10 files changed

+37
-45
lines changed

10 files changed

+37
-45
lines changed

src/hmemory.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace hcpp
2424
class memory
2525
{
2626
public:
27-
virtual awaitable<bool> wait() = 0;
27+
virtual awaitable<void> wait() {co_return;}
2828

2929
// XXX读取最大max_n的数据,放到buff中.
3030
virtual awaitable<std::string_view> async_load_some(std::size_t max_n = 1024 * 4) { co_return ""; }
@@ -67,11 +67,6 @@ namespace hcpp
6767
static void expection_impl() { throw std::runtime_error("to implement"); }
6868

6969
public:
70-
virtual awaitable<bool> wait()
71-
{
72-
expection_impl();
73-
co_return false;
74-
}
7570

7671
// XXX读取最大max_n的数据,放到buff中.
7772
virtual awaitable<std::string_view> async_load_some(std::size_t max_n = 1024 * 4)

src/http/http_svc_keeper.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,35 @@
1616

1717
namespace hcpp
1818
{
19+
class failed_mem : public simple_mem
20+
{
21+
virtual bool ok()
22+
{
23+
return false;
24+
}
25+
};
26+
1927
http_svc_keeper::http_svc_keeper(std::shared_ptr<svc_cache> cache, std::shared_ptr<slow_dns> dns) : slow_dns_(dns), endpoint_cache_(cache)
2028
{
2129
}
2230

2331
awaitable<std::shared_ptr<memory>> http_svc_keeper::wait(std::string svc_host, std::string svc_service)
2432
{
25-
auto svc_endpoint = co_await endpoint_cache_->get_endpoint(svc_host, svc_service, slow_dns_);
33+
if (!m_)
34+
{
35+
if (auto o = co_await make_socket(svc_host, svc_service); o)
36+
{
37+
m_ = std::make_shared<socket_memory>(std::move(*o));
38+
}
39+
else
40+
{
41+
m_ = std::make_shared<failed_mem>();
42+
}
43+
}
44+
co_return m_;
45+
// auto svc_endpoint = co_await endpoint_cache_->get_endpoint(svc_host, svc_service, slow_dns_);
2646

27-
co_return std::make_shared<service_worker>(svc_endpoint, svc_host, svc_service, endpoint_cache_);
47+
// co_return std::make_shared<service_worker>(svc_endpoint, svc_host, svc_service, endpoint_cache_);
2848
}
2949

3050
awaitable<std::shared_ptr<tunnel>> http_svc_keeper::wait_tunnel(std::string svc_host, std::string svc_service)
@@ -65,14 +85,6 @@ namespace hcpp
6585
}
6686
};
6787

68-
class failed_mem : public simple_mem
69-
{
70-
virtual bool ok()
71-
{
72-
return false;
73-
}
74-
};
75-
7688
awaitable<void>
7789
svc_cache::imp::get_endpoint(const std::string &host, const std::string &service, std::shared_ptr<hcpp::slow_dns> dns, std::shared_ptr<channel> c)
7890
{
@@ -242,7 +254,7 @@ namespace hcpp
242254
}
243255
}
244256

245-
awaitable<bool> service_worker::wait()
257+
awaitable<void> service_worker::wait()
246258
{
247259
co_return co_await m_->wait();
248260
}

src/http/http_svc_keeper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace hcpp
5353
~service_worker();
5454

5555
public:
56-
virtual awaitable<bool> wait() override;
56+
virtual awaitable<void> wait() override;
5757

5858
virtual awaitable<std::string_view> async_load_some(std::size_t max_n) override;
5959
virtual awaitable<std::size_t> async_write_some(std::string_view) override;
@@ -99,6 +99,7 @@ namespace hcpp
9999
private:
100100
std::shared_ptr<slow_dns> slow_dns_;
101101
std::shared_ptr<svc_cache> endpoint_cache_;
102+
std::shared_ptr<memory> m_;
102103
};
103104

104105
class svc_cache

src/http/httpmsg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ namespace hcpp
144144
if (header.contains("content-length"))
145145
{
146146
std::string cl = header.at("content-length");
147-
static std::regex digit(R"(0|[1-9]\d{0,10})");
147+
static thread_local std::regex digit(R"(0|[1-9]\d{0,10})");
148148
std::smatch m;
149149
if (!std::regex_search(cl, m, digit))
150150
{

src/http/socket_wrap.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ namespace hcpp
2121
bool operator<(int lk, const socket_memory::mem_block &fk) { return lk < fk.begin_; }
2222
bool operator<(const socket_memory::mem_block &fk1, const socket_memory::mem_block &fk2) { return fk1.begin_ < fk2.begin_; }
2323

24-
awaitable<bool> socket_memory::wait()
24+
awaitable<void> socket_memory::wait()
2525
{
2626
co_await sock_->async_wait(tcp_socket::wait_read);
27-
co_return ok();
2827
}
2928

3029
awaitable<std::string_view> hcpp::socket_memory::async_load_some(std::size_t max_n)

src/http/socket_wrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace hcpp
1818
class socket_memory : public memory
1919
{
2020
public:
21-
virtual awaitable<bool> wait() override;
21+
virtual awaitable<void> wait() override;
2222
virtual awaitable<std::string_view> async_load_some(std::size_t max_n) override;
2323
virtual awaitable<std::size_t> async_write_some(std::string_view) override;
2424

src/http/tunnel.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ namespace hcpp
3131
}
3232

3333
public:
34-
virtual awaitable<bool> wait();
3534

3635
// XXX读取最大max_n的数据,放到buff中.
3736
virtual awaitable<std::string_view> async_load_some(std::size_t max_n = 1024 * 4 * 2);
@@ -42,14 +41,6 @@ namespace hcpp
4241
tcp_socket sock_;
4342
};
4443

45-
awaitable<bool> simple_tunnel_mem::wait()
46-
{
47-
// co_await sock_.async_wait(tcp_socket::wait_write);
48-
// co_await sock_.async_wait(tcp_socket::wait_read);
49-
// read_ok_ = write_ok_ = true;
50-
co_return ok();
51-
}
52-
5344
awaitable<std::string_view> simple_tunnel_mem::async_load_some(std::size_t max_n)
5445
{
5546

@@ -70,9 +61,8 @@ namespace hcpp
7061
awaitable<void> simple_tunnel_mem::async_write_all(std::string_view data)
7162
{
7263
auto [e, n] = co_await async_write(sock_, buffer(data), as_tuple(use_awaitable));
73-
if (n == 0)
64+
if (e)
7465
{
75-
sock_.shutdown(tcp_socket::shutdown_send);
7666
write_ok_ = false;
7767
}
7868
}

src/https/ssl_socket_wrap.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ namespace hcpp
1717
bool operator<(int lk, const ssl_sock_mem::mem_block &fk) { return lk < fk.begin_; }
1818
bool operator<(const ssl_sock_mem::mem_block &fk1, const ssl_sock_mem::mem_block &fk2) { return fk1.begin_ < fk2.begin_; }
1919

20-
awaitable<bool> ssl_sock_mem::wait()
21-
{
22-
co_return ok();
23-
}
2420
awaitable<std::string_view> ssl_sock_mem::async_load_some(std::size_t max_n)
2521
{
2622
std::size_t begin = write_index_;

src/https/ssl_socket_wrap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace hcpp
2424
class ssl_sock_mem : public memory
2525
{
2626
public:
27-
virtual awaitable<bool> wait() override;
2827
virtual awaitable<std::string_view> async_load_some(std::size_t max_n) override;
2928
virtual awaitable<std::size_t> async_write_some(std::string_view) override;
3029

src/httpserver.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ namespace hcpp
2424
{
2525
auto ss = client->get_memory();
2626
ss->reset();
27-
if (!co_await ss->wait())
27+
if (!ss->ok())
2828
{
2929
break;
3030
}
31-
31+
co_await ss->wait();
3232
auto req = client->make_request();
3333
auto p1 = req.get_first_parser(ss);
3434

@@ -45,7 +45,6 @@ namespace hcpp
4545
else if (!req.host_.empty())
4646
{
4747
req.headers_.erase("proxy-connection");
48-
// req.headers_["connection"] = "Connection: keep-alive\r\n";
4948
std::string req_line = req.method_str_ + " " + req.url_ + " " + req.version_ + "\r\n";
5049
for (auto &&header : req.headers_)
5150
{
@@ -56,6 +55,7 @@ namespace hcpp
5655
auto w = co_await sk->wait(req.host_, req.port_);
5756

5857
log::info("http_do:请求开始\n{}{}", req.host_, req.url_);
58+
5959
auto start_point = std::chrono::high_resolution_clock::now();
6060
co_await w->async_write_all(req_line);
6161
if (req.chunk_coding_)
@@ -86,11 +86,11 @@ namespace hcpp
8686
{
8787
log::warn("不保活 {}", req.host_);
8888
w->close();
89+
ss->close();
8990
}
9091
}
9192

9293
std::string msg_header = rp.response_line_ + rp.response_header_str_;
93-
// log::error("{}响应头\n{}",req.host_,msg_header);
9494
co_await ss->async_write_all(msg_header);
9595
if (rp.chunk_coding_)
9696
{
@@ -125,11 +125,11 @@ namespace hcpp
125125
}
126126
else
127127
{
128-
;
128+
log::debug("httpdo: 没有响应体");
129129
}
130130
auto end_point = std::chrono::high_resolution_clock::now();
131131
auto du = std::chrono::duration_cast<std::chrono::milliseconds>(end_point - start_point).count();
132-
log::info("http_do:请求结束\n{}{}\n间隔 {}ms ", req.host_, req.url_, du);
132+
log::error("http_do:请求结束\n{}{} 间隔 {}ms ", req.host_, req.url_, du);
133133
continue;
134134
}
135135
}
@@ -223,7 +223,7 @@ namespace hcpp
223223
// TODO 抽象工厂方法
224224
auto sk = std::make_shared<mitm_svc>();
225225

226-
hsc->init_ = [&cr_ps_map,sk,cc,ca_subject](https_client &self)->awaitable<void>
226+
hsc->init_ = [&cr_ps_map, sk, cc, ca_subject](https_client &self) -> awaitable<void>
227227
{
228228
subject_identify si{};
229229
if (auto i = cr_ps_map.find(self.host_); i != cr_ps_map.end())

0 commit comments

Comments
 (0)