2D online game development (network) (six)

xiaoxiao2021-04-05  252

2D online game development (network) (six)

Author: akinggw

In the previous chapter, we have implemented a simple chat room. Today, we still have to revolve around this theme, but take other ways, this method is useful, it should be the key to the entire network engine, it is -RPC (Remote Procedure Calls), translated into Chinese, you can understand "remote function transfer".

Normally, you send a message, you must implement the following four steps:

1.

Build a packet to store your data;

2.

A function must be established to implement the encoding and transmission of packets;

3.

Establish a data package identification function to identify your packets so that you call which function you call it;

4.

At last

Establish a function to decode your packets and process it.

The above is the four steps we have to do in writing a network program.

But Raknet has done all this, that is, RPC, with it, you only use two steps so you have more time to concentrate on the game.

1.

Code your data;

2.

Then call a corresponding function of the remote system to handle it.

The process implemented in your game is as follows:

A.

Tell the network system allows the use of RPC call functions

Of course, you don't need any functions in the RPC call system, which will make a lot of trouble. You only need a few functions defined with the RPC parameter, you can define your RPC function with the example below.

C

function

Void myfunction (rpcparameters * rpcparameters) {} //

Client pointer

RakClient * rakclient; //

Registration

RpcRegister_as_Remote_Procedure_call (rakclient, myfunction); C

Static function

Static void myclass :: myfunction (rpcparameters * rpcparameters) {} //

Client pointer

RakClient * rakclient; //

Registration

RpcRegister_as_remote_procedure_call (rakclient, myclass :: myfunction); C

Member function

Class myclass: public networkidgenerator {void __cdecl func1 (rpcparameters * rpcparms);}; //

Client pointer

RakClient * rakclient; //

Registration

RpcRegister_class_member_rpc (rakclient, myclass, func1)

The server's registration is the same.

B. Give your data encoding

Your RPC method can handle a length of string or bitstream, which is equivalent to packaging data.

C. Call the RPC function to process

D. The corresponding function on the remote system will process your data.

The above is the process of the RPC.

Below, let's take a look at the parameters and structure of the RPC:

CHAR * INPUT; data from the remote system;

Unsigned int numberofbitsofdata; the size of the data we receive;

Playerid Sender; which system calls this RPC;

RakpeerInterface * Recipient; which instance in rakpeer will be called this call;

Bool HastimeStamp; If true, the input starts 4 bytes to indicate time;

Raknet :: BitStream * ReplyToSender recommends the sender with the corresponding data stream.

Below, let's take a look at the code:

Char Message1 [300];

Void PrintMessage (rpcparameters * rpcparameters) {

Printf ("% SN", rpcparameters-> input);

Sprintf (Message1, "% s", rpcparameters-> input);

IF (RakserverInterface)

{

RakserverInterface-> RPC ("PrintMessage", Message1, (Strlen (Message1) 1) * 8 8, High_Priority, Reliable_Ordered, 0, RpCparameters-> Sender, true, false, unassigned_network_id, 0);

}

}

Below, let's specifically explain this function.

Message1 is used to store the obtained information. First, we print the information we get, and then determine if we are a server, if yes, then call the client's RPC.

Here this RPC function prototype is as follows:

Bool Rak Server :: RPC (Char * UniqueID,

Const Char * DATA,

Unsigned int bitlength,

PacketPriority Priority,

Packetreliability Reliability,

Char OrderInnenel,

Playerid Playerid,

Bool Broadcast,

Bool ShiftTimeStamp,

Objectid ObjectID,

Raknet :: BitStream * replyfromtarget

[Virtual, Inherited]

The first parameter, our registered RPC function name;

The second parameter, the data we have to send;

The third parameter, the size of the transmitted data;

The fourth parameter, the security level of the packet, like the Send function;

The fifth parameters, the reliability of the packet, and the SEND function;

The sixth parameter, like the Send function;

Seventh parameters, recipient ID;

Eighth parameters, whether broadcasting;

The ninth parameters, related to time, later explained;

Tenth parameters, if it is a static function, set directly to

Unassigned_Object_id

The eleventh parameters, retained.

IF (RakserverInterface)

{

// Server runs at 600,000 ports

RakserverInterface-> Start (32, 0, 0, 60000);

Register_static_rpc (rakserverinterface, printmessage);

}

Else

{

// Run the client

Printf ("Enter Server IP Address: 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);

Register_static_rpc (rakclientInterface, PrintMessage);

}

Register the RPC in the server or client.

Gets (Message);

IF (RakserverInterface)

{

RakserverInterface-> RPC ("PrintMessage", Message, (Strlen 1) * 8, High_Priority, Reliable_ORDERED, 0, Unassigned_Player_ID, True, False, Unassigned_Network_ID, 0);} Else

{

RakClientInterface-> RPC ("PrintMessage", Message, (Strlen 1) * 8, High_Priority, Reliable_ORDERED, 0, FALSE, UNASSIGNED_NETWORK_ID, 0);

}

Get information, then call the RPC.

Other code does not have to be changed, the effect of the program is as follows:

Figure 1

Figure 2

OK, today's content is here.

For more information, please visit the Golden Bridge Science website (http://popul.jqcq.com) Game development section, such as you need a book development, please refer to Jinqiao Book City Game Channel (http://book.jqcq.com/category /1_70_740.html). If you have any good suggestions when you read this article, please let me, my e_mail: akinggw@126.com. If you have any questions when using SDL, please go to the Jinqiao Scot website (http: // popul. JQCQ.com) Game Development section, I will answer your questions in detail.

转载请注明原文地址:https://www.9cbs.com/read-132128.html

New Post(0)