Tribute to the original http://blog.csdn.net/janeky/article/details/17151465
To develop a network game, the first thing to consider is the client server in what encoding format to communicate. Previously, we introduced the method of using protobuf for the Unity game front end. Today we are going to talk about how the service side makes Protobuf. The game service-side language is flourishing, in addition to the traditional C + +, Java,erlang,python have a lot of teams in use.
Today we recommend Java as the server development language. Many of the domestic excellent page and hand tours are used in Java as a service-side language. such as "The Divine Comedy" "Qin Beauty" "Dragon will" "time and Space Hunter"
And so on.
This paper mainly discusses how to simplify the development of PROTOBUF program.
According to the traditional development process, are as follows:
1. Write the proto file format first, for example
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- Message person {
- Required Int32 id = 1;
- Required String name = 2;
- Optional String email = 3;
- }
2. Run the compiler to generate the entity class Person.java
Protoc--java_out=./src./person.proto
3. Can be serialized and deserialized directly in the program using the related functions of the person class
[Java]View Plaincopy < param name= "wmode" value= "Transparent" >
- Serialization of
- Person person = builder.build ();
- byte[] buf = Person.tobytearray ();
- Deserialization
- Person Person2 = PersonProbuf.Person.parseFrom (BUF);
The above process seems very convenient, what is the problem? Can we improve it again? Let's take a look at the generated person class code, which has thousands of rows.
In fact, we only contain 3 variables, which is greatly reduced in readability. Second, the development process has to use external compilation tools to generate code each time.
So we may have a simple idea. Write your own class of person and then serialize and deserialize the class directly from the external API.
Directly abandoned the Protoc tool.
This idea, has been realized, the earliest is in the C # language version of the Protobuf-net implementation. It uses C # 's attribute (Java-like annotation annotations) + reflection to achieve protobuf serialization of ordinary entity classes and
Deserialization.
Ben wanted to convert Protobuf-net's C # code into Java code. Later in the overstack to someone introduced Protostuff, it is based on this way of thinking.
1. Writing the person class (to make the demo code compact, the fields are temporarily set to public)
[Java]View Plaincopy
- Public class person{
- public int id;
- Public String name;
- public String Email;
- }
2. Testing of serialization and deserialization
[Java]View Plaincopy
- Public static void Main (string[] args) throws IOException {
- ////The mode of the class is set to the person class
- schema<person> schema = Runtimeschema.getschema (person. Class);
- Person Person1 = new Person ();
- Person1.id = 10086;
- Person1.name = "Ken";
- Person1.email = "k[email protected]";
- //Cache buff
- Linkedbuffer buffer = linkedbuffer.allocate (1024);
- //serialization of binary data into Protobuf
- byte[] data = Protobufioutil.tobytearray (person1, schema, buffer);
- //Deserialization
- Person Person2 = new Person ();
- Protobufioutil.mergefrom (data, Person2, schema);
- System.out.println (person2.id);
- }
More features to read the relevant manuals in detail. For example, specify the serialization order of the Entity class fields, ignore some fields that are not serialized, and so on.
- Performance considerations
At first, given the Protostuff's reflection, would it be far worse than the official recommended reflection performance? Without investigation, there is no say. Someone has already tested, the performance aspect is similar, can rest assured use.
The specific test data can be consulted on the relevant data on the Internet, omitted in this.
The advantage of Protostuff is that it simplifies the development process so that we can develop more efficiently and with more focus. Have any questions welcome to explore [email protected]
Next, I'll use a full demo to concatenate the entire Unity client and server communication flow.
Unity Hands-On tour < two >java Edition Server uses Protostuff to simplify PROTOBUF development