Skip to content

Commit c9c0fb4

Browse files
committed
Add the Resolver class (#47)
1 parent 60097fe commit c9c0fb4

File tree

11 files changed

+565
-93
lines changed

11 files changed

+565
-93
lines changed

CMakeLists.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ include_directories(
4040
# ${PROJECT_BINARY_DIR}
4141
# )
4242

43-
set(trantor_sources
43+
set(TRANTOR_SOURCES
4444
trantor/utils/AsyncFileLogger.cc
4545
trantor/utils/ConcurrentTaskQueue.cc
4646
trantor/utils/Date.cc
@@ -70,12 +70,24 @@ find_package(OpenSSL)
7070
if(OpenSSL_FOUND)
7171
include_directories(${OPENSSL_INCLUDE_DIR})
7272
link_libraries(${OPENSSL_LIBRARIES})
73-
set(trantor_sources ${trantor_sources} trantor/net/ssl/SSLConnection.cc)
73+
set(TRANTOR_SOURCES ${TRANTOR_SOURCES} trantor/net/ssl/SSLConnection.cc)
7474
else()
75-
set(trantor_sources ${trantor_sources} trantor/net/ssl/SSLConnectionSkipped.cc)
75+
set(TRANTOR_SOURCES ${TRANTOR_SOURCES} trantor/net/ssl/SSLConnectionSkipped.cc)
7676
endif()
7777

78-
add_library(trantor ${trantor_sources})
78+
find_path(CARES_INCLUDE_DIR ares.h)
79+
find_library(CARES_LIBRARY NAMES cares)
80+
if(CARES_INCLUDE_DIR AND CARES_LIBRARY)
81+
message(STATUS "found cares")
82+
message(STATUS "inc:" ${CARES_INCLUDE_DIR})
83+
message(STATUS "lib:" ${CARES_LIBRARY})
84+
include_directories(${CARES_INCLUDE_DIR})
85+
link_libraries(${CARES_LIBRARY})
86+
set(TRANTOR_SOURCES ${TRANTOR_SOURCES} trantor/net/inner/AresResolver.cc)
87+
else()
88+
set(TRANTOR_SOURCES ${TRANTOR_SOURCES} trantor/net/inner/NormalResolver.cc)
89+
endif()
90+
add_library(trantor ${TRANTOR_SOURCES})
7991
set_property(TARGET trantor PROPERTY CXX_STANDARD 14)
8092
set_property(TARGET trantor PROPERTY CXX_STANDARD_REQUIRED ON)
8193
set_property(TARGET trantor PROPERTY CXX_EXTENSIONS OFF)
@@ -92,7 +104,8 @@ set(public_net_headers
92104
trantor/net/TcpClient.h
93105
trantor/net/TcpConnection.h
94106
trantor/net/TcpServer.h
95-
trantor/net/callbacks.h)
107+
trantor/net/callbacks.h
108+
trantor/net/Resolver.h)
96109

97110
set(public_utils_headers
98111
trantor/utils/AsyncFileLogger.h

ChangeLog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
55

6+
### API change list
7+
8+
- Remove the resolve method from the InetAddress class.
9+
10+
### Added
11+
12+
- Add the Resolver class.
13+
614
## [1.0.0-rc4] - 2019-08-08
715

816
### API change list

trantor/net/InetAddress.cc

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ InetAddress::InetAddress(const std::string &ip, uint16_t port, bool ipv6)
8484
_addr6.sin6_port = htons(port);
8585
if (::inet_pton(AF_INET6, ip.c_str(), &_addr6.sin6_addr) <= 0)
8686
{
87-
LOG_SYSERR << "sockets::fromIpPort";
88-
abort();
87+
// LOG_SYSERR << "sockets::fromIpPort";
88+
// abort();
8989
}
9090
}
9191
else
@@ -95,8 +95,8 @@ InetAddress::InetAddress(const std::string &ip, uint16_t port, bool ipv6)
9595
_addr.sin_port = htons(port);
9696
if (::inet_pton(AF_INET, ip.c_str(), &_addr.sin_addr) <= 0)
9797
{
98-
LOG_SYSERR << "sockets::fromIpPort";
99-
abort();
98+
// LOG_SYSERR << "sockets::fromIpPort";
99+
// abort();
100100
}
101101
}
102102
}
@@ -208,77 +208,3 @@ uint16_t InetAddress::toPort() const
208208
{
209209
return ntohs(portNetEndian());
210210
}
211-
#ifdef __linux__
212-
static __thread char t_resolveBuffer[64 * 1024];
213-
#endif
214-
std::mutex InetAddress::_dnsMutex;
215-
std::unordered_map<std::string, std::pair<struct in_addr, trantor::Date>>
216-
InetAddress::_dnsCache;
217-
bool InetAddress::resolve(const std::string &hostname,
218-
InetAddress *out,
219-
size_t timeout)
220-
{
221-
assert(out != NULL);
222-
{
223-
std::lock_guard<std::mutex> guard(_dnsMutex);
224-
if (_dnsCache.find(hostname) != _dnsCache.end())
225-
{
226-
auto &addr = _dnsCache[hostname];
227-
if (timeout == 0 || (timeout > 0 && (addr.second.after(timeout) >
228-
trantor::Date::date())))
229-
{
230-
LOG_TRACE << "dns:Hit cache";
231-
out->_addr.sin_addr = addr.first;
232-
return true;
233-
}
234-
}
235-
}
236-
#ifdef __linux__
237-
struct hostent hent;
238-
struct hostent *he = NULL;
239-
int herrno = 0;
240-
memset(&hent, 0, sizeof(hent));
241-
242-
int ret = gethostbyname_r(hostname.c_str(),
243-
&hent,
244-
t_resolveBuffer,
245-
sizeof t_resolveBuffer,
246-
&he,
247-
&herrno);
248-
if (ret == 0 && he != NULL)
249-
#else
250-
/// Multi-threads safety
251-
static std::mutex _mutex;
252-
struct hostent *he = NULL;
253-
struct hostent hent;
254-
{
255-
std::lock_guard<std::mutex> guard(_mutex);
256-
auto result = gethostbyname(hostname.c_str());
257-
if (result != NULL)
258-
{
259-
memcpy(&hent, result, sizeof(hent));
260-
he = &hent;
261-
}
262-
}
263-
264-
if (he != NULL)
265-
#endif
266-
{
267-
assert(he->h_addrtype == AF_INET && he->h_length == sizeof(uint32_t));
268-
out->_addr.sin_addr = *reinterpret_cast<struct in_addr *>(he->h_addr);
269-
{
270-
std::lock_guard<std::mutex> guard(_dnsMutex);
271-
_dnsCache[hostname].first = out->_addr.sin_addr;
272-
_dnsCache[hostname].second = trantor::Date::date();
273-
}
274-
return true;
275-
}
276-
else
277-
{
278-
// if (ret)
279-
{
280-
LOG_SYSERR << "InetAddress::resolve";
281-
}
282-
return false;
283-
}
284-
}

trantor/net/InetAddress.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,10 @@ class InetAddress
8787
{
8888
return _addr.sin_port;
8989
}
90-
91-
// resolve hostname to IP address, not changing port or sin_family
92-
// return true on success.
93-
// thread safe
94-
static bool resolve(const std::string &hostname,
95-
InetAddress *result,
96-
size_t timeout = 3600);
97-
// static std::vector<InetAddress> resolveAll(const char* hostname, uint16_t
98-
// port = 0);
90+
void setPortNetEndian(uint16_t port)
91+
{
92+
_addr.sin_port = port;
93+
}
9994

10095
private:
10196
union {

trantor/net/Resolver.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016, Tao An. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license
4+
// that can be found in the License file.
5+
6+
// Author: Tao An
7+
8+
#pragma once
9+
#include <memory>
10+
#include <trantor/net/EventLoop.h>
11+
#include <trantor/net/InetAddress.h>
12+
13+
namespace trantor
14+
{
15+
class Resolver
16+
{
17+
public:
18+
typedef std::function<void(const trantor::InetAddress&)> Callback;
19+
static std::shared_ptr<Resolver> newResolver(EventLoop* loop = nullptr,
20+
size_t timeout = 60);
21+
virtual void resolve(const std::string& hostname,
22+
const Callback& callback) = 0;
23+
virtual ~Resolver()
24+
{
25+
}
26+
};
27+
} // namespace trantor

0 commit comments

Comments
 (0)