table of Contents
2. File definition format of protobuf
5. How to use in game projects
There are many forms of communication between game server and client, some use http, some use websocket, but the most common is the socket server, which is the most common in games. As for why and how to create it, we will talk about it later, today Let's talk about the protocol between the server and the client. The definition of the protocol is the result of the communication between the server and the client, forming a consistent data format, so that everyone can parse it and know what the other party is saying and doing.
At the beginning, some people customized the format. Although it is compact, there may be some problems and it is not stable enough. Some people use xml, while others use json. The problem is that although the format is good, the network package is too large and not suitable. If the problem exists, it must be solved. Is there a solution to the above problem? The obvious answer is the protobuf we talked about today.
protobuf is Google's open source cross-platform communication protocol, which is more compact and more efficient. Not much nonsense, enter the text.
1. Java project reference
Add the following dependencies to pom.xml, the version can be selected according to your needs
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.6.1</version>
</dependency>
2. File definition format of protobuf
option java_package ="com.gamwatcher.soulmsg";
option java_outer_classname = "SoulMsg";
option java_multiple_files = true;
message SOUL_UP_OUT{
required int64 uid =1;
repeated int64 costuid =2;
optional int64 useExp = 3;
}
Basic type
.proto type | java type | Remarks |
---|---|---|
double | double | |
float | float | |
int32 | int | Use variable length encoding. It is not efficient enough to encode negative numbers-if your field may contain negative numbers, use sint32. |
int64 | long | Use variable length encoding. Encoding negative numbers is not efficient enough-if your field may contain negative numbers, use sint64. |
unit32 | int[1] | Always 4 bytes. If the value is always greater than 228, this type will be more efficient than uint32. |
unit64 | long[1] | Always 8 bytes. If the value is always greater than 256, this type will be more efficient than uint64. |
sint32 | int | Use variable length encoding. Signed integer value. Encoding is more efficient than the usual int32. |
sint64 | long | Use variable length encoding. Signed integer value. Encoding is more efficient than the usual int64. |
fixed32 | int[1] | |
fixed64 | long[1] | Always 8 bytes. If the value is always greater than 256, this type will be more efficient than uint64. |
sfixed32 | int | Always 4 bytes. |
sfixed64 | long | Always 8 bytes. |
bool | boolean | |
string | String | A string must be UTF-8 encoded or 7-bit ASCII encoded text. |
bytes | ByteString | May contain byte data in any order |
Special field
English | Chinese | Remarks |
---|---|---|
enum | Enumeration (number starting from zero) is used to specify a "predefined sequence of values" for the field | enum Type {MAN = 0;WOMAN = 1; OTHER= 3;} |
message | Informed body | message User{} |
repeated | Array/collection | repeated User users = 1 |
import | Import definition | import "protos/other_protos.proto" |
// | Comment | //Used for comments |
extend | Expand | extend User {} |
package | Package names | Equivalent to a namespace, used to prevent clear conflicts of different message types |
3. Generate java classes
下载protoc:https://github.com/protocolbuffers/protobuf/releases
protoc.exe --java_out = ../../src/main/java **.proto
4. Use agreement
SOUL_UP_OUT.Builder builder = SOUL_UP_OUT.newBuilder();
builder.setUid(1);
builder.addAllCostUid(costUidList);
builder.setUserExp(1000)
builder.build()
5. How to use in game projects
Normal protocol format:
len + encrypted [headMsgId + proto binary data]
Commonly used encryption algorithms: AES and RSA, DES, choose a simple and efficient one, if the game is on fire, you can change to a slightly more complicated encryption algorithm, small things, not important
The client parses out the data length according to the length to read the analysis. so easy!!!, the same rules on the server side. The communication between client and server is that simple.
Summary: protobuf is just a protocol format, which saves us the process of customizing messages. Since we have ready-made wheels, we don’t need to make them ourselves. Moreover, we don’t make them as good as others. We can use them first and then understand the principles. It’s no big deal. .