Skip to content

Commit

Permalink
c-ares is using threads
Browse files Browse the repository at this point in the history
  • Loading branch information
atupone committed Jul 6, 2024
1 parent 4715841 commit b1bf64c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/net/AresHandler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
/* system implementation headers */
#include <cerrno>
#include <cstring>
#include <mutex>

bool AresHandler::globallyInited = false;
std::mutex callback_mutex;

AresHandler::AresHandler(int _index)
: index(_index), status(None)
Expand All @@ -28,7 +30,15 @@ AresHandler::AresHandler(int _index)
/* ask for local "hosts" lookups too */
static char lookups[] = "fb";
struct ares_options opts;
memset(&opts, 0, sizeof(opts));
int flags = 0;

opts.lookups = lookups;
flags |= ARES_OPT_LOOKUPS;
#if ARES_VERSION_MAJOR >= 1 && ARES_VERSION_MINOR >= 26
opts.evsys = ARES_EVSYS_DEFAULT;
flags |= ARES_OPT_EVENT_THREAD;
#endif

/* start up our resolver */
if (!globallyInited)
Expand All @@ -42,7 +52,7 @@ AresHandler::AresHandler(int _index)
}

/* start up our resolver */
int code = ares_init_options (&aresChannel, &opts, ARES_OPT_LOOKUPS);
int code = ares_init_options (&aresChannel, &opts, flags);
aresFailed = (code != ARES_SUCCESS);
if (aresFailed)
{
Expand Down Expand Up @@ -129,6 +139,9 @@ void AresHandler::callback(int callbackStatus, struct hostent *hostent)
{
if (callbackStatus == ARES_EDESTRUCTION)
return;

const std::lock_guard<std::mutex> lock(callback_mutex);

if (callbackStatus != ARES_SUCCESS)
{
logDebugMessage(1,"Player [%d] failed to resolve: error %d\n", index,
Expand All @@ -150,17 +163,29 @@ void AresHandler::callback(int callbackStatus, struct hostent *hostent)

const char *AresHandler::getHostname()
{
const std::lock_guard<std::mutex> lock(callback_mutex);

return hostName.c_str();
}

AresHandler::ResolutionStatus AresHandler::getHostAddress(struct in_addr
*clientAddr)
{
const std::lock_guard<std::mutex> lock(callback_mutex);

if (status == HbNSucceeded)
memcpy(clientAddr, &hostAddress, sizeof(hostAddress));
return status;
}

#if ARES_VERSION_MAJOR >= 1 && ARES_VERSION_MINOR >= 26
void AresHandler::setFd(fd_set *, fd_set *, int &)
{
}
void AresHandler::process(fd_set *, fd_set *)
{
}
#else
void AresHandler::setFd(fd_set *read_set, fd_set *write_set, int &maxFile)
{
if (aresFailed)
Expand All @@ -176,6 +201,7 @@ void AresHandler::process(fd_set *read_set, fd_set *write_set)
return;
ares_process(aresChannel, read_set, write_set);
}
#endif

// Local Variables: ***
// mode: C++ ***
Expand Down

0 comments on commit b1bf64c

Please sign in to comment.