Example #1
0
void CMultiXL2Link::OnAcceptCompleted(CMultiXL2Link &Listener)
{
	int	IoError	=	MultiXNoError;
	m_LinkType		=	Listener.m_LinkType;
	m_MaxReadSize	=	Listener.m_MaxReadSize;
	m_OpenMode		=	MultiXOpenModeAccept;
#ifdef	OPENSSL_SUPPORT
	if(Listener.m_pOpenSSLIF)
	{
		IoError	=	Listener.m_pOpenSSLIF->Accept(&m_pOpenSSLIF);
	}
#endif
	
	GetConnectionParams(m_LocalName,m_LocalPort,m_RemoteName,m_RemotePort);
	Connected()	=	true;
	ConnectPending()	=	false;
	WritePending()	=	false;
	ReadPending()	=	false;

	CMultiXL2Event	*Event	=	new	CMultiXL2Event(CMultiXEvent::L2AcceptCompleted,Owner(),Owner()->UpperLayer());
	Event->L2LinkID()	=	Listener.ID();
	Event->IoError()	=	IoError;
	if(IoError	==	MultiXNoError)
	{
		Event->NewL2LinkID()	=	ID();
		Read();
	}	else
	{
		Close();
	}
	Owner()->MultiX().QueueEvent(Event);

	Read();
}
Example #2
0
void CMultiXL2Link::OnConnectCompleted(MultiXError IoError)
{
	if(IoError	==	MultiXNoError)
	{
		if(!ClosePending())
		{
			GetConnectionParams(m_LocalName,m_LocalPort,m_RemoteName,m_RemotePort);
			Connected()	=	true;
			ConnectPending()	=	false;
			WritePending()	=	false;
			ReadPending()	=	false;
#ifdef	OPENSSL_SUPPORT
			if(m_pOpenSSLIF)
			{
				IoError	=	m_pOpenSSLIF->Connect(m_RemoteName);
			}
#endif
		}	else
			return;
	}
	CMultiXL2Event	*Event	=	new	CMultiXL2Event(CMultiXEvent::L2ConnectCompleted,Owner(),Owner()->UpperLayer());
	Event->L2LinkID()	=	ID();
	Event->IoError()	=	IoError;
	Owner()->MultiX().QueueEvent(Event);
	if(IoError	==	0)
		Read();
}
unsigned Listener::Process()
{
	//Profile profile("Listener::Process");
    ////////////////////////////////////////
    //  handle inactive state (with UdpManager)
    if (!IsActive() && mTcpManager)
    {
		//	check all connections to see if they are idle
		std::set<Connection *>::iterator iterator;
		for (iterator = mConnections.begin(); iterator != mConnections.end(); iterator++)
		{
			Connection * connection = *iterator;
			if (connection->IsConnected())
				connection->Disconnect();
		}
		//	close the UdpManager if all the connections are closed
		if (!mConnectionCount)
		{
			mTcpManager->Release();
			mTcpManager = 0;
			OnShutdown();
		}
    }
    ////////////////////////////////////////
    //  handle active state (without UdpManager)
    else if (IsActive() && !mTcpManager)
    {
		mParams = GetConnectionParams();
        mActiveMax = GetActiveRequestMax();
        mTcpManager = new TcpManager(mParams);
		mTcpManager->SetHandler(this);
		if (mTcpManager->BindAsServer()){
            OnStartup();
        }else{
            OnFailedStartup();
            return 0;
        }
    }

    ////////////////////////////////////////
    //  process the TcpManager
    if (mTcpManager)
    {
		//Profile subProfile("TcpManager::GiveTime()");
        mTcpManager->GiveTime();
    }

	//	check all closed connections to see if they are idle
	std::list<Connection *>::iterator closedIterator = mClosedConnections.begin();
	while (closedIterator != mClosedConnections.end())
	{
		//Profile profile("Listener::Process (cleanup connection)");
		std::list<Connection *>::iterator current = closedIterator++;
		Connection * connection = *current;
		if (!connection->GetActiveRequests() && 
			!connection->GetQueuedRequests())
		{
			mClosedConnections.erase(current);
			mConnections.erase(connection);
			mConnectionCount--;
			OnConnectionDestroyed(connection);
			delete connection;
		}
	}

    ////////////////////////////////////////
    //  process request queue
    while (!mQueuedRequests.empty() && (!mActiveMax || mActiveCount < mActiveMax))
    {
		//Profile profile("Listener::Process (activate queued request)");
        QueueNode & node = mQueuedRequests.front();
        if (!IsActive())
        {
            //  If not active, discard queued request
            if (node.connection)
            {
                // normal request, internal requests have no connection
                node.connection->NotifyDiscardRequest(node.request);
            }
            DestroyRequest(node.request);
        }
        else
        {
            //  Move request to active list
            if (node.connection)
            {
                // normal request, internal requests have no connection
                node.connection->NotifyBeginRequest(node.request);
            }
            mActiveRequests.push_back(node);
			mActiveCount++;
        }
        mQueuedRequests.pop_front();
    }

    ////////////////////////////////////////
    //  Process active requests
    unsigned requestsProcessed = 0;
    std::list<QueueNode>::iterator iterator = mActiveRequests.begin();
    while (iterator != mActiveRequests.end())
    {
		//Profile profile("Listener::Process (process request)");
        std::list<QueueNode>::iterator current = iterator++;
        RequestBase * request = current->request;
        Connection * connection = current->connection;
    
        if (request->Process())
        {
            if (connection)
            {
                // normal request, internal requests have no connection
                connection->NotifyEndRequest(request);
            }
            DestroyRequest(request);
            mActiveRequests.erase(current);
			mActiveCount--;
        }
		else if (mSleepingRequests.find(request) != mSleepingRequests.end())
		{
            mActiveRequests.erase(current);
		}
	    requestsProcessed++;
    }
    return requestsProcessed;
}