示例#1
0
void SessionManager::onAcceptNewClient(zsummer::network::NetErrorCode ec, const TcpSocketPtr& s, const TcpAcceptPtr &accepter, AccepterID aID)
{
    if (!_running ||  _stopAccept)
    {
        LCI("shutdown accepter. aID=" << aID);
        return;
    }
    auto founder = _mapAccepterOptions.find(aID);
    if (founder == _mapAccepterOptions.end())
    {
        LCE("Unknown AccepterID aID=" << aID);
        return;
    }
    if (ec)
    {
        LCE("doAccept Result Error. ec=" << ec << ", extend=" << founder->second);
        
        auto &&handler = std::bind(&SessionManager::onAcceptNewClient, this, std::placeholders::_1, std::placeholders::_2, accepter, aID);
        auto timer = [accepter, handler]()
        {
            accepter->doAccept(std::make_shared<TcpSocket>(), std::move(handler));
        };
        createTimer(5000, std::move(timer));
        return;
    }

    std::string remoteIP;
    unsigned short remotePort = 0;
    s->getPeerInfo(remoteIP, remotePort);
    
    //! check white list
    //! ---------------------
    if (!founder->second._whitelistIP.empty())
    {
        bool checkSucess = false;
        for (auto white : founder->second._whitelistIP)
        {
            if (remoteIP.size() >= white.size())
            {
                if (remoteIP.compare(0,white.size(), white) == 0)
                {
                    checkSucess = true;
                    break;
                }
            }
        }

        if (!checkSucess)
        {
            LCW("Accept New Client Check Whitelist Failed remoteAdress=" << remoteIP << ":" << remotePort
                << ", extend=" << founder->second);
            accepter->doAccept(std::make_shared<TcpSocket>(), std::bind(&SessionManager::onAcceptNewClient, this, std::placeholders::_1, std::placeholders::_2, accepter, aID));
            return;
        }
        else
        {
            LCI("Accept New Client Check Whitelist Success remoteAdress=" << remoteIP << ":" << remotePort
                << ", extend=" << founder->second);
        }
    }
    
    //! check Max Sessions
    if (founder->second._currentLinked >= founder->second._maxSessions)
    {
        LCW("Accept New Client. Too Many Sessions And The new socket will closed. extend=" << founder->second );
    }
    else
    {
        LCD("Accept New Client. Accept new Sessions. The new socket  remoteAddress=" << remoteIP << ":" << remotePort 
            << ", Aready linked sessions = " << founder->second._currentLinked << ", extend=" << founder->second);
        founder->second._currentLinked++;
        founder->second._totalAcceptCount++;

        _lastSessionID = nextSessionID(_lastSessionID);
        s->initialize(_summer);

        TcpSessionPtr session = std::make_shared<TcpSession>();
        session->getOptions() = founder->second._sessionOptions;
        session->setEventLoop(_summer);
        if (session->attatch(s, aID, _lastSessionID))
        {
            _mapTcpSessionPtr[_lastSessionID] = session;
        }
    }
    
    //! accept next socket.
    accepter->doAccept(std::make_shared<TcpSocket>(), std::bind(&SessionManager::onAcceptNewClient, this, std::placeholders::_1, std::placeholders::_2, accepter, aID));
}
示例#2
0
void SessionManager::onAcceptNewClient(zsummer::network::NetErrorCode ec, const TcpSocketPtr& s, const TcpAcceptPtr &accepter, AccepterID aID)
{
	if (!_running || ! _openAccept)
	{
		LCI("shutdown accepter. aID=" << aID);
		return;
	}
	auto iter = _mapAccepterConfig.find(aID);
	if (iter == _mapAccepterConfig.end())
	{
		LCE("Unknown AccepterID aID=" << aID);
		return;
	}
	if (ec)
	{
		LCE("doAccept Result Error. ec=" << ec << ", traits=" << iter->second.first);
		
		TcpSocketPtr newclient(new zsummer::network::TcpSocket);
		newclient->initialize(_summer);
		auto &&handler = std::bind(&SessionManager::onAcceptNewClient, this, std::placeholders::_1, std::placeholders::_2, accepter, aID);
		auto timer = [accepter, newclient, handler]()
		{
			accepter->doAccept(newclient, std::move(handler));
		};
		createTimer(5000, std::move(timer));
		return;
	}

	std::string remoteIP;
	unsigned short remotePort = 0;
	s->getPeerInfo(remoteIP, remotePort);
	
	//! check white list
	//! ---------------------
	if (!iter->second.first._whitelistIP.empty())
	{
		bool checkSucess = false;
		for (auto white : iter->second.first._whitelistIP)
		{
			if (remoteIP.size() >= white.size())
			{
				if (remoteIP.compare(0,white.size(), white) == 0)
				{
					checkSucess = true;
					break;
				}
			}
		}

		if (!checkSucess)
		{
			LCW("Accept New Client Check Whitelist Failed remoteAdress=" << remoteIP << ":" << remotePort
				<< ", trais=" << iter->second.first);

			TcpSocketPtr newclient(new zsummer::network::TcpSocket);
			newclient->initialize(_summer);
			accepter->doAccept(newclient, std::bind(&SessionManager::onAcceptNewClient, this, std::placeholders::_1, std::placeholders::_2, accepter, aID));
			return;
		}
		else
		{
			LCI("Accept New Client Check Whitelist Success remoteAdress=" << remoteIP << ":" << remotePort
				<< ", trais=" << iter->second.first);
		}
	}
	
	//! check Max Sessions

	if (iter->second.second._currentLinked >= iter->second.first._maxSessions)
	{
		LCW("Accept New Client. Too Many Sessions And The new socket will closed. remoteAddress=" << remoteIP << ":" << remotePort 
			<< ", Aready linked sessions = " << iter->second.second._currentLinked << ", trais=" << iter->second.first);
	}
	else
	{
		LCD("Accept New Client. Accept new Sessions. The new socket  remoteAddress=" << remoteIP << ":" << remotePort 
			<< ", Aready linked sessions = " << iter->second.second._currentLinked << ", trais=" << iter->second.first);
		iter->second.second._currentLinked++;
		iter->second.second._totalAcceptCount++;

		_lastSessionID = nextSessionID(_lastSessionID);
		TcpSessionPtr session(new TcpSession());
		s->initialize(_summer);
		if (session->bindTcpSocketPrt(s, aID, _lastSessionID, iter->second.first))
		{
			_mapTcpSessionPtr[_lastSessionID] = session;
			_totalAcceptCount++;
			MessageDispatcher::getRef().dispatchOnSessionEstablished(session);
		}
	}
	
	//! accept next socket.
	TcpSocketPtr newclient(new zsummer::network::TcpSocket);
	newclient->initialize(_summer);
	accepter->doAccept(newclient, std::bind(&SessionManager::onAcceptNewClient, this, std::placeholders::_1, std::placeholders::_2, accepter,aID));
}