示例#1
0
void SessionList::ValidCheck(DWORD TickTime)
{
	UNREFERENCED_PARAMETER(TickTime);

	AutoMutex mutex(&_mutex);
	mutex.Lock();

	Session* pSession = NULL;
	for (LISTIT it = _sessionList.Begin(); it != _sessionList.End();)
	{
		pSession = *(_sessionList.GetPtr(it));

		if (pSession)
		{
			if (false == pSession->ValidCheck(TickTime))
			{
				Logger::Log("The session[%X] should be disconnected due to timeout.\n", pSession);
				pSession->Disconnect(false);
			}

			if (true == pSession->IsShutdownable())
			{
				pSession->Shutdown();
				_NetworkRef->PostNetEventMessage((WPARAM)NETEVENT_CLOSE, (LPARAM)pSession);
				it = _sessionList.Remove(it);
				continue;
			}
		}

		it = _sessionList.Next(it);
	}

}
示例#2
0
void IOCPPool::HandleRecv(Session& ikey)
{
	if(ikey.m_nPendingRecv.get()==0)
	{
		return;
	}

	for(;;)
	{
		TempOlapPtr q=ikey.lkfq_recv.getq();

		if(!q)
		{
			break;
		}

		if(q->type==MyOverLapped::ACTION_WAIT_RECV)
		{
			ikey.m_nPendingRecv--;
			ikey.OnRecvReady();
			break;
		}


		if(q->type==MyOverLapped::ACTION_UDP_RECV)
		{
			int bRet=ikey.sk_local.sock.recv(q->dbuf[0].buf,q->dbuf[0].len,q->peer);
			if(bRet>0)
			{
				q->size=bRet;
				accounter.nRecvBytes.fetch_add(bRet);
				accounter.nSendCount++;

				ikey.m_nPendingRecv--;
				ikey.OnRecvCompleted(q);
				break;
			}
			else if(bRet<0)
			{
				ikey.m_nPendingRecv--;
				ikey.Disconnect();
				break;
			}
			else
			{
				ikey.m_nPendingRecv--;
				this_logger().LogMessage("recv udp failed");
			}
			break;
		}

		q->size=0;

		for(;;)
		{

			int bRet=ikey.sk_local.sock.recv(q->dbuf[0].buf+q->size,q->dbuf[0].len-q->size);
			if(bRet<0)
			{
				if(errno==EAGAIN)
				{
					break;
				}
				if(errno==EINTR)
				{
					continue;
				}

				ikey.Disconnect();
				break;
			}
			else if(bRet==0)
			{
				break;
			}

			q->size+=bRet;
			break;
		}

		if(q->size>0)
		{
			accounter.nRecvCount++;
			accounter.nRecvBytes.fetch_add(q->size);
			ikey.m_nPendingRecv--;
			ikey.OnRecvCompleted(q);
		}
		else
		{
			ikey.m_nPendingRecv--;
		}

		break;

	}

}
示例#3
0
void IOCPPool::HandleSend(Session& ikey)
{

	if(ikey.m_nPendingSend.get()==0)
	{
		return;
	}

	TempOlapPtr q=ikey.tmp_send;

	for(;; q.reset(NULL))
	{
		if(!q)
		{
			q=ikey.lkfq_send.getq();
			if(!q) break;
			q->size=0;

			if(q->type==MyOverLapped::ACTION_WAIT_SEND)
			{
				ikey.m_nPendingSend--;
				ikey.OnSendReady();
				ikey.lkfq_free.putq(q);
				continue;
			}
		}

		if(q->type==MyOverLapped::ACTION_UDP_SEND)
		{
			int bRet=ikey.sk_local.sock.send(q->dbuf[0].buf,q->dbuf[0].len,q->peer);
			if(bRet>0)
			{
				q->size=bRet;
				accounter.nSendBytes.fetch_add(q->size);
				accounter.nSendCount++;
				ikey.m_nPendingSend--;
				ikey.OnSendCompleted(q);
				continue;
			}
			else if(bRet==0)
			{
				ikey.tmp_send=q;
				break;
			}
			else
			{
				ikey.Disconnect();
				break;
			}
		}

		for(;;)
		{

			int bRet=ikey.sk_local.sock.send(q->dbuf[0].buf+q->size,q->dbuf[0].len-q->size);
			if(bRet<0)
			{
				if(errno==EAGAIN)
				{
					ikey.tmp_send=q;
					break;
				}
				if(errno==EINTR)
				{
					continue;
				}

				ikey.Disconnect();
				break;
			}
			else if(bRet==0)
			{
				ikey.tmp_send=q;
				return;
			}

			q->size+=bRet;

			if(q->size==q->dbuf[0].len)
			{
				accounter.nSendCount++;
				accounter.nSendBytes.fetch_add(q->size);
				ikey.m_nPendingSend--;
				ikey.OnSendCompleted(q);
				break;
			}

		}


	}

}