Rakkarsoft LLC

Tutorial code sample 2
Connecting, reading, and parsing network messages.

The target of this exercise was to add the following features to sample 1:
  1. Create a main loop for the program.
  2. Call Receive and store the pointer returned in a pointer variable of type Packet.
  3. Include the header file to use struct Packet
  4. If a packet arrived, check the first byte of Packet::data. Map this to a connectivity related network enumeration.
  5. Print out the comment that goes along with the enumeration.
  6. Deallocate the packet pointer when done with it.
New code over sample 1 is in bold.

#include <stdio.h> // Printf and gets
#include <string.h> // strcpy
#include "RakClientInterface.h"
#include "RakNetworkFactory.h"
#include "RakServerInterface.h"
#include "PacketEnumerations.h"

int main(void)
{
	char str[512];
	RakClientInterface *rakClientInterface;
	RakServerInterface *rakServerInterface;
	Packet *packet;

	printf("(C)lient or (S)erver?\n");
	gets(str);
	if (str[0]=='c' || str[0]=='C')
	{
		rakClientInterface=RakNetworkFactory::GetRakClientInterface();
		rakServerInterface=0;
	}
	else
	{
		rakClientInterface=0;
		rakServerInterface=RakNetworkFactory::GetRakServerInterface();
	}

	if (rakServerInterface)
	{
		// Running in server mode on port 60000
		rakServerInterface->Start(32, 0, 0, 60000);
	}
	else
	{
		// Running in client mode
		printf("Enter server IP or hit enter for 127.0.0.1\n");
		gets(str);
		// 127.0.0.1 designates the feedback loop so we can test on one computer
		if (str[0]==0)
			strcpy(str, "127.0.0.1");
		rakClientInterface->Connect(str, 60000, 0, 0, 0);
	}

	while (1)
	{
		if (rakServerInterface)
			packet=rakServerInterface->Receive();
		else
			packet=rakClientInterface->Receive();

		if (packet)
		{
			switch (packet->data[0])
			{
			case ID_REMOTE_DISCONNECTION_NOTIFICATION:
				printf("Another client has disconnected.\n");
				break;
			case ID_REMOTE_CONNECTION_LOST:
				printf("Another client has lost the connection.\n");
				break;
			case ID_REMOTE_NEW_INCOMING_CONNECTION:
				printf("Another client has connected.\n");
				break;
			case ID_CONNECTION_REQUEST_ACCEPTED:
				printf("Our connection request has been accepted.\n");
				break;

			case ID_NEW_INCOMING_CONNECTION:
				printf("A connection is incoming.\n");
				break;
			case ID_NO_FREE_INCOMING_CONNECTIONS:
				printf("The server is full.\n");
				break;
			case ID_DISCONNECTION_NOTIFICATION:
				if (rakServerInterface)
					printf("A client has disconnected.\n");
				else
					printf("We have been disconnected.\n");
				break;
			case ID_CONNECTION_LOST:
				if (rakServerInterface)
					printf("A client lost the connection.\n");
				else
					printf("Connection lost.\n");
				break;
			default:
				printf("Message with identifier %i has arrived.\n", packet->data[0]);
				break;
			}

			if (rakServerInterface)
				rakServerInterface->DeallocatePacket(packet);
			else
				rakClientInterface->DeallocatePacket(packet);
		}
	}

	if (rakClientInterface)
		RakNetworkFactory::DestroyRakClientInterface(rakClientInterface);
	else if (rakServerInterface)
		RakNetworkFactory::DestroyRakServerInterface(rakServerInterface);
	return 0;
}