Beispiel #1
0
	/** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
	 * - in front of any nick removes, and an * lists. This effectively means you can do:
	 * /accept nick1,nick2,nick3,*
	 * to add 3 nicks and then show your list
	 */
	CmdResult Handle(const std::vector<std::string> &parameters, User* user)
	{
		if (CommandParser::LoopCall(user, this, parameters, 0))
			return CMD_SUCCESS;

		/* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
		 * have a list already setup. */

		if (parameters[0] == "*")
		{
			ListAccept(user);
			return CMD_SUCCESS;
		}

		std::string tok = parameters[0];
		ACCEPTAction action = GetTargetAndAction(tok, user);
		if (!action.first)
		{
			user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", tok.c_str());
			return CMD_FAILURE;
		}

		if ((!IS_LOCAL(user)) && (!IS_LOCAL(action.first)))
			// Neither source nor target is local, forward the command to the server of target
			return CMD_SUCCESS;

		// The second item in the pair is true if the first char is a '+' (or nothing), false if it's a '-'
		if (action.second)
			return (AddAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
		else
			return (RemoveAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
	}
bool EpollMonitor::Stop()
{
#ifndef WIN32
	if ( m_bStop ) return true;
	m_bStop = true;
	AddRecv(m_epollExit, NULL, 0);
	AddSend(m_epollExit, NULL, 0);
	AddAccept(m_epollExit);
	m_acceptThread.Stop();
	m_pollinThread.Stop();
	m_polloutThread.Stop();
#endif
	return true;
}
bool IOCPMonitor::WaitEvent( void *eventArray, int &count, bool block )
{
#ifdef WIN32
	IO_EVENT *events = (IO_EVENT*)eventArray;
	count = 0;
	/*
		完成操作上的传输长度
		发现accept操作完成时为0
		发现recv/send操作完成时,记录接收/发送字节数
		如果recv/send操作上的传输长度小于等于0表示套接字已关闭
	*/
	DWORD dwIOSize;
	IOCP_OVERLAPPED *pOverlapped = NULL;//接收完成数据
	if ( !::GetQueuedCompletionStatus( m_hCompletPort, &dwIOSize, 
						(LPDWORD)&events[count].sock, (OVERLAPPED**)&pOverlapped,
						INFINITE ) )
	{
		DWORD dwErrorCode = GetLastError();
		if ( ERROR_INVALID_HANDLE == dwErrorCode ) //启动时,未知原因非法句柄,可能是捕获到前一次程序关闭后未处理的完成事件
		{
			try
			{
				if ( NULL != pOverlapped )
				{
					m_iocpDataPool.Free(pOverlapped);
					pOverlapped = NULL;
					return true;
				}
			}catch(...)
			{
			}
			return true;
		}
		
		if ( ERROR_OPERATION_ABORTED == dwErrorCode ) 
		{
			if ( IOCPMonitor::connect == pOverlapped->completiontype ) 
			{
				m_iocpDataPool.Free(pOverlapped);
				pOverlapped = NULL;
				return false;
			}
		}
		if ( IOCPMonitor::connect == pOverlapped->completiontype ) //Accept上的socket关闭,重新投递监听
		{
			AddAccept(events[count].sock);
		}
		else//客户端异常断开,拔网线,断电,终止进程
		{
			events[count].type = IOCPMonitor::close;
			count++;
		}
	}
	else if ( 0 == dwIOSize && IOCPMonitor::recv == pOverlapped->completiontype )
	{
		events[count].type = IOCPMonitor::close;
		count++;
	}
	else//io事件
	{
		//io事件
		events[count].type = pOverlapped->completiontype;
		events[count].client = pOverlapped->sock;
		events[count].pData = pOverlapped->m_wsaBuffer.buf;
		events[count].uDataSize = dwIOSize;
		count++;
	}
	m_iocpDataPool.Free(pOverlapped);
	pOverlapped = NULL;
				
	return true;
	
#endif
	return true;
}