Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jul 4, 2023
1 parent 5adf343 commit cbc1540
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 53 deletions.
20 changes: 15 additions & 5 deletions include/swoole_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -88,6 +97,7 @@ class AsyncThreads {
void notify_one();

static int callback(Reactor *reactor, Event *event);

private:
std::mutex init_lock;
};
Expand Down
21 changes: 3 additions & 18 deletions src/coroutine/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
Expand Down
33 changes: 7 additions & 26 deletions src/network/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1129,18 +1112,17 @@ 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;
}

Client *cli = (Client *) event->object;
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;
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/os/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cbc1540

Please sign in to comment.