コード例 #1
0
ファイル: Network.cpp プロジェクト: snikk/Junk
void BaseSocketManager::DoSelect(int pauseMicroSecs, int handleInput) {
    timeval tv;
    tv.tv_sec = 0;

    tv.tv_usec = pauseMicroSecs;

    fd_set inp_set, out_set, exc_set;
    int maxDesc;

    FD_ZERO(&inp_set);
    FD_ZERO(&out_set);
    FD_ZERO(&exc_set);

    maxDesc = 0;

    for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i) {
        NetSocket* pSock = *i;
        if ((pSock->m_deleteFlag & 1) || pSock->m_sock == INVALID_SOCKET)
            continue;

        if (handleInput)
            FD_SET(pSock->m_sock, &inp_set);

        FD_SET(pSock->m_sock, &exc_set);

        if (pSock->VHasOutput())
            FD_SET(pSock->m_sock, &out_set);

        if ((int)pSock->m_sock > maxDesc)
            maxDesc = (int)pSock->m_sock;
    }

    int selRet = 0;

    selRet = select(maxDesc + 1, &inp_set, &out_set, &exc_set, &tv);
    if (selRet == SOCKET_ERROR) {
        GCC_ERROR("Error in DoSelect!");
        return;
    }

    if (selRet) {
        for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i) {
            NetSocket* pSock = *i;

            if ((pSock->m_deleteFlag & 1) || pSock->m_sock == INVALID_SOCKET)
                continue;

            if (FD_ISSET(pSock->m_sock, &exc_set))
                pSock->HandleException();

            if (!(pSock->m_deleteFlag & 1) && FD_ISSET(pSock->m_sock, &out_set))
                pSock->VHandleOutput();

            if (handleInput && !(pSock->m_deleteFlag & 1) && FD_ISSET(pSock->m_sock, &inp_set)) {
                pSock->VHandleInput();
            }
        }
    }

    unsigned int timeNow = timeGetTime();

    SocketList::iterator i = m_SockList.begin();
    while (i != m_SockList.end()) {
        NetSocket* pSock = *i;
        if (pSock->m_timeOut && pSock->m_timeOut < timeNow) 
            pSock->VTimeOut();

        if (pSock->m_deleteFlag & 1) {
            switch (pSock->m_deleteFlag) {
                case 1:
                    g_pSocketManager->RemoveSocket(pSock);
                    i = m_SockList.begin();
                    break;

                case 3:
                    pSock->m_deleteFlag = 2;
                    if (pSock->m_sock != INVALID_SOCKET) {
                        closesocket(pSock->m_sock);
                        pSock->m_sock = INVALID_SOCKET;
                    }
                    break;
            }
        }
        i++;
    }
}
コード例 #2
0
//
// BaseSocketManager::DoSelect					- Chapter 19, page 679
//
void BaseSocketManager::DoSelect(int pauseMicroSecs, bool handleInput) 
{
	timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = pauseMicroSecs;    // 100 microseconds is 0.1 milliseconds or .0001 seconds

	fd_set inp_set, out_set, exc_set;
	int maxdesc;

	FD_ZERO(&inp_set);
	FD_ZERO(&out_set);
	FD_ZERO(&exc_set);

	maxdesc = 0;

	// set everything up for the select
	for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i)
	{
		NetSocket *pSock = *i;
		if ((pSock->m_deleteFlag&1) || pSock->m_sock == INVALID_SOCKET)
			continue;

		if (handleInput)
			FD_SET(pSock->m_sock, &inp_set);

		FD_SET(pSock->m_sock, &exc_set);

		if (pSock->VHasOutput())
			FD_SET(pSock->m_sock, &out_set);

		if ((int)pSock->m_sock > maxdesc)
			maxdesc = (int)pSock->m_sock;

	 }
  
	int selRet = 0;

	// do the select (duration passed in as tv, NULL to block until event)
	selRet = select(maxdesc+1, &inp_set, &out_set, &exc_set, &tv) ;
	if (selRet == SOCKET_ERROR)
	{
		PrintError();
		return;
	}

	// handle input, output, and exceptions

	if (selRet)
	{
		for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i)
		{
			NetSocket *pSock = *i;

			if ((pSock->m_deleteFlag&1) || pSock->m_sock == INVALID_SOCKET)
				continue;

			if (FD_ISSET(pSock->m_sock, &exc_set))
			{
				pSock->HandleException();
			}

			if (!(pSock->m_deleteFlag&1) && FD_ISSET(pSock->m_sock, &out_set))
			{
				pSock->VHandleOutput();
			}

			if (   handleInput
				&& !(pSock->m_deleteFlag&1) && FD_ISSET(pSock->m_sock, &inp_set))
			{
				pSock->VHandleInput();
			}
		 }	
	}

	unsigned int timeNow = GLUFGetTimeMs();

	// handle deleting any sockets
	SocketList::iterator i = m_SockList.begin();
	while (i != m_SockList.end())
	{
		NetSocket *pSock = *i;
		if (pSock->m_timeOut) 
		{
			if (pSock->m_timeOut < timeNow)
			{
				pSock->VTimeOut();
			}
		}

		if (pSock->m_deleteFlag&1)
		{
			switch (pSock->m_deleteFlag) 
			{
			  case 1:
					g_pSocketManager->RemoveSocket(pSock);
					i = m_SockList.begin();
					break;
				case 3:
					pSock->m_deleteFlag = 2;
					if (pSock->m_sock != INVALID_SOCKET) 
					{
						closesocket(pSock->m_sock);
						pSock->m_sock = INVALID_SOCKET;
					}
					break;
			}
		}

		++i;

	 }
 }