Пример #1
0
int32 EventLoop::run() {
    int32 ret = 0;
    int32 nfds = 0;
    m_status = STATUS_RUNNING;
    NLogError << "EventLoop:" << std::hex << this << " run start!";

    struct epoll_event events[MAX_PROC_EVENT_CNT];
    int32 events_on_loop = MAX_PROC_EVENT_CNT < NetSettings::procEventCnt ?
                           MAX_PROC_EVENT_CNT : NetSettings::procEventCnt;

    time_tv tv;
    while(1) {
        //process Op and timer at first
        processOps();
        int64 t = processTimers();
        if(m_status != STATUS_RUNNING&& !ConnectionsCount()) {
            m_status = STATUS_CLOSED;
            break;
        }
        if(t > 0) {
            nfds = epoll_wait(m_epl,events, events_on_loop, t);
        } else {
            nfds = epoll_wait(m_epl,events, events_on_loop, -1);
        }
        if(nfds < 0) {
            NLogTrace << "EventLoop:" << std::hex << this
                      << ", epoll error,errno:" << errno;
            if(errno != SOCK_EINTR) {
                break;
            }
        }
        for(int32 i = 0; i < nfds && i < events_on_loop; ++i) {
            if(events[i].data.fd == m_ctlfd) {
                //std::cout << "epoll m_ctlfd"<< std::endl;
                continue;
            }
            std::string addr;
            int32 port;
            Device* con = (Device*)events[i].data.ptr;
            con->getAddr(addr,port);
            if(events[i].events & EPOLLIN) {
                ret = con->handleRead(this);
            } else if(events[i].events & EPOLLOUT) {
                ret = con->handleWrite(this);
            } else {
                ret = -1;
            }

            if(ret < 0) {
                NLogWarn << "EventLoop:" << std::hex << this
                         << ", con:" << std::hex << con
                         << std::dec << ", fd:" << con->getFd()
                         << ", " << (addr) << ":" << port
                         << " handle event:" << events[i].events
                         << " fail, recycle!";
                con->onFail();
                continue;
            }
#if DETAIL_NET_LOG
            else {
                NLogTrace << "EventLoop:" << std::hex << this
                          << ", con:" << std::hex << con
                          << std::dec << ", fd:" << con->getFd()
                          << ", " << (addr) << ":" << port
                          << " handle event success!";
            }
#endif
        }
    }

    NLogError << "EventLoop:" << std::hex << this << " run stop!";
    return 0;
}
Пример #2
0
	int32 EventLoop::run()
	{
		SDK_LOG(LOG_LEVEL_TRACE, "eventloop run");
		const struct timeval c_tvmax = { LONG_MAX, LONG_MAX };
		struct timeval tv;
		struct timeval* ptv;
		fd_set fds;

		while (m_run)
		{
			tv = c_tvmax;
			processTimers(tv);
			ptv = (tv_cmp(c_tvmax, tv) == 0) ? NULL : &tv;
			FD_ZERO(&fds);
			SOCKET maxfd = m_ctlfdr;
			FD_SET(m_ctlfdr, &fds);
			for (CliConnMap::iterator it = m_conns.begin(); it != m_conns.end();it++)
			{
				CliConn* pcon = it->second;
				if (pcon && pcon->getfd() != INVALID_SOCKET)
				{
					FD_SET(pcon->getfd(), &fds);
					maxfd = (maxfd >= pcon->getfd()) ? maxfd:pcon->getfd();
				}
			}
			maxfd++;

			//SDK_LOG(LOG_LEVEL_TRACE, "select time out = %s", itostr(tv.tv_sec).c_str());
			int32 ret = select(maxfd, &fds, NULL, NULL, ptv);
			if (ret < 0)
			{
				if (ret != /*SOCK_EINTR*/4)
				{
					SDK_LOG(LOG_LEVEL_TRACE, "select error %d", ret);
					return -1;
				}
			}
			else if (ret == 0)
			{
				//time out
			}
			else
			{
				if (FD_ISSET(m_ctlfdr, &fds))
				{
					processOps();
				}

				std::vector<std::string> errconns;
				for (CliConnMap::iterator it = m_conns.begin(); it != m_conns.end(); it++)
				{
					CliConn* pcon = it->second;
					if (pcon && FD_ISSET(pcon->getfd(), &fds))
					{
						if (pcon->handleRead() < 0)
						{
							pcon->onDisconnect(true, MY_NETWORK_ERROR);
							errconns.push_back(pcon->getCid());
						}
					}
				}
				for (std::vector<std::string>::iterator it = errconns.begin(); it != errconns.end(); ++it)
				{
					delConn(*it);
				}
			}
		}
		//onStopAndWait();
		SDK_LOG(LOG_LEVEL_TRACE, "eventloop exit");
		return 0;
	}