From 9534dfba568b00e2051c1917a2c5f3ea40dacf8b Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Tue, 8 Oct 2019 09:39:05 +0800 Subject: [PATCH] change function conn_pool_connect_server_ex --- src/connection_pool.c | 27 +++++++++++++++++---------- src/connection_pool.h | 22 ++++++++++++++++++++-- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/connection_pool.c b/src/connection_pool.c index 46687ba..a64802f 100644 --- a/src/connection_pool.c +++ b/src/connection_pool.c @@ -86,7 +86,8 @@ void conn_pool_disconnect_server(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 domain; @@ -108,15 +109,18 @@ int conn_pool_connect_server_ex(ConnectionInfo *pConnection, pConnection->sock = socket(domain, SOCK_STREAM, 0); if(pConnection->sock < 0) { - logError("file: "__FILE__", line: %d, " \ - "socket create failed, errno: %d, " \ + logError("file: "__FILE__", line: %d, " + "socket create fail, errno: %d, " "error info: %s", __LINE__, errno, STRERROR(errno)); return errno != 0 ? errno : EPERM; } 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); @@ -127,14 +131,17 @@ int conn_pool_connect_server_ex(ConnectionInfo *pConnection, return result; } - if ((result=connectserverbyip_nb(pConnection->sock, \ - pConnection->ip_addr, pConnection->port, \ + if ((result=connectserverbyip_nb(pConnection->sock, + pConnection->ip_addr, pConnection->port, connect_timeout)) != 0) { - logError("file: "__FILE__", line: %d, " \ - "connect to %s:%d fail, errno: %d, " \ - "error info: %s", __LINE__, pConnection->ip_addr, \ - pConnection->port, result, STRERROR(result)); + if (log_connect_error) + { + 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)); + } close(pConnection->sock); pConnection->sock = -1; diff --git a/src/connection_pool.h b/src/connection_pool.h index a72be85..7169ff3 100644 --- a/src/connection_pool.h +++ b/src/connection_pool.h @@ -139,11 +139,13 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection); * pConnection: the connection * connect_timeout: the connect timeout in seconds * 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 * return 0 for success, != 0 for error */ 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 @@ -158,7 +160,23 @@ static inline int conn_pool_connect_server(ConnectionInfo *pConnection, { const char *bind_ipaddr = NULL; 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); } /**