change function conn_pool_connect_server_ex

pull/37/head
YuQing 2019-10-08 09:39:05 +08:00
parent 69463768ea
commit 9534dfba56
2 changed files with 37 additions and 12 deletions

View File

@ -86,7 +86,8 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection)
} }
int conn_pool_connect_server_ex(ConnectionInfo *pConnection, int conn_pool_connect_server_ex(ConnectionInfo *pConnection,
const int connect_timeout, const char *bind_ipaddr) const int connect_timeout, const char *bind_ipaddr,
const bool log_connect_error)
{ {
int result; int result;
int domain; int domain;
@ -108,15 +109,18 @@ int conn_pool_connect_server_ex(ConnectionInfo *pConnection,
pConnection->sock = socket(domain, SOCK_STREAM, 0); pConnection->sock = socket(domain, SOCK_STREAM, 0);
if(pConnection->sock < 0) if(pConnection->sock < 0)
{ {
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, "
"socket create failed, errno: %d, " \ "socket create fail, errno: %d, "
"error info: %s", __LINE__, errno, STRERROR(errno)); "error info: %s", __LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM; return errno != 0 ? errno : EPERM;
} }
if (bind_ipaddr != NULL && *bind_ipaddr != '\0') if (bind_ipaddr != NULL && *bind_ipaddr != '\0')
{ {
socketBind2(domain, pConnection->sock, bind_ipaddr, 0); if ((result=socketBind2(domain, pConnection->sock, bind_ipaddr, 0)) != 0)
{
return result;
}
} }
SET_SOCKOPT_NOSIGPIPE(pConnection->sock); SET_SOCKOPT_NOSIGPIPE(pConnection->sock);
@ -127,14 +131,17 @@ int conn_pool_connect_server_ex(ConnectionInfo *pConnection,
return result; return result;
} }
if ((result=connectserverbyip_nb(pConnection->sock, \ if ((result=connectserverbyip_nb(pConnection->sock,
pConnection->ip_addr, pConnection->port, \ pConnection->ip_addr, pConnection->port,
connect_timeout)) != 0) connect_timeout)) != 0)
{ {
logError("file: "__FILE__", line: %d, " \ if (log_connect_error)
"connect to %s:%d fail, errno: %d, " \ {
"error info: %s", __LINE__, pConnection->ip_addr, \ logError("file: "__FILE__", line: %d, "
"connect to server %s:%d fail, errno: %d, "
"error info: %s", __LINE__, pConnection->ip_addr,
pConnection->port, result, STRERROR(result)); pConnection->port, result, STRERROR(result));
}
close(pConnection->sock); close(pConnection->sock);
pConnection->sock = -1; pConnection->sock = -1;

View File

@ -139,11 +139,13 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection);
* pConnection: the connection * pConnection: the connection
* connect_timeout: the connect timeout in seconds * connect_timeout: the connect timeout in seconds
* bind_ipaddr: the ip address to bind, NULL or empty for any * bind_ipaddr: the ip address to bind, NULL or empty for any
* log_connect_error: if log error info when connect fail
* NOTE: pConnection->sock will be closed when it >= 0 before connect * NOTE: pConnection->sock will be closed when it >= 0 before connect
* return 0 for success, != 0 for error * return 0 for success, != 0 for error
*/ */
int conn_pool_connect_server_ex(ConnectionInfo *pConnection, int conn_pool_connect_server_ex(ConnectionInfo *pConnection,
const int connect_timeout, const char *bind_ipaddr); const int connect_timeout, const char *bind_ipaddr,
const bool log_connect_error);
/** /**
* connect to the server * connect to the server
@ -158,7 +160,23 @@ static inline int conn_pool_connect_server(ConnectionInfo *pConnection,
{ {
const char *bind_ipaddr = NULL; const char *bind_ipaddr = NULL;
return conn_pool_connect_server_ex(pConnection, return conn_pool_connect_server_ex(pConnection,
connect_timeout, bind_ipaddr); connect_timeout, bind_ipaddr, true);
}
/**
* connect to the server
* parameters:
* pConnection: the connection
* connect_timeout: the connect timeout in seconds
* return 0 for success, != 0 for error
*/
static inline int conn_pool_connect_server_anyway(ConnectionInfo *pConnection,
const int connect_timeout)
{
const char *bind_ipaddr = NULL;
pConnection->sock = -1;
return conn_pool_connect_server_ex(pConnection,
connect_timeout, bind_ipaddr, true);
} }
/** /**