1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "Kbhit.h"
5 #include "RakPeerInterface.h"
6 #include "RakNetworkFactory.h"
7 #include "MessageIdentifiers.h"
8 #include "PacketLogger.h"
9 #include "NetworkIDManager.h"
10 #include "RakSleep.h"
11 #include "BitStream.h"
12 
13 #include "Common.h"
14 
15 RakPeerInterface *rakClient;
16 CustomPacketLogger clientLogger;
17 NetworkIDManager clientNetworkIDManager;
18 
RunAsClient(void)19 void RunAsClient(void)
20 {
21 
22 	rakClient = RakNetworkFactory::GetRakPeerInterface();
23 	// Apple is defined in "Common.h", and it's our class derived from NetworkIDObject
24 	Apple *serverApple = 0;
25 
26 #if DOLOG
27 	clientLogger.SetPrefix("CLIENT> ");
28 	clientLogger.SetSuffix("\n");
29 	rakClient->AttachPlugin(&clientLogger);
30 #endif
31 
32 	char serverip[256];
33 	printf("Enter ip of the server (ENTER defaults to 127.0.0.1): ");
34 	gets(serverip);
35 	if (serverip[0]==0) strcpy(serverip, "127.0.0.1");
36 	char serverport[256];
37 	printf("Enter port of the server (ENTER defaults to 60000): ");
38 	gets(serverport);
39 	if (serverport[0]==0) strcpy(serverport, "60000");
40 
41 	SocketDescriptor sd;
42 	rakClient->Startup(1, 30, &sd, 1);
43 	rakClient->Connect(serverip, atoi(serverport), 0,0);
44 
45 	// In a client/server architecture, only the server can create NetworkIDs
46 	clientNetworkIDManager.SetIsNetworkIDAuthority(false);
47 
48 	rakClient->SetNetworkIDManager(&clientNetworkIDManager);
49 
50 	// Clear keyboard buffer before entering loop
51 	while(kbhit()) getch();
52 	do{
53 
54 		Packet *p = rakClient->Receive();
55 		while(p){
56 			unsigned char msgID = p->data[0];
57 
58 			// Check if the server has sent us the NetworkID of his Apple object
59 			if (msgID==ID_USER_SERVERAPPLE_CREATED) {
60 				RakNet::BitStream bs(p->data, p->length, false);
61 				// Ignore the message ID
62 				bs.IgnoreBits(8);
63 
64 				NetworkID appleNetworkID;
65 				bs.Read(appleNetworkID);
66 
67 				// Create the object on the client side
68 				serverApple = new GrannySmith;
69 				serverApple->SetNetworkIDManager(&clientNetworkIDManager);
70 				serverApple->SetNetworkID(appleNetworkID);
71 
72 				// Now that we have the object created, we can use RPC on it.
73 				const char *hello="Hello World!!!!!";
74 				rakClient->RPC(CLASS_MEMBER_ID(Apple,func1), hello, (unsigned int) (strlen(hello)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED,0, UNASSIGNED_SYSTEM_ADDRESS, true, 0, serverApple->GetNetworkID(),0);
75 				rakClient->RPC(CLASS_MEMBER_ID(Apple,func2), hello, (unsigned int) (strlen(hello)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED,0, UNASSIGNED_SYSTEM_ADDRESS, true, 0, serverApple->GetNetworkID(),0);
76 				rakClient->RPC(CLASS_MEMBER_ID(Apple,func3), hello, (unsigned int) (strlen(hello)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED,0, UNASSIGNED_SYSTEM_ADDRESS, true, 0, serverApple->GetNetworkID(),0);
77 			}
78 
79 			rakClient->DeallocatePacket(p);
80 			p = rakClient->Receive();
81 		}
82 
83 		// Sleep for a while, so we don't eat all the CPU time with such a simple program.
84 		// In a real game, you probably won't need this, as you want your game to run at full speed.
85 		RakSleep(30);
86 
87 	} while(!kbhit());
88 
89 
90 	// Shutdown
91 	if (serverApple) delete serverApple;
92 	rakClient->Shutdown(300);
93 	RakNetworkFactory::DestroyRakPeerInterface(rakClient);
94 }