Esempio n. 1
0
void* Network::run()
{
	if (m_listening == false)
		return NULL;

	m_running = true;

	int maxfd;
	fd_set checkfds;
	struct timeval timeout;

	FD_ZERO(&checkfds);
	FD_SET(m_notify.notifyFD(), &checkfds);
	FD_SET(m_Server->getFD(), &checkfds);

	(m_notify.notifyFD() > m_Server->getFD()) ?
		(maxfd = m_notify.notifyFD()) : (maxfd = m_Server->getFD());

	for (;;) {
		fd_set readfds;
		int ret;

		// set select timeout 1 secs
		timeout.tv_sec = 1;
		timeout.tv_usec = 0;

		// set readfds to inital checkfds
		readfds = checkfds;

		ret = select(maxfd + 1, &readfds, NULL, NULL, &timeout);
		if (ret == 0) {
			cleanConnections();
			continue;
		}

		// new data from notify
		if (FD_ISSET(m_notify.notifyFD(), &readfds)) {
			m_running = false;
			break;
		}

		// new data from socket
		if (FD_ISSET(m_Server->getFD(), &readfds)) {
			TCPSocket* socket = m_Server->newSocket();
			if (socket == NULL)
				continue;

			Connection* connection = new Connection(socket, m_queue);
			if (connection == NULL)
				continue;

			connection->start("netConnection");
			m_connections.push_back(connection);
			L.log(net, trace, "[%08x] connection opened %s", connection->getID(), socket->getIP().c_str());
		}

	}

	return NULL;
}
Esempio n. 2
0
void Network::run()
{
    if (!m_listening)
        return;

    int ret;
    struct timespec tdiff;

    // set timeout
    tdiff.tv_sec = 1;
    tdiff.tv_nsec = 0;
#ifdef HAVE_PPOLL
    int socketCount = m_httpServer ? 2 : 1;
    int nfds = 1+socketCount;
    struct pollfd fds[nfds];

    memset(fds, 0, sizeof(fds));

    fds[0].fd = m_notify.notifyFD();
    fds[0].events = POLLIN;

    fds[1].fd = m_tcpServer->getFD();
    fds[1].events = POLLIN;

    if (m_httpServer) {
        fds[2].fd = m_httpServer->getFD();
        fds[2].events = POLLIN;
    }
#else
#ifdef HAVE_PSELECT
    int maxfd;
    fd_set checkfds;

    FD_ZERO(&checkfds);
    FD_SET(m_notify.notifyFD(), &checkfds);
    FD_SET(m_tcpServer->getFD(), &checkfds);
    if (m_httpServer) {
        FD_SET(m_httpServer->getFD(), &checkfds);
    }

    maxfd = (m_notify.notifyFD() > m_tcpServer->getFD()) ?
            m_notify.notifyFD() : m_tcpServer->getFD();
    if (m_httpServer && m_httpServer->getFD()>maxfd) {
        maxfd = m_httpServer->getFD();
    }
#endif
#endif

    while (true) {

#ifdef HAVE_PPOLL
        // wait for new fd event
        ret = ppoll(fds, nfds, &tdiff, NULL);
#else
#ifdef HAVE_PSELECT
        // set readfds to inital checkfds
        fd_set readfds = checkfds;
        // wait for new fd event
        ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tdiff, NULL);
#endif
#endif

        if (ret == 0) {
            cleanConnections();
            continue;
        }
        bool newData = false, isHttp = false;
#ifdef HAVE_PPOLL
        // new data from notify
        if (fds[0].revents & POLLIN) {
            return;
        }

        // new data from socket
        if (fds[1].revents & POLLIN) {
            newData = true;
        } else if (m_httpServer && fds[2].revents & POLLIN) {
            newData = isHttp = true;
        }
#else
#ifdef HAVE_PSELECT
        // new data from notify
        if (FD_ISSET(m_notify.notifyFD(), &readfds)) {
            return;
        }

        // new data from socket
        if (FD_ISSET(m_tcpServer->getFD(), &readfds)) {
            newData = true;
        } else if (m_httpServer && FD_ISSET(m_httpServer->getFD(), &readfds)) {
            newData = isHttp = true;
        }
#endif
#endif
        if (newData) {
            TCPSocket* socket = (isHttp ? m_httpServer : m_tcpServer)->newSocket();
            if (socket == NULL)
                continue;

            Connection* connection = new Connection(socket, isHttp, m_netQueue);
            if (connection == NULL)
                continue;

            connection->start("connection");
            m_connections.push_back(connection);
            logInfo(lf_network, "[%05d] %s connection opened %s", connection->getID(), isHttp ? "HTTP" : "client", socket->getIP().c_str());
        }
    }
}