本文整理汇总了C++中TcpConnection::getSocketFd方法的典型用法代码示例。如果您正苦于以下问题:C++ TcpConnection::getSocketFd方法的具体用法?C++ TcpConnection::getSocketFd怎么用?C++ TcpConnection::getSocketFd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TcpConnection
的用法示例。
在下文中一共展示了TcpConnection::getSocketFd方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: start
int Server::start()
{
epoll_event * e = new epoll_event[1000];
if (e == NULL) return -1;
for (;;)
{
int nfd, l_ret;
nfd = epoll_wait(m_epfd, e, 1000, -1);
//printf("nfd: %i %d\n", nfd, m_ready);
for (int i = 0; i < nfd ; i++)
{
if (e[i].data.fd == m_ServerConn.getSocketFd()) // new connection
{
if (m_servers_connected < m_servers_total || m_servers_total == -1)
{
TcpConnection * temp = m_ServerConn.accept();
if (temp != NULL)
{
int fd = temp->getSocketFd();
m_Clients[fd] = temp;
this->setnonblock(fd); // @ todo check for errors
if( this->addHandler(fd, EPOLLIN|EPOLLET|EPOLLRDHUP, temp) != 0 )
{
printf("SERVER ERROR:\t Failed connecting to client.\n");
// @ todo -- attempt recovery
return -1;
}
else
{
printf("SERVER STATUS:\t A client connected.\n");
m_servers_connected += 1;
if ((l_ret = this->handleNewConnection(fd)) < 0) return l_ret;
if (m_servers_total != -1 && m_servers_connected == m_servers_total)
{
m_ready = true;
if ((l_ret = this->allConnectionReadyHandler()) < 0) return l_ret;
}
}
}
} else {
}
}
else
{
if (e[i].events & EPOLLRDHUP || e[i].events & EPOLLHUP || e[i].events & EPOLLERR)
{
printf("SERVER WARNING:\t Client Hangup/Error\n");
handle_epoll(m_epfd, EPOLL_CTL_DEL,e[i].data.fd, NULL); // @todo add error checking
continue;
}
else if (e[i].events & EPOLLIN)
{
if (m_ready)
{
int ret = handler(e[i].data.fd);
if (ret < 0)
{
if (handle_epoll(m_epfd, EPOLL_CTL_DEL,e[i].data.fd, NULL) != 0)
{
return -1;
}
}
} else {
printf("SERVER ERROR:\t Not handleing because not all clients connected\n");
}
}
}
if( modifyHandler(e[i].data.fd, EPOLLET|EPOLLIN|EPOLLHUP ) == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not add handler to TCP Connection\n");
return -1;
}
}
}
delete [] e;
return 0;
}
示例2: sizeof
int Network::Server::initConnection(const char* host, const char* port)
{
if (strlen(host) > INET6_ADDRSTRLEN || strlen(port) > MAX_PORT_LENGTH )
{
DEBUGPRINT("SERVER ERROR:\t Invalid port or host length to init\n");
return -2;
}
if (host == NULL || port == NULL)
{
DEBUGPRINT("SERVER ERROR:\t Invalid arguments to init\n");
}
LOGPRINT("SERVER STATUS:\t Connection to host:%s:%s\n", host, port);
addrinfo l_hints, *l_result, *l_p;
int l_resvalue = -1;
memset(&l_hints, 0, sizeof(struct addrinfo) );
l_hints.ai_family = AF_UNSPEC;
l_hints.ai_socktype = SOCK_STREAM;
if ((l_resvalue = getaddrinfo(host, port, &l_hints, &l_result)) == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not get addrinfo: %s\n", gai_strerror(l_resvalue));
return -1;
}
TcpConnection * conn = new TcpConnection();
if (conn == NULL)
{
DEBUGPRINT("SERVER ERROR:\t Could not create new TCP Connection\n");
return -1;
}
for (l_p = l_result; l_p != NULL; l_p = l_p->ai_next)
{
if ((l_resvalue =conn->socket(l_p)) != 0)
{
if (l_resvalue == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not connect socket trying next\n");
} else if (l_resvalue == -2)
{
DEBUGPRINT("SERVER ERROR:\t Argument error to socket\n");
} else
{
DEBUGPRINT("SERVER ERROR:\t Invalid Return\n");
}
continue;
}
if ((l_resvalue = conn->connect(l_p)) != 0)
{
if (l_resvalue == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not connect to server/port\n");
} else if (l_resvalue == -2)
{
DEBUGPRINT("SERVER ERROR:\t Argument error to connect\n");
} else {
DEBUGPRINT("SERVER ERROR:\t Invalid return\n");
}
conn->close();
continue;
}
break;
}
if (l_p == NULL)
{
DEBUGPRINT("SERVER FAILURE:\t Failed to connect to %s:%s\n", host, port);
return -1;
}
int fileDesc = conn->getSocketFd();
int enable = 1;
if (setsockopt(fileDesc, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) == -1)
{
return -1;
}
if ( setnonblock(fileDesc) == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not set socket to non-blocking\n");
}
if( addHandler(fileDesc, EPOLLET|EPOLLIN|EPOLLHUP, conn ) == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not add handler to tcpConnection\n");
return -1;
}
return fileDesc;
//.........这里部分代码省略.........