Unity Hands-On tour < two >java Edition Server uses Protostuff to simplify PROTOBUF development

Source: Internet
Author: User

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.

    • Traditional way

According to the traditional development process, are as follows:

1. Write the proto file format first, for example

[Plain]View Plaincopy
    1. Message person {
    2. Required Int32 id = 1;
    3. Required String name = 2;
    4. Optional String email = 3;
    5. }


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
    1. Serialization of
    2. Person person = builder.build ();
    3. byte[] buf = Person.tobytearray ();
    4. Deserialization
    5. 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.

    • Improved version

1. Writing the person class (to make the demo code compact, the fields are temporarily set to public)

[Java]View Plaincopy
    1. Public class person{
    2. public int id;
    3. Public String name;
    4. public String Email;
    5. }


2. Testing of serialization and deserialization

[Java]View Plaincopy
  1. Public static void Main (string[] args) throws IOException {
  2. ////The mode of the class is set to the person class
  3. schema<person> schema = Runtimeschema.getschema (person.   Class);
  4. Person Person1 = new Person ();
  5. Person1.id = 10086;
  6. Person1.name = "Ken";
  7. Person1.email = "[email protected]";
  8. //Cache buff
  9. Linkedbuffer buffer = linkedbuffer.allocate (1024);
  10. //serialization of binary data into Protobuf
  11. byte[] data = Protobufioutil.tobytearray (person1, schema, buffer);
  12. //Deserialization
  13. Person Person2 = new Person ();
  14. Protobufioutil.mergefrom (data, Person2, schema);
  15. System.out.println (person2.id);
  16. }


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.

    • Summarize

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

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.