Raknet Basic Tutorials

Source: Internet
Author: User

Official website: http://www.jenkinssoftware.com/raknet/manual/tutorialsample3.html

Tutorial code Sample 3
Remote Procedure call setup.

The target of this exercise is to add the following features to Sample 2:
    1. When the client successfully connects, send a custom user message.
    2. 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.
    3. 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,&AMP;SD, 1); isserver = false;} else {raknet::socketdescriptor SD (server_port,0);p eer->startup (max_clients, &AMP;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;}

Raknet Basic Tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.