add function socketCreateEx2

pull/37/head
YuQing 2019-09-29 21:35:53 +08:00
parent 583bdc87dc
commit d32a72db1f
2 changed files with 59 additions and 6 deletions

View File

@ -626,12 +626,11 @@ int connectserverbyip_nb_ex(int sock, const char *server_ip, \
return result;
}
int socketClientEx2(int af, const char *server_ip,
const short server_port, const int timeout,
const int flags, const char *bind_ipaddr, int *err_no)
int socketCreateEx2(int af, const char *server_ip,
const int timeout, const int flags,
const char *bind_ipaddr, int *err_no)
{
int sock;
bool auto_detect;
if (af == AF_UNSPEC)
{
@ -642,8 +641,8 @@ int socketClientEx2(int af, const char *server_ip,
if (sock < 0)
{
*err_no = errno != 0 ? errno : EMFILE;
logError("file: "__FILE__", line: %d, " \
"socket create failed, errno: %d, error info: %s", \
logError("file: "__FILE__", line: %d, "
"socket create failed, errno: %d, error info: %s",
__LINE__, errno, STRERROR(errno));
return -1;
}
@ -669,11 +668,34 @@ int socketClientEx2(int af, const char *server_ip,
}
}
*err_no = 0;
return sock;
}
int socketClientEx2(int af, const char *server_ip,
const short server_port, const int timeout,
const int flags, const char *bind_ipaddr, int *err_no)
{
int sock;
bool auto_detect;
sock = socketCreateEx2(af, server_ip,
timeout, flags, bind_ipaddr, err_no);
if (sock < 0)
{
return sock;
}
auto_detect = ((flags & O_NONBLOCK) == 0);
*err_no = connectserverbyip_nb_ex(sock, server_ip,
server_port, timeout, auto_detect);
if (*err_no != 0)
{
logError("file: "__FILE__", line: %d, "
"connect to %s:%d fail, "
"errno: %d, error info: %s",
__LINE__, server_ip, server_port,
*err_no, STRERROR(*err_no));
close(sock);
return -4;
}

View File

@ -325,6 +325,37 @@ int socketServerIPv6(const char *bind_ipaddr, const int port, int *err_no);
*/
int socketServer2(int af, const char *bind_ipaddr, const int port, int *err_no);
/** create socket (NOT connect to server yet)
* parameters:
* af: family, AF_UNSPEC (auto dectect), AF_INET or AF_INET6
* server_ip: ip address of the server to detect family when af == AF_UNSPEC
* timeout: connect timeout in seconds
* flags: socket flags such as O_NONBLOCK for non-block socket
* bind_ipaddr: the ip address to bind, NULL or empty for bind ANY
* err_no: store the error no
* return: >= 0 server socket, < 0 fail
*/
int socketCreateEx2(int af, const char *server_ip,
const int timeout, const int flags,
const char *bind_ipaddr, int *err_no);
/** create socket (NOT connect to server yet)
* parameters:
* server_ip: ip address of the server to detect family
* timeout: connect timeout in seconds
* flags: socket flags such as O_NONBLOCK for non-block socket
* bind_ipaddr: the ip address to bind, NULL or empty for bind ANY
* err_no: store the error no
* return: >= 0 server socket, < 0 fail
*/
static inline int socketCreateExAuto(const char *server_ip,
const int timeout, const int flags,
const char *bind_ipaddr, int *err_no)
{
return socketCreateEx2(AF_UNSPEC, server_ip, timeout, flags,
bind_ipaddr, err_no);
}
/** connect to server
* parameters:
* af: family, AF_UNSPEC (auto dectect), AF_INET or AF_INET6