2D online game development (network) (8) 2

xiaoxiao2021-04-05  251

#pragma pack (1)

Struct C //

structure

C

Type and structure

A

Type

{

A a a;

B B;

}

#pragma pack (1)

Struct D //

structure

C

Type and structure

A

Type

{

B b;

A Al

}

Use a bitstream to create a packet

We still use the above example, but this time we decided to rewrite him with a specific stream. We have the same data as before.

Unsigned char useterstamp; //

Assign this

ID_TimeStamp

Unsigned long timestamp; //

Put by

GetTime ()

Returned system time is here

Unsigned char typeid; //

Here, a data type will be added to

Packetenumerates.h

In the file, here we set up

ID_SET_TIMED_MINE

Usetimestamp = id_timestamp;

Timestamp = getTime ();

TYPEID = ID_SET_TIMED_MINE;

BitStream mybitstream;

MyBitStream.write (UsetimeStamp);

MyBitStream.write (TimeStamp);

MyBitStream.write (TypeID);

//

Suppose we have a definition

Mine *

of

Mine

Object

Mybitstream.write (Mine-> getPosition (). x);

Mybitstream.write (Mine-> getPosition (). Y);

MyBitStream.write (Mine-> getPosition (). z);

Mybitstream.write (Mine-> getId ()); //

In this structure, this is

ObjectID

Object

Id

Mybitstream.write (mine-> getowner ()); //

In this structure, this is

Playerid,

Player

Id

If we use it now

RakClient :: Send

or

Rakserver :: Send

To send our bitstream will lead to the chaos of the structure. Let us try to do some improvements. Now, suppose our players' coordinates for some reason,

0

,

0

,

0

. Then the previous code will modify the following form:

Unsigned char useterstamp; //

Assign this to

ID_TimeStamp

Unsigned long timestamp; //

Store

GetTime ()

Returned system time

Unsigned char typeid; //

Assign a type to

Packetenumerates.h

In the middle, we set it here.

ID_SET_TIMED_MINE

Usetimestamp = id_timestamp;

Timestamp = getTime ();

TYPEID = ID_SET_TIMED_MINE;

BitStream mybitstream;

MyBitStream.write (UsetimeStamp);

MyBitStream.write (TimeStamp);

MyBitStream.write (TypeID);

IF (mine-> getPosition (). X == 0.0f && mine-> getPOStion (). Y == 0.0f && mine-> getPosition (). Z == 0.0f) {

MyBitStream.write (TRUE);

}

Else

{

MyBitStream.write (false);

Mybitstream.write (Mine-> getPosition (). x);

Mybitstream.write (Mine-> getPosition (). Y);

MyBitStream.write (Mine-> getPOSISTON (). z);

}

Mybitstream.write (Mine-> getId ()); //

In this structure, this is an object

Mybitstream.write (mine-> getowner ()); //

In this structure, this is a player

Written

Overload

BitStream

You can write a word stream with an array. One method is to write data length, then write data, the content is as follows:

Void WriteStringTroitStream (Char * MyString, BitStream * Output)

{

Output-> Write (UNSIGNED SHORT) Strlen (MyString);

Output-> Write (MyString, Strlen (MyString);

}

Their coding is the same, no matter what, it is not very efficient.

Raknet

There is a character compression class

Stringcompressor

Compression of the string can be performed. If we want to use this class to encode data, we can write the following form:

Void WriteStringTroitStream (Char * MyString, BitStream * Output)

{

Stringcompressor-> Encodestring (MyString, 256, Output);

}

You can extract the compressed string by the following form.

Void WriteBitStreamTRING (Char * MyString, BitStream * Input)

{

Stringcompressor-> Decodestring (mystring, 256, input);

}

it's here,

256

Represents the maximum number of bytes. During the encoding process, if your strings are less

256

This is written, then the entire string will be written. Conversely, if your string is greater than

256

One byte, then truncation it, then

256

The array of bytes is encoded, of course, including the terminator

NULL

.

For more information, please visit the Jinqiao Science website (

http://popul.jqcq.com

The game development section, if 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, mine.

E_mail: akinggw@126.com.

If you are using

SDL

What is wrong with it, please go to Jinqiao Scot,

http://popul.jqcq.com

) The game development section, I will answer your questions in detail.

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

New Post(0)