本文整理汇总了C++中ListenSocket::poll方法的典型用法代码示例。如果您正苦于以下问题:C++ ListenSocket::poll方法的具体用法?C++ ListenSocket::poll怎么用?C++ ListenSocket::poll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ListenSocket
的用法示例。
在下文中一共展示了ListenSocket::poll方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: slock
bool
Server::mainloop( Object* root ) {
std::lock_guard<std::mutex> slock( static_lock );
instance_in_mainloop =this;
ListenSocket sock;
sock.setBlocking( false );
if( !sock.bind( _portnum ) || !sock.listen() ) {
log << "Could not bind to port " << _portnum << std::endl;
log << strerror( errno ) << std::endl;
exit( 1 );
}
log << VOXOWL_VERSION_FULL_NAME << " listening to port " << _portnum << std::endl;
connection_list_t::iterator it;
while( !_stop ) {
// Cleanup any closed connections
/* We cannot do this because it's thread-unsafe. We delete everything
* on exit, which may also be a potential problem. FIXME
for( it = connection_list.begin(); it != connection_list.end(); ) {
if( !(*it)->socket->isOpen() ) {
delete (*it);
it =connection_list.erase( it );
}
else
it++;
}*/
// Wait for one second at a time
if( !sock.poll( 1 ) ) {
continue;
}
// It seems a new connection attempt is being made
ClientSocket *csock;
if( ( csock = sock.accept() ) ) {
std::lock_guard<std::mutex> lock( write_lock );
log << "Accepting new connection" << std::endl;
Connection *c = new Connection( &connections );
c->socket =csock;
c->pbuffer =new PacketBuffer();
c->pbuffer->setOutgoingPacketHandler( &connectionSendFunc );
if( !data_connection )
setDataConnection( c );
c->thread =new std::thread( connectionMain, c, this );
connection_list.push_back( c );
}
else
break;
}
log << "Stopping server, waiting for connections to close." << std::endl;
for( it = connection_list.begin(); it != connection_list.end(); it++ ) {
if( (*it)->socket->isOpen() )
(*it)->socket->close();
delete (*it);
}
log << "All connections terminated." << std::endl;
instance_in_mainloop =0;
return true;
}