2D
Online game development (network) (11)
Author:
akinggw
Foreword
In the previous content, we explained what is bitstream, how to build a packet, how to send packets. Today, we will explain how to receive packets and translate these three contents, and we will actually write code. See how important these things are.
Receive packets
When a packet is transmitted from the network. The reception function will return is not 0, here we have to process this receiving process three steps. The MultiPlayer class will handle the first step and third step.
1.
Determine the type of packet, the type of this packet can be returned by the following code:
Unsigned char getpacketIdentifier (packet * p)
{
IF ((unsigned char) P-> Data [0] == id_timestamp)
Return (unsigned char) p-> data [sizeof (unsigned char) sizeof (unsigned long);
Else
Return (unsigned char) p-> data [0];
}
Receive data structure
If you send a structure, you can receive it like this:
// If you don't use MultiPlayer, you will be handed over to ProcessunhandLeDPacket Processing.
IF (packetidentifier == / * Here is a packet ID that has been assigned to the user * /)
DomypacketHandler (Packet);
// put this function anywhere you want to put, and process the game in the state class is a good place.
Void DomypacketHandler (Packet * Packet)
{
// Place the data in the appropriate type of structure
MyStruct * s = (mystruct *) packet-> data;
Assert (p-> length == sizeof (mystruct));
IF (p-> length! = Sizeof (mystruct))
Return;
// Use the networkidgenerator and macro position, then get a pointer pointing to the object indicating the structure.
MyObject * Object = (myObject *) get_Object_form_id (s.object);
/ / Perform a function for this type of packet
Object-> DOFunction ();
}
Useful comment
l
We use the data pointer to the data packet to avoid empty copy. If you do this, no matter what, if we change any data in the structure, you will also change the packet. This may or may not be the result you want. Be careful to keep the information returned by the server because it may cause some unnecessary trouble.
l
The Assert is not necessarily necessary, but when we send packets, it is assigned an error or the error length, it will be very useful for finding an error.
l
But some people send some packets that contain errors and types in order to attack clients or servers. In this case, the status is particularly important.
2.
Delete the packet by deallocatepacket = 0; function.
Receive bitstream
If you send a bitstream, we build bitstream to analyze data should follow the same order as we write data. We build a bitstream, using the length of the data and packets. We then use the read function before using the write function; use read compression before using write compression (WriteCompressed), regardless of the data we write, will follow the same logical branch given above. Below, we give an example, this example uses the structure MINE used when building a packet. This example is how to read the data in the MINE structure.
Void DomypacketHandler (Packet * Packet)
{
BitStream MyBitStream (Packet-> Data, Packet-> Length, false); // The function of the third parameter FALSE is not allowed to copy the data transferred
Mybitstream.read (UsetimeStamp);
MyBitStream.read (TimeStamp);
MyBitStream.read (TypeID);
Bool isatzero;
Mybitstream.read (isatzero);
IF (isatzero == false)
{
X = 0.0F;
Y = 0.0F;
z = 0.0F;
}
Else
{
Mybitstream.read (x);
Mybitstream.read (y);
MyBitStream.read (z);
}
MyBitStream.read (ObjectID); // In this structure, this is an object identity
MyBitStream.read (Playerid); // In this structure, this is the player logo
}
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.