コード例 #1
0
BOOL LoginPlayerManager::ProcessInputs( )
{
    __ENTER_FUNCTION

    BOOL ret = FALSE ;

    if (m_MinFD == INVALID_SOCKET && m_MaxFD == INVALID_SOCKET) // no player exist
    {
        return TRUE ;
    }

    //新连接接入:
    if( FD_ISSET(m_SocketID,&m_ReadFDs[SELECT_USE]) )
    {
        for( INT i=0; i<ACCEPT_ONESTEP; i++ )
        {
            if( !AcceptNewConnection() )
                break;
        }
    }

    //数据读取
    uint nPlayerCount = GetPlayerNumber() ;
    for( uint i=0; i<nPlayerCount; i++ )
    {
        if( m_pPlayers[i]==INVALID_ID )
            continue ;

        LoginPlayer* pPlayer = g_pPlayerPool->GetPlayer(m_pPlayers[i]) ;
        Assert( pPlayer ) ;

        SOCKET s = pPlayer->GetSocket()->getSOCKET() ;
        if( s == m_SocketID )
            continue ;

        if( pPlayer->GetSocket()->isSockError() )
        {   //连接出现错误
            RemovePlayer( pPlayer ) ;
        }
        else if( FD_ISSET( s, &m_ReadFDs[SELECT_USE] ) )
        {   //连接正常
            _MY_TRY
            {
                ret = pPlayer->ProcessInput( ) ;
                if( !ret )
                {
                    RemovePlayer( pPlayer ) ;
                }
            }
            _MY_CATCH
            {
                RemovePlayer( pPlayer ) ;
            }
        }
    }
コード例 #2
0
bool NetworkManager::StartNetwork(boost::uint16_t port, std::string address)
{
    if (running_)
        return false;
    
    address_ = address;
    port_ = port;
    
    if (network_threads_count_ <= 0)
    {
        sLog.outError("Number of network threads is incorrect = " SIZEFMTD,
                      network_threads_count_);
        return false;
    }

    network_threads_count_ += 1;
    network_threads_.reset(new NetworkThread[network_threads_count_]);

    try
    {
        protocol::Endpoint listen_address(protocol::IPAddress::from_string(address_), port_);
        acceptor_.reset(new protocol::Acceptor(get_acceptor_thread().service(), listen_address));
    }
    catch (boost::system::error_code&)
    {
        sLog.outError("Failed to open acceptor, check if the port is free");
        return false;
    }

    running_ = true;

    AcceptNewConnection();

    std::string threadName = "\"" + m_managerName + "\" Acceptor";
    network_threads_[0].SetName(threadName);
    threadName = "\"" + m_managerName + "\" Service";

    for (size_t i = 0; i < network_threads_count_; ++i)
    {
        // set thread name
        if (i > 0)
        {
            std::string tname = threadName + std::to_string(i);
            network_threads_[i].SetName(threadName);
        }
        
        network_threads_[i].Start();        
    }
    
    return true;
}
コード例 #3
0
void NetworkManager::OnNewConnection(SocketPtr connection, const boost::system::error_code& error)
{
    if (error)
    {
        sLog.outError("Error accepting new client connection!");
        return;
    }
    
    if (!connection->Open())
    {
        sLog.outError("Unable to start new client connection!");

        connection->CloseSocket();
        return;
    }

    AcceptNewConnection();
}
コード例 #4
0
void CodeLiteLLDBApp::MainLoop()
{
    try {
        AcceptNewConnection();
        // We got both ends connected
        wxPrintf("codelite-lldb: successfully established connection to codelite\n");

        while(!m_exitMainLoop) {
            CodeLiteLLDBApp::QueueItem_t msg;
            CodeLiteLLDBApp::NotifyFunc_t notify_func = NULL;

            bool got_something = false;
            if(m_commands_queue.ReceiveTimeout(1, msg) == wxMSGQUEUE_NO_ERROR) {
                // Process the command
                CodeLiteLLDBApp::CommandFunc_t pFunc = msg.first;
                LLDBCommand command = msg.second;
                (this->*pFunc)(command);

                got_something = true;
            }

            if(m_notify_queue.ReceiveTimeout(1, notify_func) == wxMSGQUEUE_NO_ERROR) {

                (this->*notify_func)();
                got_something = true;
            }

            if(!got_something) {
                wxThread::Sleep(10);
            }
        }
        wxPrintf("codelite-lldb: terminating\n");

    } catch(clSocketException& e) {
        wxPrintf(
            "codelite-lldb: an error occurred during MainLoop(). %s. strerror=%s\n", e.what().c_str(), strerror(errno));
        // exit now
        exit(0);
    }
}
コード例 #5
0
ファイル: ndaseventpub.cpp プロジェクト: yzx65/ndas4windows
DWORD
CNdasEventPublisher::OnTaskStart()
{
	_ASSERTE(NULL != m_hSemQueue && "Don't forget to call initialize().");

	// Queue Semaphore, Terminating Thread, Pipe Instances(MAX...)
	HANDLE hWaitHandles[2 + MAX_NDAS_EVENT_PIPE_INSTANCES];
	hWaitHandles[0] = m_hTaskTerminateEvent;
	hWaitHandles[1] = m_hSemQueue;

	//
	// initial pipe instance
	//
	m_PipeData.clear();
	BOOL fSuccess = AcceptNewConnection();
	if (!fSuccess) {
		DPErrorEx(_T("Creating a first pipe instance failed: "));
		return -1;
	}

	BOOL bTerminate(FALSE);

	while (FALSE == bTerminate) {

		DWORD dwWaitHandles = 2 + m_PipeData.size();
		for (DWORD i = 0; i < m_PipeData.size(); ++i) {
			hWaitHandles[i + 2] = m_PipeData[i]->overlapped.hEvent;
		}

		DWORD dwWaitResult = ::WaitForMultipleObjects(
			dwWaitHandles,
			hWaitHandles,
			FALSE,
			m_dwPeriod);


		if (dwWaitResult == WAIT_OBJECT_0) 
		{
			//
			// Terminate Thread
			//
			bTerminate = TRUE;
		} 
		else if (dwWaitResult == WAIT_OBJECT_0 + 1) 
		{
			//
			// Event Message is queued
			//
			while (TRUE) {
				m_queueLock.Lock();
				bool bEmpty = m_EventMessageQueue.empty();
				if (bEmpty) {
					m_queueLock.Unlock();
					break;
				}
				NDAS_EVENT_MESSAGE message = m_EventMessageQueue.front();
				m_EventMessageQueue.pop();
				m_queueLock.Unlock();
				Publish(&message);
			}

		} 
		else if (dwWaitResult >= WAIT_OBJECT_0 + 2 && 
			dwWaitResult < WAIT_OBJECT_0 + 2 + m_PipeData.size())
		{
			DWORD dwPipe = dwWaitResult - WAIT_OBJECT_0 - 2;

			DPInfo(_FT("Event Client %d\n"), dwPipe);

			CLIENT_DATA* pCurClientData = m_PipeData[dwPipe];

			DPInfo(_FT("Connected: %d\n"), pCurClientData->bConnected);

			fSuccess = ::ResetEvent(pCurClientData->overlapped.hEvent);
			_ASSERT(fSuccess);

			if (!pCurClientData->bConnected) {
				
				//
				// create another instance
				//
				fSuccess = AcceptNewConnection();
				if (!fSuccess) {
					DPWarningEx(_FT("Creating another pipe instance failed: "));
					DPWarning(_FT("No more event subscribers can be accepted.\n"));
				}

				// AcceptNewConnection will invalidate pCurClientData;

				pCurClientData = m_PipeData.at(dwPipe);

				//
				// Accepting connection
				//
				pCurClientData->bConnected = TRUE;
				fSuccess = ::ResetEvent(pCurClientData->overlapped.hEvent);
				_ASSERT(fSuccess);
				
				//
				// Send a version event for connected client
				//
				fSuccess = SendVersionInfo(
					pCurClientData->hPipe, 
					&pCurClientData->overlapped);

				//
				// Any failure will disconnect the client
				//
				if (!fSuccess && ERROR_IO_PENDING != ::GetLastError()) {
					ClientDataVector::iterator itr = m_PipeData.begin();
					CleanupConnection(pCurClientData);
					while (itr != m_PipeData.end()) {
						if ((CLIENT_DATA*)*itr == pCurClientData) {
							m_PipeData.erase(itr);
							break;
						}
						++itr;
					}
					DPInfo(_FT("Accepted removed event subscriber.\n"));
				} else {
					DPInfo(_FT("Accepted new event subscriber.\n"));
				}

			} else {
			}
			// ignore other status
		} else if (WAIT_TIMEOUT == dwWaitResult) {
			NDAS_EVENT_MESSAGE msg = {0};
			msg.EventType = NDAS_EVENT_TYPE_PERIODIC;
			Publish(&msg);
		} else 
		{
			//
			// Error
			//
		}

	}

	//
	// TODO: Add cleanup
	//
	DWORD nPipeData = m_PipeData.size();
	ClientDataVector::iterator itr = m_PipeData.begin();
	while (itr != m_PipeData.end()) {
		CleanupConnection(*itr);
		++itr;
	}
	m_PipeData.clear();

	_tprintf(TEXT("Terminating Publisher Thread...\n"));
	return 0;
}
コード例 #6
0
ファイル: ndaseventpub.cpp プロジェクト: yzx65/ndas4windows
void 
CNdasEventPublisher::Publish(const PNDAS_EVENT_MESSAGE pMessage)
{
	DPInfo(_FT("Publishing Event: %s\n"), 
		NdasEventTypeString(pMessage->EventType));

	//
	// sent the message to the connected pipes
	//
	for (ClientDataVector::iterator itr = m_PipeData.begin(); 
		itr != m_PipeData.end();) 
		//
		// do not forward the iterator here when erasing some 
		// elements 
		// itr2 = v.erase(itr); 
		// itr2 has a forwarded iterator from itr
		//
	{
		CLIENT_DATA* pClientData = *itr;
		if (pClientData->bConnected) {

			DWORD cbWritten;

			BOOL fSuccess = ::WriteFile(
				pClientData->hPipe,
				pMessage,
				sizeof(NDAS_EVENT_MESSAGE),
				&cbWritten,
				&pClientData->overlapped);

			if (!fSuccess && ERROR_IO_PENDING != ::GetLastError()) {
				
				DPErrorEx(_FT("Writing to a pipe failed: "));
				DPInfo(_FT("Detaching an event subscriber.\n"));
				
				CleanupConnection(pClientData);
				//
				// erasing an element will automatically
				// forward the vector 
				// (actually, itr remains the same, but the itr is
				// a next elemen)
				itr = m_PipeData.erase(itr);

				//
				// create another instance
				//
				fSuccess = AcceptNewConnection();
				if (!fSuccess) {
					DPWarningEx(_FT("Creating another pipe instance failed: "));
					DPWarning(_FT("No more event subscribers can be accepted.\n"));
				}

			} else {
				//
				// forward the iterator if we did erase
				//
				DPInfo(_FT("Event written to a pipe %p.\n"), (*itr)->hPipe);
				++itr;
			}
		} else {
			//
			// forward the iterator if not connected
			//
			++itr;
		}
	}	
}
コード例 #7
0
bool	ConnectorManager_Client::Handle_Inputs()
{
	try
	{
		m_nEvents = epoll_wait(m_Epollfd, events, m_uEventsMaxSize, 0);
		if(m_nEvents > m_uEventsMaxSize || m_nEvents < 0)
		{
			WriteLog(KERN_ERR "epoll wait Error ! m_nEvents=%d", m_nEvents);
		}

	}
	catch(...)
	{
		WriteLog(KERN_ERR "ConnectorManager_Client::epoll_wait(): Error ! m_nEvents=%d", m_nEvents);
	}

	bool ret = false;

	for(int i = 0; i < m_nEvents; i++)
	{

		int32_t SockFD = GetHighSection(events[i].data.u64);
		ConnectorID_t idx = GetLowSection(events[i].data.u64);

		if(SockFD == m_SocketID)
		{
			if( m_bServerMode )
			{
				for(int32_t i = 0; i < ACCEPT_ONESTEP; i++)
				{
					if(AcceptNewConnection()==NULL) 
						break;
				}
			}
		}
		else if( events[i].events & EPOLLIN )
		{
			Connector* pConnector = GetConnector(idx);

			if( pConnector == NULL )
			{
				WriteLog(KERN_ERR "ConnectorManager_Epoll::ProcessInputs(): Error ! pConnector==NULL,id=%d", idx );
				continue;
			}

			if( pConnector->IsDisconnect() )
				continue;

			int fd = pConnector->GetSocket()->getFD();

			if(fd == INVALID_SOCKET)
			{
				WriteLog(KERN_ERR "ConnectorManager_Epoll::ProcessInputs(): Error ! socket==INVALID" );
				continue;
			}

			if(pConnector->GetSocket()->isSockError())
			{
				RemoveConnector(pConnector);
			}
			else
			{
				_NET_TRY
				{
					ret = pConnector->Handle_Input();
					if(!ret)
					{
						RemoveConnector(pConnector);
					}
				}
				_NET_CATCH
				{
					RemoveConnector(pConnector);
				}
			}
		}
	}