Ejemplo n.º 1
0
int main(void)
{
    int i;

    for (i=0; i < NUM_PEERS; i++)
        rakPeer[i]=RakNetworkFactory::GetRakPeerInterface();

    printf("This project tests and demonstrates the fully connected mesh plugin.\n");
    printf("No data is actually sent so it's mostly a sample of how to use a plugin.\n");
    printf("Difficulty: Beginner\n\n");

    int peerIndex;

    // Initialize the message handlers
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
//		fullyConnectedMeshPlugin[peerIndex].Startup(0,0);
        rakPeer[peerIndex]->AttachPlugin(&fullyConnectedMeshPlugin[peerIndex]);
        // The fully connected mesh relies on the connection graph plugin also being attached
        rakPeer[peerIndex]->AttachPlugin(&connectionGraphPlugin[peerIndex]);
        rakPeer[peerIndex]->SetMaximumIncomingConnections(NUM_PEERS);
    }

    // Initialize the peers
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        SocketDescriptor socketDescriptor(60000+peerIndex,0);
        rakPeer[peerIndex]->Startup(NUM_PEERS, 0, &socketDescriptor, 1);
    }

    // Give the threads time to properly start
    RakSleep(200);

    printf("Peers initialized. ");
    printf("Connecting each peer to the prior peer\n");

    // Connect each peer to the prior peer
    for (peerIndex=1; peerIndex < NUM_PEERS; peerIndex++)
    {
        rakPeer[peerIndex]->Connect("127.0.0.1", 60000+peerIndex-1, 0, 0);
    }

    PrintConnections();

    // Close all connections
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        rakPeer[peerIndex]->Shutdown(100);
    }

    // Reinitialize the peers
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        SocketDescriptor socketDescriptor(60000+peerIndex,0);
        rakPeer[peerIndex]->Startup(NUM_PEERS, 0,&socketDescriptor, 1 );
    }

    printf("Connecting each peer to a central peer.\n");
    // Connect each peer to a central peer
    for (peerIndex=1; peerIndex < NUM_PEERS; peerIndex++)
    {
        rakPeer[peerIndex]->Connect("127.0.0.1", 60000, 0, 0);
    }

    PrintConnections();

    // Close all connections
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        rakPeer[peerIndex]->Shutdown(100);
    }

    // Reinitialize the peers
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        SocketDescriptor socketDescriptor(60000+peerIndex,0);
        rakPeer[peerIndex]->Startup(NUM_PEERS, 0, &socketDescriptor, 1);
    }

    printf("Cross connecting each pair of peers, then first and last peer.\n");
    // Connect each peer to a central peer
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        rakPeer[peerIndex]->Connect("127.0.0.1", 60000+peerIndex+(((peerIndex%2)==0) ? 1 : -1), 0, 0);
    }

    printf("Pairs Connected\n");
    PrintConnections();
    rakPeer[0]->Connect("127.0.0.1", 60000+NUM_PEERS-1, 0, 0);
    printf("First and last connected\n");
    PrintConnections();

    // Close all connections
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        rakPeer[peerIndex]->Shutdown(100);
    }

    // Reinitialize the peers
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        SocketDescriptor socketDescriptor(60000+peerIndex,0);
        rakPeer[peerIndex]->Startup(NUM_PEERS, 0, &socketDescriptor, 1);
    }


    unsigned int seed = (unsigned int) RakNet::GetTime();
    seedMT(seed);
    printf("Connecting each peer to a random peer with seed %u.\n", seed);
    int connectTo=0;
    // Connect each peer to a central peer
    for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
    {
        do
        {
            connectTo=randomMT() % NUM_PEERS;
        } while (connectTo==peerIndex);

        rakPeer[peerIndex]->Connect("127.0.0.1", 60000+connectTo, 0, 0);
    }

    PrintConnections();

    for (i=0; i < NUM_PEERS; i++)
        RakNetworkFactory::DestroyRakPeerInterface(rakPeer[i]);

    return 1;
}
Ejemplo n.º 2
0
int main(void)
{
	int i;

	for (i=0; i < NUM_PEERS; i++)
		rakPeer[i]=RakNetworkFactory::GetRakPeerInterface();

	printf("This project tests and demonstrates the ready event plugin.\n");
	printf("It is used in a peer to peer environment to have a group of\nsystems signal an event.\n");
	printf("It is useful for changing turns in a turn based game,\nor for lobby systems where everyone has to set ready before the game starts\n");
	printf("Difficulty: Beginner\n\n");

	int peerIndex;

	// Initialize the message handlers
	for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
	{
		fullyConnectedMeshPlugin[peerIndex].Startup(0,0);
		rakPeer[peerIndex]->AttachPlugin(&connectionGraphPlugin[peerIndex]);
		rakPeer[peerIndex]->AttachPlugin(&fullyConnectedMeshPlugin[peerIndex]);
		rakPeer[peerIndex]->AttachPlugin(&readyEventPlugin[peerIndex]);
		rakPeer[peerIndex]->SetMaximumIncomingConnections(NUM_PEERS);
	}


	// Initialize the peers
	for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
	{
		SocketDescriptor socketDescriptor(60000+peerIndex,0);
		rakPeer[peerIndex]->Startup(NUM_PEERS, 0, &socketDescriptor, 1);
	}

	// Give the threads time to properly start
	RakSleep(200);

	printf("Peers initialized.\n");

	printf("'C' to connect a system\n");
	printf("'D' to disconnect a system\n");
	printf("'S' to signal a system\n");
	printf("'U' to unsignal a system\n");
	printf("'F' to force all systems to be completed (cannot be unset)\n");
	printf("'Q' to quit\n");
	printf("' ' to print wait statuses\n");

	char str[128];
	char ch=0;
	while (1)
	{
		if (kbhit())
			ch=getch();

		if (ch=='s' || ch=='S')
		{
			ch=0;
			printf("Which system? 0 to %i\n", NUM_PEERS-1);
			gets(str);
			int sysIndex = atoi(str);
			if (sysIndex>=0 && sysIndex<NUM_PEERS)
			{
				if (readyEventPlugin[sysIndex].SetEvent(0,true))
					printf("Set system %i to signaled\n", sysIndex);
				else
					printf("Set system %i to signaled FAILED\n", sysIndex);
			}
			else
			{
				printf("Invalid range\n");
			}
		}
		if (ch=='u' || ch=='U')
		{
			ch=0;
			printf("Which system? 0 to %i\n", NUM_PEERS-1);
			gets(str);
			int sysIndex = atoi(str);
			if (sysIndex>=0 && sysIndex<NUM_PEERS)
			{
				if (readyEventPlugin[sysIndex].SetEvent(0,false))
					printf("Set index %i to unsignaled\n", sysIndex);
				else
					printf("Set index %i to unsignaled FAILED\n", sysIndex);
			}
			else
			{
				printf("Invalid range\n");
			}
		}
		if (ch=='c' || ch=='C')
		{
			ch=0;
			printf("Which system? 0 to %i\n", NUM_PEERS-1);
			gets(str);
			int sysIndex = atoi(str);
			if (sysIndex>=0 && sysIndex<NUM_PEERS)
			{
				rakPeer[sysIndex]->Connect("127.0.0.1", 60000, 0, 0, 0);
				printf("Connecting system %i.\n", sysIndex);
			}
			else
			{
				printf("Invalid range\n");
			}
		}
		if (ch=='d' || ch=='D')
		{
			ch=0;
			printf("Which system? 0 to %i\n", NUM_PEERS-1);
			gets(str);
			int sysIndex = atoi(str);
			if (sysIndex>=0 && sysIndex<NUM_PEERS)
			{
				rakPeer[sysIndex]->Shutdown(100,0);
				SocketDescriptor socketDescriptor(60000+sysIndex,0);
				rakPeer[sysIndex]->Startup(NUM_PEERS, 0, &socketDescriptor, 1);
				printf("Restarting system %i.\n", sysIndex);
			}
			else
			{
				printf("Invalid range\n");
			}
		}
		if (ch=='f' || ch=='F')
		{
			ch=0;
			printf("Which system? 0 to %i\n", NUM_PEERS-1);
			gets(str);
			int sysIndex = atoi(str);
			if (sysIndex>=0 && sysIndex<NUM_PEERS)
			{
				if (readyEventPlugin[sysIndex].ForceCompletion(0))
					printf("Set system %i to force complete\n", sysIndex);
				else
					printf("Set system %i to force complete FAILED\n", sysIndex);
			}
			else
			{
				printf("Invalid range\n");
			}
		}
		if (ch==' ')
		{
			SystemAddress sysAddr;
			sysAddr.SetBinaryAddress("127.0.0.1");
			unsigned j;
			printf("\n");
			PrintConnections();
			for (i=0; i < NUM_PEERS; i++)
			{
				printf("System %i, ", i);
				if (readyEventPlugin[i].IsEventSet(0))
					printf("Set=True, ");
				else
					printf("Set=False, ");

				if (readyEventPlugin[i].IsEventCompleted(0))
					printf("Completed=True\n");
				else if (readyEventPlugin[i].IsEventCompletionProcessing(0))
					printf("Completed=InProgress\n");
				else
					printf("Completed=False\n");

				for (j=0; j < NUM_PEERS; j++)
				{
					if (i!=j)
					{

						ReadyEventSystemStatus ress;
						sysAddr.port=60000+j;
						ress = readyEventPlugin[i].GetReadyStatus(0, sysAddr);
						printf("  Remote system %i, status = ", j);
						
						switch (ress)
						{
						case RES_NOT_WAITING:
							printf("RES_NOT_WAITING\n");
							break;
						case RES_WAITING:
							printf("RES_WAITING\n");
							break;
						case RES_READY:
							printf("RES_READY\n");
							break;
						case RES_ALL_READY:
							printf("RES_ALL_READY\n");
							break;
						case RES_UNKNOWN_EVENT:
							printf("RES_UNKNOWN_EVENT\n");
							break;

						}
					}
				}
			}
			ch=0;
		}
		if (ch=='Q')
		{
			break;
		}

		for (i=0; i < NUM_PEERS; i++)
		{
			Packet *p = rakPeer[i]->Receive();
			if (p)
			{
				switch (p->data[0])
				{
				case ID_NEW_INCOMING_CONNECTION:
					readyEventPlugin[i].AddToWaitList(0, p->systemAddress);
					break;
				case ID_CONNECTION_REQUEST_ACCEPTED:
					readyEventPlugin[i].AddToWaitList(0, p->systemAddress);
					break;
				case ID_READY_EVENT_ALL_SET:
					printf("Got ID_READY_EVENT_ALL_SET from %s\n", p->systemAddress.ToString(true));
					break;
				}

				rakPeer[i]->DeallocatePacket(p);
			}			
		}

		// Keep raknet threads responsive
		RakSleep(30);
	}
	
	for (i=0; i < NUM_PEERS; i++)
		RakNetworkFactory::DestroyRakPeerInterface(rakPeer[i]);

	return 1;
}