static int anetTcpGenericConnect(char *err, const char *addr, int port,const char *source_addr, int flags)
{
int s = ANET_ERR, rv;
char portstr[6];
struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
snprintf(portstr,sizeof(portstr),"%d",port);
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_UNSPEC; //未指定
hints.ai_socktype = SOCK_STREAM;//有序、可靠、面向连接的双向字节流
if ((rv = getaddrinfo(addr,portstr,&hints,&servinfo)) != 0) { //解析addr信息,存入 servinfo
//不懂一定要看上面的工具包,写了一晚上呢
anetSetError(err, "%s", gai_strerror(rv)); //这个不管它,报错函数而已
return ANET_ERR;
}
for (p = servinfo; p != NULL; p = p->ai_next) { //工具包里有说,给你一个addrinfo,它可能是一串
if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1) //这里s成为了监听套接字
continue; //如果没打开,那就继续循环,无碍
if (anetSetReuseAddr(err,s) == ANET_ERR) goto error; //设置地址重用
if (flags & ANET_CONNECT_NONBLOCK && anetNonBlock(err,s) != ANET_OK) //设置非阻塞
goto error;
if (source_addr) { //source_addr:传入参数
int bound = 0;
if ((rv = getaddrinfo(source_addr, NULL, &hints, &bservinfo)) != 0) //内啥,不多说了啊
{
anetSetError(err, "%s", gai_strerror(rv));
goto error;
}
for (b = bservinfo; b != NULL; b = b->ai_next) {
if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
bound = 1;
break;
}
}
freeaddrinfo(bservinfo);
if (!bound) {
anetSetError(err, "bind: %s", strerror(errno));
goto error;
}
} //for循环到这里结束
if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
if (errno == EINPROGRESS && flags & ANET_CONNECT_NONBLOCK)
goto end;
close(s);
s = ANET_ERR;
continue;
}
goto end;
}
if (p == NULL)
anetSetError(err, "creating socket: %s", strerror(errno));
error:
if (s != ANET_ERR) {
close(s);
s = ANET_ERR;
}
end:
freeaddrinfo(servinfo);
if (s == ANET_ERR && source_addr && (flags & ANET_CONNECT_BE_BINDING)) {
//#define ANET_CONNECT_BE_BINDING 2
return anetTcpGenericConnect(err,addr,port,NULL,flags);
} else {
return s;
}
}
如果还有什么疑惑,可以在评论区留言一起讨论哦。
相见即是缘分,最近正在写《redis源码学习》系列,可以在我的主页找到。
何妨来个关注呢?