Exemplo n.º 1
0
	Bool HawkGateThread::CloseSession(SID iSid)
	{
		if (iSid)
		{
			SessionMap::iterator it = m_mSession.find(iSid);
			if (it != m_mSession.end())
			{
				//剔除会话列表
				Session* pSession = it->second;
				m_mSession.erase(it);

				//添加日志
				HawkFmtLog("SessionClose, SID: %u, Address: %s", pSession->Sid, pSession->Addr->ToString().c_str());

				//释放会话
				FreeSession(pSession);

				//调用性能监视器
				if (m_pGateway->GetProfiler())
					m_pGateway->GetProfiler()->RegConnect(false);

				//修改连接数
				m_pGateway->RegConnection(false);

				return true;
			}
		}		
		return false;
	}
void DTLS_Reset(NetworkAddress * address)
{
    DTLS_Session * session = GetSession(address);
    if (session)
    {
        FreeSession(session);
    }
}
Exemplo n.º 3
0
static void FreeSessions( void )
/******************************/
{
    session     *link;

    while( SessionList != NULL ) {
        link = SessionList->link;
        FreeSession( SessionList );
        SessionList = link;
    }
}
Exemplo n.º 4
0
	Bool HawkGateThread::FreeSessionMap()
	{
		SessionMap::iterator it = m_mSession.begin();
		for (;it!=m_mSession.end();it++)
		{
			Session* pSession = it->second;
			FreeSession(pSession);
		}
		m_mSession.clear();
		return true;
	}
Exemplo n.º 5
0
void SyncHandler::ProcessAcceptedSessionList()
{
	SESSION_LIST_ITER 	it;
	Session 		*pSession;
	
	m_pAcceptedSessionList->Lock();
	m_pTemplateList->splice( *m_pAcceptedSessionList );
	m_pAcceptedSessionList->Unlock();

	SESSION_LIST		activeList;
	while( !m_pTemplateList->empty() ) 
	{
		pSession = m_pTemplateList->pop_front();
		if (m_numActiveSessions >= m_dwMaxAcceptSession) {
			printf("Connection full! no available accept socket! \n");
			FreeSession(pSession);
			continue;
		}
		
		if (!AddEpollEvent(pSession)) {
			FreeSession(pSession);
			continue;
		}
		
		NetworkObject * pNet = m_fnCreateAcceptedObject();
		assert(pNet);
		
		pSession->BindNetworkObject(pNet);
		pSession->OnAccept();
		
		++m_numActiveSessions;
		activeList.push_back(pSession);
	}
	
	if ( !activeList.empty() ) {
		m_pActiveSessionList->Lock();
		m_pActiveSessionList->splice( activeList );
		m_pActiveSessionList->Unlock();
	}
}
Exemplo n.º 6
0
static void DeleteSession( HWND hwnd )
/************************************/
{
    session     **owner;
    session     *curr;

    owner = &SessionList;
    for(;;) {
        curr = *owner;
        if( curr->hwnd == hwnd ) break;
        owner = &curr->link;
    }
    *owner = curr->link;
    FreeSession( curr );
}
bool DTLS_Decrypt(NetworkAddress * sourceAddress, uint8_t * encrypted, int encryptedLength, uint8_t * decryptBuffer, int decryptBufferLength, int * decryptedLength, void *context)
{
    bool result = false;
    DTLS_Session * session = GetSession(sourceAddress);
    if (session)
    {
        session->Buffer = encrypted;
        session->BufferLength = encryptedLength;
        if (session->SessionEstablished)
        {
            *decryptedLength = gnutls_read(session->Session, decryptBuffer, decryptBufferLength);
            result = (*decryptedLength > 0);
            if (!result)
            {
                FreeSession(session);
                session = NULL;
            }
        }
        else
        {
            *decryptedLength = 0;
            session->SessionEstablished = (gnutls_handshake(session->Session) == GNUTLS_E_SUCCESS);
            if (session->SessionEstablished)
                Lwm2m_Info("Session established");
        }
    }

    if (!session)
    {
        int index;
        for (index = 0;index < MAX_DTLS_SESSIONS; index++)
        {
            if (!sessions[index].Session)
            {
                SetupNewSession(index, sourceAddress, false);
                sessions[index].UserContext = context;
                gnutls_transport_set_push_function(sessions[index].Session, SSLSendCallBack);
                sessions[index].Buffer = encrypted;
                sessions[index].BufferLength = encryptedLength;
                sessions[index].SessionEstablished = (gnutls_handshake(sessions[index].Session) == GNUTLS_E_SUCCESS);
                break;
            }
        }
    }
    return result;
}
void DTLS_Shutdown(void)
{
    int index;
    for (index = 0;index < MAX_DTLS_SESSIONS; index++)
    {
        if (sessions[index].Session)
        {
            FreeSession(&sessions[index]);
        }
    }
    if (_CertCredentials)
    {
        gnutls_certificate_free_credentials(_CertCredentials);
        _CertCredentials = NULL;
    }
//  gnutls_dh_params_deinit(_DHParameters);
    gnutls_priority_deinit(_PriorityCache);
    gnutls_global_deinit();
}
Exemplo n.º 9
0
void SyncHandler::KickDeadSession()
{
	SESSION_LIST_ITER 	it, it2;
	Session 		*pSession;
	
	m_pActiveSessionList->Lock();
	for (it = m_pActiveSessionList->begin(); it != m_pActiveSessionList->end();)
	{
		pSession = *it;
		if ( pSession->ShouldBeRemoved() ) {
			it2 = it++;
			m_pActiveSessionList->remove( it2 );
			m_pTemplateList->push_back( pSession );
		}
		else {
			++it;
		}
	}
	m_pActiveSessionList->Unlock();
	
	while ( !m_pTemplateList->empty() ) {
		Session * pSession = m_pTemplateList->pop_front();
		
		--m_numActiveSessions;
		NetworkObject * pNet = pSession->GetNetworkObject();
		
		pSession->UnbindNetworkObject();
		DelEpollEvent( pSession );
		FreeSession( pSession );
		
		pNet->OnDisconnect();
		if ( pSession->IsAcceptSocket() ) {
			m_fnDestroyAcceptedObject(pNet);
		}
		else {
			m_fnDestroyConnectedObject(pNet);
		}
	}
}
int LSelectServer::CheckForSocketEvent()
{
	if (!m_SelectServerSessionManager.BuildSet())
	{
		return -1;
	}

	fd_set rdset = m_SelectServerSessionManager.GetRecvSet();
	fd_set wrset = m_SelectServerSessionManager.GetSendSet();
	map<int, LSelectServerSession*>* pMapSocket = m_SelectServerSessionManager.GetSessionManagerForEvent();

	struct timeval val;
	val.tv_sec 		= 0;
	val.tv_usec 	= 0;
	int iReturn = select(m_SelectServerSessionManager.GetMaxSocketPlus1(), &rdset, &wrset, NULL, &val);	
	if (iReturn == -1)
	{
		return -1;
	}
	else if (iReturn == 0)
	{
		return 0;
	}
	int nListenSocket = GetSocket(); 
	if (FD_ISSET(nListenSocket, &rdset))
	{
		int nAcceptedSocket = accept(nListenSocket, NULL, NULL);
		if (nAcceptedSocket != -1)
		{
			LSelectServerSession* pSession = AllocSession();
			if (pSession == NULL)
			{
#ifndef WIN32
				close(nAcceptedSocket);
#else
				closesocket(nAcceptedSocket);
#endif
			}
			else
			{
				pSession->SetSocket(nAcceptedSocket);
				if (!AddSession(pSession))
				{
#ifndef WIN32
					close(nAcceptedSocket);
#else
					closesocket(nAcceptedSocket);
#endif
					FreeSession(pSession);
				}
				else
				{ 
					AddOneRecvedPacket(pSession->GetSessionID(), (LPacketSingle*)0xFFFFFFFF);
				}
			}
		}
	}
	map<int, LSelectServerSession*>::iterator _ito = pMapSocket->begin();
	while (_ito != pMapSocket->end())
	{
		int nTempSocket = _ito->first;
		LSelectServerSession* pSelectServerSession = _ito->second;
		if (pSelectServerSession == NULL)
		{
			_ito++;
			continue;
		}
		if (FD_ISSET(nTempSocket, &rdset))
		{
			int nErrorID = pSelectServerSession->SessionRecv();
			if (nErrorID < 0)
			{
				pSelectServerSession->SetSendable(false);
				AddOneCloseSessionWork(pSelectServerSession->GetSessionID());
				_ito++;
				continue;
			}
		}
		if (FD_ISSET(nTempSocket, &wrset))
		{
			pSelectServerSession->SetSendable(true);
		}
		_ito++;
	}

	return 1; 
}
int LSelectServer::ThreadDoing(void* pParam)
{ 
	while (1)
	{
		unsigned short usProcessedCloseWorkCount = 0;
		unsigned int unSessionIDForClose = 0;
		while (GetOneCloseSessionWork(unSessionIDForClose))
		{
			LSelectServerSession* pSession = FindSession(unSessionIDForClose);	
			if (pSession != NULL)
			{
				bool bReconnect = pSession->GetReconnect();
				pSession->ReleaseAllPacketInSendQueue();
#ifndef WIN32
				close(pSession->GetSocket());
#else
				closesocket(pSession->GetSocket());
#endif
				RemoveSession(unSessionIDForClose);
				FreeSession(pSession);
				AddOneRecvedPacket(unSessionIDForClose, NULL);
				if (bReconnect)
				{
					char szIP[32];
					unsigned short usPort = 0;
#ifndef WIN32
					pthread_mutex_lock(&m_mutexForConnectToServer);
#else
					EnterCriticalSection(&m_mutexForConnectToServer);
#endif
					if (!m_ConnectorWorkManager.AddConnectWork(szIP, usPort, NULL, 0))
					{
						//	error print
					}
#ifndef WIN32
					pthread_mutex_unlock(&m_mutexForConnectToServer);
#else
					LeaveCriticalSection(&m_mutexForConnectToServer);
#endif
				}
			}
			usProcessedCloseWorkCount++;
			if (usProcessedCloseWorkCount >= 200)
			{
				break;
			}
		}
		t_Connector_Work_Result cwr;
		memset(&cwr, 0, sizeof(cwr));
		int nProcessConnecttedWorkCount = 0;
		while (GetOneConnectedSession(cwr))
		{ 
			LSelectServerSession* pSession = AllocSession();
			if (pSession == NULL)
			{
#ifndef WIN32
				close(cwr.nSocket);
				pthread_mutex_lock(&m_mutexForConnectToServer);
#else
				closesocket(cwr.nSocket);
				EnterCriticalSection(&m_mutexForConnectToServer);
#endif

				
				if (!m_ConnectorWorkManager.AddConnectWork(cwr.szIP, cwr.usPort, NULL, 0))
				{
					//	error print
				}
#ifndef WIN32
				pthread_mutex_unlock(&m_mutexForConnectToServer);
#else
				LeaveCriticalSection(&m_mutexForConnectToServer);
#endif
				nProcessConnecttedWorkCount++;
				if (nProcessConnecttedWorkCount >= 200)
				{
					break;
				}
				continue;
			}
			else
			{
				pSession->SetSocket(cwr.nSocket);
				if (!AddSession(pSession))
				{
#ifndef WIN32
					close(cwr.nSocket);
					pthread_mutex_lock(&m_mutexForConnectToServer);
#else
					closesocket(cwr.nSocket);
					EnterCriticalSection(&m_mutexForConnectToServer);
#endif
					
					if (!m_ConnectorWorkManager.AddConnectWork(cwr.szIP, cwr.usPort, NULL, 0))
					{
						//	error print
					}
#ifndef WIN32
					pthread_mutex_unlock(&m_mutexForConnectToServer);
#else
					LeaveCriticalSection(&m_mutexForConnectToServer);
#endif
					FreeSession(pSession);
				}
				else
				{ 
					pSession->SetReconnect(true);
					AddOneRecvedPacket(pSession->GetSessionID(), (LPacketSingle*)0xFFFFFFFF);
				}
			}
			nProcessConnecttedWorkCount++;
			if (nProcessConnecttedWorkCount >= 200)
			{
				break;
			}
		}

		int nWorkCount = CheckForSocketEvent();
		if (nWorkCount < 0)
		{
			return 0;
		}
		int nSendWorkCount = SendData();
		if (nWorkCount == 0 && nSendWorkCount == 0)
		{
			//	sched_yield();
#ifndef WIN32
			struct timespec timeReq;
			timeReq.tv_sec 	= 0;
			timeReq.tv_nsec = 10;
			nanosleep(&timeReq, NULL);
#else
			Sleep(0);
#endif
		}
		if (CheckForStop())
		{
			return 0;
		} 
	}
	return 0; 
}