当前位置: 首页>>代码示例>>C++>>正文


C++ ListenSocket::poll方法代码示例

本文整理汇总了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;
}
开发者ID:mickymuis,项目名称:voxowl,代码行数:69,代码来源:server.cpp


注:本文中的ListenSocket::poll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。