Exemplo n.º 1
0
int CRecvAndSendTask::HandleProcess(int events)
{        
        if ((events&MYPOLLERR) == MYPOLLERR || (events&MYPOLLHUP) == MYPOLLHUP)
        {
                WriteRunInfo::WriteLog("Recv a invalid data pack, event %d, close the socket %d error %d [threadtype = %d]", events, 
                        m_pTcpSocket->GetSocket(), errno, m_pThread->GetEntityType());
                CMyserver::GetInstance()->GetCommonStat().AddNormalStat(SOCKETERRORTYPE);
                DestroySelf();
                return 0;
        }
        //不断改变开始时间, 如果超过2分钟没有响应,就会自动断开 
        m_nStartTime = CMyserver::GetInstance()->GetTimeVal().tv_sec;
        
        int ret = 0;
        if ((events& MYPOLLIN) == MYPOLLIN) //读事件
        {
                ret = RecvProcess();
                if (ret == -1)
                {
                    DestroySelf();
                    return -1;
                }
        }
        
        if ((events& MYPOLLOUT) == MYPOLLOUT) //写事件
        {
                ret =  SendProcess(); 
                if ( ret == -1 )
                {
                    DestroySelf();
                    return -1;                	
                }
        }               
        return 0;
}
Exemplo n.º 2
0
//Local DNS server with UDP protocol
size_t __stdcall UDPMonitor(const SOCKET_DATA LocalhostData)
{
	if (!Parameter.DNSTarget.IPv6 && LocalhostData.AddrLen == sizeof(sockaddr_in6) || //IPv6
		!Parameter.DNSTarget.IPv4 && LocalhostData.AddrLen == sizeof(sockaddr_in)) //IPv4
	{
		closesocket(LocalhostData.Socket);
		return FALSE;
	}

//Socket initialization
	if (LocalhostData.Socket == INVALID_SOCKET)
	{
		PrintError(Winsock_Error, _T("UDP Monitor socket initialization failed"), WSAGetLastError(), NULL);

		closesocket(LocalhostData.Socket);
		return EXIT_FAILURE;
	}

	if (bind(LocalhostData.Socket, (PSOCKADDR)&LocalhostData.SockAddr, LocalhostData.AddrLen) == SOCKET_ERROR)
	{
		PrintError(Winsock_Error, _T("Bind UDP Monitor socket error"), WSAGetLastError(), NULL);

		closesocket(LocalhostData.Socket);
		return EXIT_FAILURE;
	}

//Initialization
	PSTR Buffer = nullptr;
	try {
		Buffer = new char[PACKET_MAXSIZE*THREAD_MAXNUM]();
	}
	catch (std::bad_alloc)
	{
		PrintError(System_Error, _T("Memory allocation failed"), NULL, NULL);

		closesocket(LocalhostData.Socket);
		return EXIT_FAILURE;
	}
	memset(Buffer, 0, PACKET_MAXSIZE*THREAD_MAXNUM);

//Start Monitor
	SSIZE_T RecvLength = 0;
	size_t Index = 0;
	while (true)
	{
		memset(Buffer + PACKET_MAXSIZE*Index, 0, PACKET_MAXSIZE);
		RecvLength = recvfrom(LocalhostData.Socket, Buffer + PACKET_MAXSIZE*Index, PACKET_MAXSIZE, NULL, (PSOCKADDR)&(LocalhostData.SockAddr), (int *)&(LocalhostData.AddrLen));

		if (RecvLength >= (SSIZE_T)(sizeof(dns_hdr) + sizeof(dns_qry)))
		{
			if (LocalhostData.AddrLen == sizeof(sockaddr_in6))
			{
				std::thread RecvProcess(RequestProcess, Buffer + PACKET_MAXSIZE*Index, RecvLength, LocalhostData, IPPROTO_UDP, Index);
				RecvProcess.detach();
			}
			if (LocalhostData.AddrLen == sizeof(sockaddr_in))
			{
				std::thread RecvProcess(RequestProcess, Buffer + PACKET_MAXSIZE*Index, RecvLength, LocalhostData, IPPROTO_UDP, Index + THREAD_MAXNUM);
				RecvProcess.detach();
			}

			Index = (Index + 1)%THREAD_MAXNUM;
		}
	}

	delete[] Buffer;
	closesocket(LocalhostData.Socket);
	return EXIT_SUCCESS;
}