Remote Procedure call setup.
The target of this exercise is to add the following features to Sample 2:
- When the client successfully connects, send a custom user message.
- The first byte of custom messages should always start after the enumeration id_user_packet_enum, defined in Messageidentif Iers.h. Otherwise, Raknet would confuse its own identifiers with your game identifiers.
- Read the custom message in our packet processing loop. The native rakstring type can read and write to the Bitstream class. You would is able to do this using std::string.
New code over sample 2 was in bold.
#include <stdio.h> #include <string.h> #include "RakPeerInterface.h" #include "MessageIdentifiers.h"#include "BitStream.h" #include "RakNetTypes.h"//MessageID#define Max_clients 10#define Server_port 60000enum gamemessages{id_game_message_1=id_user_packet_enum+1};int main (void) {char str[512]; Raknet::rakpeerinterface *peer = raknet::rakpeerinterface::getinstance (); bool Isserver; Raknet::P acket *packet;printf ("(c) or (S) erver?\n"), gets (str), if ((str[0]== ' C ') | | (str[0]== ' C ')) {Raknet::socketdescriptor Sd;peer->startup (1,&SD, 1); isserver = false;} else {raknet::socketdescriptor SD (server_port,0);p eer->startup (max_clients, &SD, 1); isserver = true;} if (isserver) {printf ("Starting the server.\n");//We need to let the server accept incoming connections from the Clientspe Er->setmaximumincomingconnections (max_clients);} else {printf ("Enter server IP or hit Enter for 127.0.0.1\n"), gets (str), if (str[0]==0) {strcpy (str, "127.0.0.1");} printf ("Starting the client.\n");p eer->connect (str, server_port, 0,0);} while (1) {for (packet=peer->receive (); packet; Peer->deallocatepacket (packet), packet=peer->receive ()) { 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");//Use a bitstream to write a custom user message//Bitstreams is easier to use than sending casted structures, and handle endian swapping Automaticallyraknet::bitstream BSOUT;BSOUT.WR ITE ((Raknet::messageid) id_game_message_1); Bsout.write ("Hello World");p Eer->send (&bsout,high_priority, Reliable_ordered,0,packet->systemaddress,false);}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 (isserver) {printf ("A Client Has disconnected.\n ");} else {printf ("We have been disconnected.\n");} Break;case Id_connection_lost:if (isserver) {printf ("A client LOST the connection.\n");} else {printf ("CONNECTION lost.\ n ");} BreakCase id_game_message_1:{raknet::rakstring rs; Raknet::bitstream Bsin (Packet->data,packet->length,false); Bsin.ignorebytes (sizeof (RakNet::MessageID)); Bsin.read (RS);p rintf ("%s\n", Rs. C_string ());} break;default:printf ("Message with identifier%i have arrived.\n", packet->data[0]); Raknet::rakpeerinterface::D estroyinstance (peer); return 0;} |