fix dns bug, if dns not be resolved then resolved when connect every time

This commit is contained in:
ruanshudong 2019-11-04 21:49:20 +08:00
parent 57da604c10
commit e21ab4fcf2
3 changed files with 47 additions and 23 deletions

2
servant/libservant/CommunicatorEpoll.cpp Normal file → Executable file
View File

@ -446,7 +446,7 @@ void CommunicatorEpoll::pushAsyncThreadQueue(ReqMessage * msg)
void CommunicatorEpoll::run()
{
TLOGDEBUG("CommunicatorEpoll::run id:"<<syscall(SYS_gettid)<<endl);
// TLOGDEBUG("CommunicatorEpoll::run id:"<<syscall(SYS_gettid)<<endl);
ServantProxyThreadData * pSptd = ServantProxyThreadData::getData();
assert(pSptd != NULL);

31
servant/libservant/EndpointInfo.cpp Normal file → Executable file
View File

@ -30,6 +30,7 @@ EndpointInfo::EndpointInfo()
, _weighttype(0)
, _authType(0)
, _isIPv6(false)
, _addressSucc(false)
{
_setDivision.clear();
memset(&_addr,0,sizeof(_addr));
@ -45,10 +46,9 @@ EndpointInfo::EndpointInfo(const string& host, uint16_t port, EndpointInfo::ETyp
, _weight(weight)
, _weighttype(weighttype)
, _authType(authType)
, _addressSucc(false)
{
_isIPv6 = TC_Socket::addressIsIPv6(host);
try
{
if(_weighttype == 0)
{
_weight = -1;
@ -63,6 +63,20 @@ EndpointInfo::EndpointInfo(const string& host, uint16_t port, EndpointInfo::ETyp
_weight = (_weight > 100 ? 100 : _weight);
}
_cmpDesc = createCompareDesc();
_desc = createDesc();
parseAddress();
}
void EndpointInfo::parseAddress()
{
if(_addressSucc)
return;
try
{
if (_isIPv6)
{
NetworkUtil::getAddress(_host, _port, _addr.in6);
@ -72,13 +86,11 @@ EndpointInfo::EndpointInfo(const string& host, uint16_t port, EndpointInfo::ETyp
NetworkUtil::getAddress(_host, _port, _addr.in);
}
_cmpDesc = createCompareDesc();
_desc = createDesc();
_addressSucc = true;
}
catch (...)
{
TLOGERROR("[ERROR:getAddress fail:" << _host << ":" << _port << "]" << endl);
TLOGERROR("EndpointInfo::parseAddress fail:" << _host << ":" << _port << "]" << endl);
}
}
@ -148,13 +160,16 @@ uint16_t EndpointInfo::port() const
return _port;
}
const struct sockaddr_in& EndpointInfo::addr() const
const struct sockaddr_in& EndpointInfo::addr()
{
parseAddress();
return _addr.in;
}
const struct sockaddr * EndpointInfo::addrPtr() const
const struct sockaddr * EndpointInfo::addrPtr()
{
parseAddress();
return _isIPv6 ? (struct sockaddr *)&_addr.in6 : (struct sockaddr *)&_addr.in;
}

13
servant/servant/EndpointInfo.h Normal file → Executable file
View File

@ -120,14 +120,14 @@ public:
*
* @return const struct sockaddr_in&
*/
const struct sockaddr_in& addr() const;
const struct sockaddr_in& addr() ;
/**
* Get ipv4 or ipv6 struct sockaddr
*
* @return const struct sockaddr *
*/
const struct sockaddr * addrPtr() const;
const struct sockaddr * addrPtr();
/**
*
@ -201,6 +201,10 @@ protected:
*/
string createCompareDesc();
/**
*
*/
void parseAddress();
private:
/**
* IP
@ -271,6 +275,11 @@ private:
* _host is IPv6 or not
*/
bool _isIPv6;
/**
*
*/
bool _addressSucc;
};
/////////////////////////////////////////////////////////////////////////////
}