示例#1
0
void MessageFilter::OnNewConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
{
	(void) systemAddress;
	(void) rakNetGUID;
	(void) isIncoming;
	// New system, automatically assign to filter set if appropriate
	if (autoAddNewConnectionsToFilter >= 0 && systemList.HasData(systemAddress) == false)
		SetSystemFilterSet(systemAddress, autoAddNewConnectionsToFilter);
}
 PluginReceiveResult MessageFilter::OnReceive(RakPeerInterface *peer, Packet *packet)
{
	bool objectExists;
	unsigned index;
	unsigned char messageId;

	switch (packet->data[0]) 
	{
	case ID_CONNECTION_LOST:
	case ID_DISCONNECTION_NOTIFICATION:
		// Lost system, remove from the list
		systemList.RemoveIfExists(packet->systemAddress);
		break;
	case ID_NEW_INCOMING_CONNECTION:
	case ID_CONNECTION_REQUEST_ACCEPTED:
		// New system, automatically assign to filter set if appropriate
		if (autoAddNewConnectionsToFilter>=0 && systemList.HasData(packet->systemAddress)==false)
            SetSystemFilterSet(packet->systemAddress, autoAddNewConnectionsToFilter);
		break;
	case ID_CONNECTION_ATTEMPT_FAILED:
	case ID_NO_FREE_INCOMING_CONNECTIONS:
	case ID_RSA_PUBLIC_KEY_MISMATCH:
	case ID_CONNECTION_BANNED:
	case ID_INVALID_PASSWORD:
	case ID_MODIFIED_PACKET:
	case ID_PONG:
	case ID_ALREADY_CONNECTED:
	case ID_ADVERTISE_SYSTEM:
	case ID_REMOTE_DISCONNECTION_NOTIFICATION:
	case ID_REMOTE_CONNECTION_LOST:
	case ID_REMOTE_NEW_INCOMING_CONNECTION:
	case ID_DOWNLOAD_PROGRESS:
		break;
	default:
		if (packet->data[0]==ID_TIMESTAMP)
		{
			if (packet->length<sizeof(MessageID) + sizeof(RakNetTime))
				return RR_STOP_PROCESSING_AND_DEALLOCATE; // Invalid message
			messageId=packet->data[sizeof(MessageID) + sizeof(RakNetTime)];
		}
		else
			messageId=packet->data[0];
		// If this system is filtered, check if this message is allowed.  If not allowed, return RR_STOP_PROCESSING_AND_DEALLOCATE
		index = systemList.GetIndexFromKey(packet->systemAddress, &objectExists);
		if (objectExists==false)
			break;
		if (messageId==ID_RPC)
		{
			const char *uniqueIdentifier = peer->GetRPCString((const char*) packet->data, packet->bitSize, packet->systemAddress);
			if (systemList[index].filter->allowedRPCs.HasData((char *const)uniqueIdentifier)==false)
			{
				OnInvalidMessage(peer, systemList[index].filter, packet->systemAddress, packet->data[0]);
				return RR_STOP_PROCESSING_AND_DEALLOCATE;
			}
		}
		else
		{
			if (systemList[index].filter->allowedIDs[messageId]==false)
			{
				OnInvalidMessage(peer, systemList[index].filter, packet->systemAddress, packet->data[0]);
				return RR_STOP_PROCESSING_AND_DEALLOCATE;
			}
		}		
		
		break;
	}
	
	return RR_CONTINUE_PROCESSING;
}