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