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


C++ TcpConnection::SetNonBlock方法代码示例

本文整理汇总了C++中TcpConnection::SetNonBlock方法的典型用法代码示例。如果您正苦于以下问题:C++ TcpConnection::SetNonBlock方法的具体用法?C++ TcpConnection::SetNonBlock怎么用?C++ TcpConnection::SetNonBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TcpConnection的用法示例。


在下文中一共展示了TcpConnection::SetNonBlock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Loopfd

/* loop */
int IOLoop::Loopfd(){

    /* loop ,until block */
    while(1) {
        ESLog::ISLog("LOOP FD CYCLE START\n", __FILE__, __LINE__);

        int result = 0;
        int nfds = 0;
        /* wait the events */
        nfds = epoll_wait( this->epfd, this->events, this->maxEvents,-1 );
        
        ESLog::ISLog("Loofd event come, there is %d number total events\n", __FILE__, __LINE__, nfds );

        /* tackle the events happend */
        for( int i = 0; i < nfds; i++ ) {
            ESLog::ISLog("event data(fd) %d\n",__FILE__, __LINE__, this->events[i].data.fd );
            ESLog::ISLog("event events %d\n",  __FILE__, __LINE__,this->events[i].events );

            /* if the listen fd event happend, than is to say a new tcp connection establish */
            if( this->events[i].data.fd == Server::Instance()->GetSite().GetSiteLisnFd() ) {
                
                ESLog::ISLog("AN NEW SESSION START\n", __FILE__, __LINE__);

                /* accept a new tcp connection */
                TcpConnection *clientConn = Server::Instance()->GetSite().AcceptTcpConn();
                if( clientConn == NULL ) {
                    continue;
                }

                /* set the connection is nonblocking */
                result = clientConn->SetNonBlock();
                if( result < 0 ) {
                    continue;
                }

                /* get an idle webStream */
                WebStream *idleStream = ((Server::Instance())->GetStreamManager()).GetIdleWebStream();

                ESLog::ISLog("now idleStream size:%d\n", __FILE__, __LINE__,Server::Instance()->GetStreamManager().GetIdleStreamSize());

                /* set the tcpConnection of the idleStream */
                idleStream->SetTcpConn(clientConn);

                /* add the idleWebStream into the readWebStram
                   and it will become an readable webStream */
                //Server::Instance()->GetStreamManager().AddReadWebStream( idleStream );

                /* listen clientconnection fd for read event in epoll */
                result = this->RegReadEventFd(clientConn->GetTcpConnectionFd(), (void *)idleStream );
                if( result < 0 ) {
                    continue;
                }

                idleStream->SetStatus(WSTREAM_UNINITILIZED);

                /* add into the readStream */
                Server::Instance()->GetStreamManager().AddReadWebStream( idleStream ); 

                ESLog::ISLog("remote bounded idlestream tcpconn fd: %d \n", __FILE__, __LINE__, idleStream->GetTcpConnFd());
                ESLog::ISLog("remote ip:%s, port:%d\n", __FILE__, __LINE__, idleStream->GetTcpConn()->GetIP().c_str(), idleStream->GetTcpConn()->GetPort());

                ESLog::ISLog("AN NEW SESSION END\n", __FILE__, __LINE__);
                //continue;

            }
            /* if the events happend, fd can be read */
            else if( this->events[i].events & EPOLLIN ) {

                ESLog::ISLog("AN READ EVENT START\n",__FILE__, __LINE__);

                int result = 0;

                 /* get the parameter from the kernel , that we pass in the former step */
                WebStream *readStream = (WebStream * )events[i].data.ptr;

                readStream->SetStatus(WSTREAM_READ);

                ESLog::ISLog("read event from fd: %d \n",__FILE__, __LINE__, readStream->GetTcpConnFd());
                ESLog::ISLog("read stream bound ip:%s, port:%d\n", __FILE__, __LINE__, readStream->GetTcpConn()->GetIP().c_str(), readStream->GetTcpConn()->GetPort());

                /* we tackle the read event, read the data from the kernel into webStream buffer */
                result = readStream->StreamRead();
                if( result == -1 ) {
                    ESLog::ISLog("there is no data to read\n", __FILE__, __LINE__);
                    ESLog::ISLog("AN READ EVENT END, THIS TIME HAS NO DATA TO READ\n",__FILE__, __LINE__);
                    continue; // we have to tackle , there is data in the remote, there is no data in the kernel
                              // or in the user space, we have no space to read.
                }
                else if( result == 0 || result == -2) {// read compelete, the client close the tcpConnection, after write, the server close too.

                    ESLog::ISLog("read complete, will close the session\n", __FILE__, __LINE__);

                    ESLog::ISLog("tcpConnection:%d fd, set fin_wait_2 status !\n", __FILE__, __LINE__, readStream->GetTcpConnFd());

                    /* change the state of the tcpConnection of webStream */
                    readStream->SetTcpConnStatusCliClose();

                    ESLog::ISLog("http ptr:%u\n", __FILE__, __LINE__, readStream->GetHttpPtr());
                    ESLog::ISLog("TcpConnection status:%s\n", __FILE__, __LINE__, readStream->GetTcpConn()->GetStatusDesc());
//.........这里部分代码省略.........
开发者ID:kzhiquan,项目名称:blog-eyesee,代码行数:101,代码来源:IOLoop.cpp


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