From cbc154038ceef62fcb860d325b97a9b8786bcbe3 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 4 Jul 2023 18:45:50 +0800 Subject: [PATCH] Optimize code --- include/swoole_async.h | 20 +++++++++++++++----- src/coroutine/system.cc | 21 +++------------------ src/network/client.cc | 33 +++++++-------------------------- src/os/base.cc | 8 ++++---- 4 files changed, 29 insertions(+), 53 deletions(-) diff --git a/include/swoole_async.h b/include/swoole_async.h index ba4355e436b..f5c718d2c0d 100644 --- a/include/swoole_async.h +++ b/include/swoole_async.h @@ -60,11 +60,20 @@ struct AsyncEvent { } }; -struct GethostbynameData { - const char *hostname; - int domain; - char *host; - size_t host_len; +struct GethostbynameRequest { + const char *name; + int family; + char *addr; + size_t addr_len; + + GethostbynameRequest(const char *_name, int _family) : name(_name), family(_family) { + addr_len = _family == AF_INET6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN; + addr = new char[addr_len]; + } + + ~GethostbynameRequest() { + delete[] addr; + } }; class AsyncThreads { @@ -88,6 +97,7 @@ class AsyncThreads { void notify_one(); static int callback(Reactor *reactor, Event *event); + private: std::mutex init_lock; }; diff --git a/src/coroutine/system.cc b/src/coroutine/system.cc index 74b38e22253..e62ce1c56e8 100644 --- a/src/coroutine/system.cc +++ b/src/coroutine/system.cc @@ -137,22 +137,8 @@ ssize_t System::write_file(const char *file, char *buf, size_t length, bool lock std::string gethostbyname_impl_with_async(const std::string &hostname, int domain, double timeout) { AsyncEvent ev{}; - GethostbynameData data; - ev.data = &data; - - if (hostname.size() < INET6_ADDRSTRLEN) { - data.host_len = INET6_ADDRSTRLEN + 1; - } else { - data.host_len = hostname.size() + 1; - } - - data.host = (char *) sw_malloc(data.host_len); - if (!data.host) { - return ""; - } - - data.hostname = hostname.c_str(); - data.domain = domain; + GethostbynameRequest dns_request(hostname.c_str(), domain); + ev.data = &dns_request; ev.retval = 1; coroutine::async(async::handler_gethostbyname, ev, timeout); @@ -164,8 +150,7 @@ std::string gethostbyname_impl_with_async(const std::string &hostname, int domai swoole_set_last_error(ev.error); return ""; } else { - std::string addr(data.host); - sw_free(data.host); + std::string addr(dns_request.addr, dns_request.addr_len); return addr; } } diff --git a/src/network/client.cc b/src/network/client.cc index 3664ecb18af..816265c9a7c 100644 --- a/src/network/client.cc +++ b/src/network/client.cc @@ -615,31 +615,14 @@ static int Client_tcp_connect_async(Client *cli, const char *host, int port, dou if (cli->wait_dns) { AsyncEvent ev{}; - auto data = (GethostbynameData *) sw_malloc(sizeof(GethostbynameData)); - ev.data = data; - - size_t len = strlen(cli->server_host); - if (len < INET6_ADDRSTRLEN) { - data->host_len = INET6_ADDRSTRLEN; - } else { - data->host_len = len + 1; - } - - data->host = (char *) sw_malloc(data->host_len); - if (!data->host) { - swoole_warning("malloc failed"); - return SW_ERR; - } - - data->hostname = host; - data->domain = cli->_sock_domain; + auto dns_request = new GethostbynameRequest(cli->server_host, cli->_sock_domain); + ev.data = dns_request; ev.object = cli; ev.handler = async::handler_gethostbyname; ev.callback = Client_onResolveCompleted; if (swoole::async::dispatch(&ev) == nullptr) { - sw_free(data->host); - sw_free(data); + delete dns_request; return SW_ERR; } else { return SW_OK; @@ -1129,10 +1112,9 @@ static void Client_onTimeout(Timer *timer, TimerNode *tnode) { } static void Client_onResolveCompleted(AsyncEvent *event) { - auto data = (GethostbynameData *) event->data; + auto dns_request = (GethostbynameRequest *) event->data; if (event->canceled) { - sw_free(data->host); - sw_free(data); + delete dns_request; return; } @@ -1140,7 +1122,7 @@ static void Client_onResolveCompleted(AsyncEvent *event) { cli->wait_dns = 0; if (event->error == 0) { - Client_tcp_connect_async(cli, data->host, cli->server_port, cli->timeout, 1); + Client_tcp_connect_async(cli, dns_request->addr, cli->server_port, cli->timeout, 1); } else { swoole_set_last_error(SW_ERROR_DNSLOOKUP_RESOLVE_FAILED); cli->socket->removed = 1; @@ -1149,8 +1131,7 @@ static void Client_onResolveCompleted(AsyncEvent *event) { cli->onError(cli); } } - sw_free(data->host); - sw_free(data); + delete dns_request; } static int Client_onWrite(Reactor *reactor, Event *event) { diff --git a/src/os/base.cc b/src/os/base.cc index 339979346f3..9d6c2d10576 100644 --- a/src/os/base.cc +++ b/src/os/base.cc @@ -85,14 +85,14 @@ namespace async { void handler_gethostbyname(AsyncEvent *event) { char addr[INET6_ADDRSTRLEN]; - auto data = (GethostbynameData *) event->data; - int ret = network::gethostbyname(data->domain, data->hostname, addr); - sw_memset_zero(data->host, data->host_len); + auto request = (GethostbynameRequest *) event->data; + int ret = network::gethostbyname(request->family, request->name, addr); + sw_memset_zero(request->addr, request->addr_len); if (ret < 0) { event->error = SW_ERROR_DNSLOOKUP_RESOLVE_FAILED; } else { - if (inet_ntop(data->domain, addr, data->host, data->host_len) == nullptr) { + if (inet_ntop(request->family, addr, request->addr, request->addr_len) == nullptr) { ret = -1; event->error = SW_ERROR_BAD_IPV6_ADDRESS; } else {