Esempio n. 1
0
void CMuleUDPSocket::OnReceive(int errorCode)
{
	AddDebugLogLineN(logMuleUDP, CFormat(wxT("Got UDP callback for read: Error %i Socket state %i"))
		% errorCode % Ok());

	char buffer[UDP_BUFFER_SIZE];
	amuleIPV4Address addr;
	unsigned length = 0;
	bool error = false;
	int lastError = 0;

	{
		wxMutexLocker lock(m_mutex);

		if (errorCode || (m_socket == NULL) || !m_socket->IsOk()) {
			DestroySocket();
			CreateSocket();

			return;
		}


		length = m_socket->RecvFrom(addr, buffer, UDP_BUFFER_SIZE);
		lastError = m_socket->LastError();
		error = lastError != 0;
	}

	const uint32 ip = StringIPtoUint32(addr.IPAddress());
	const uint16 port = addr.Service();
	if (error) {
		OnReceiveError(lastError, ip, port);
	} else if (length < 2) {
		// 2 bytes (protocol and opcode) is the smallets possible packet.
		AddDebugLogLineN(logMuleUDP, m_name + wxT(": Invalid Packet received"));
	} else if (!ip) {
		// wxFAIL;
		AddLogLineNS(wxT("Unknown ip receiving a UDP packet! Ignoring: '") + addr.IPAddress() + wxT("'"));
	} else if (!port) {
		// wxFAIL;
		AddLogLineNS(wxT("Unknown port receiving a UDP packet! Ignoring"));
	} else if (theApp->clientlist->IsBannedClient(ip)) {
		AddDebugLogLineN(logMuleUDP, m_name + wxT(": Dropped packet from banned IP ") + addr.IPAddress());
	} else {
		AddDebugLogLineN(logMuleUDP, (m_name + wxT(": Packet received ("))
			<< addr.IPAddress() << wxT(":") << port << wxT("): ")
			<< length << wxT("b"));
		OnPacketReceived(ip, port, (byte*)buffer, length);
	}
}
Esempio n. 2
0
bool
GameServer::DispatchReceivedData(SocketContext* pSockConn, BYTE* pData, DWORD dwSize, bool bIsFirstData){
	ASSERT(m_pPacketHandler && pData && dwSize > 0);
	if( !pData || !dwSize )
		return false;
	ClientConnection* pConn = (ClientConnection*)pSockConn->GetParam();

	GrowableMemory mem(0, 0, false);
	mem.SetReadonlyBuffer(pData, dwSize);
	if( !pConn )
		return false;

	BasePacket	basePacket;
	int			nOffset				= 0;
	int			nBufferSize			= dwSize;
	int			nSerializedBytes	= 0;

	while( TRUE ){
		if( basePacket.Deserialize(&mem) ){ // Success
			// Handshake must be the only one command accepted while session is not created. 
			if( (basePacket.m_nCode != PacketCodes::_HandShake && basePacket.m_nCode != PacketCodes::_AcquireTableAccess) && pConn->GetSession(false) == NULL ) 
				return false;
			BasePacket* packet	= m_pPacketHandler->CreatePacketByCode(basePacket.m_nCode);
			ASSERT( packet );

#ifndef _DEBUG
		if( packet == NULL )
			return false;
#endif
			// Deserialize received data into known packet.
			if( !mem.SetCurrentOffset(nOffset) )
				return false;
			if( !packet->Deserialize(&mem) )
				return false;

			// Handle received packet.
			OnPacketReceived(pConn, packet);
			delete packet;
		
			nOffset	= mem.GetCurrentOffset();
			if( nOffset == nBufferSize )
				break;
			}
		else
			break;
		}

	return true;
	}