Online Game Development Experience (server) (2)

Source: Internet
Author: User

Bromon originality, please respect copyright

Ii. Communication Protocol

This project does not have complex communication commands, and the number of commands is very limited, but there is still a key issue to be concerned: traffic. To minimize the traffic, we use bytes instead of strings to save system commands, which can reduce the traffic by half. For example, we use one byte to save a playing card, and the byte height indicates the color, the low byte level indicates the number. If 0 represents the black peach, then the black peach 3 should be 0x03. This requires bitwise operations:

 

Int m = 0;

Int n = 3;

Byte card = (byte) (m) <4) | (byte) N; // M shifts four places left and then operates with N

 

In the game, you need to pass the user's points. This is a big integer. it is safer to use four bytes to save. The operation to convert an integer to four bytes is as follows:

 

Package org. bromon. Games. Al;
Public static byte [] translatelong (long mark)

{

Byte [] B = new byte [4];

For (INT I = 0; I <4; I ++)

{

B [I] = (byte) (Mark >>> (24-I * 8 ));

}

Return B;

}

 

The operation to convert the four bytes back is as follows:


Package org. bromon. Games. Al;
 

Public static long translatebyte (byte [] B)

{

Int mask = 0xff;

Int temp = 0;

Int res = 0;

For (INT I = 0; I <4; I ++)

{

Res <= 8;

Temp = B [I] & mask;

Res | = temp;

}

Return res;

}

Next article: About database connection pool

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.