本文整理汇总了C++中TcpConnection::set_fd方法的典型用法代码示例。如果您正苦于以下问题:C++ TcpConnection::set_fd方法的具体用法?C++ TcpConnection::set_fd怎么用?C++ TcpConnection::set_fd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TcpConnection
的用法示例。
在下文中一共展示了TcpConnection::set_fd方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: connect
TcpConnection* TcpConnector::connect(const TcpSockAddr& addr, int &ret, long timeout) {
int sockfd;
ret = -1;
sockfd = socket(addr.ai_family, SOCK_STREAM, 0);
if (sockfd < 0) return 0L;
if (timeout < 0)
ret = ::connect(sockfd, (struct sockaddr*)&addr.ai_addr, addr.ai_addrlen);
else
ret = timeout_connect(sockfd, (struct sockaddr*)&addr.ai_addr, addr.ai_addrlen, timeout);
if (ret < 0) {
close(sockfd);
return 0L;
} else {
TcpConnection *tcon;
tcon = new TcpConnection;
if (tcon->set_fd(sockfd) < 0) {
delete tcon;
return 0L;
} else {
return tcon;
}
}
}
示例2: accept
TcpConnection* TcpAcceptor::accept(int &ret, long timeout) {
struct timeval *ptimeout;
struct timeval time;
fd_set r_fdset;
int conn_fd;
TcpConnection *conn;
if (timeout < 0) { // block
ptimeout = NULL;
} else {
time.tv_sec = timeout;
time.tv_usec = 0;
ptimeout = &time;
}
while (1) {
FD_ZERO(&r_fdset);
FD_SET(listen_fd, &r_fdset);
switch (select(listen_fd + 1, &r_fdset, NULL, NULL, ptimeout)) {
case 0:
ret = E_TIMEOUT;
return 0L;
case -1:
ret = -1;
return 0L;
default:
if (FD_ISSET(listen_fd, &r_fdset))
break;
else
continue;
}
break;
}
if ((conn_fd=::accept(listen_fd, NULL, NULL)) < 0) {
ret = -1;
return 0L;
}
conn = new TcpConnection;
if (conn->set_fd(conn_fd) < 0) {
delete conn;
return 0L;
} else {
return conn;
}
}