Resolution PROTOBUF cannot be used directly on iOS, using protobuf-net to communicate on iOS

Source: Internet
Author: User

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------

Earlier in the company to transplant the project to iOS, but protobuf, but there is no use of the situation, and later found on the Internet solution, thank the great God.

------------------------------------------------------------------------------------------------------ -------------------------------------------------------------

Original connection address:http://game.ceeger.com/forum/read.php?tid=14359&fid=27&page=1

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------

  The following is the author's own use of records, in order to facilitate their later use again to see, can not understand the author of the coarse language to the original view, do not like to spray-----------~ ~ ~-------------~~!!!

The first is to download protobuf-net, of course, I was using the r668 version, there is nothing new I also did not check. Okay, no nonsense, now it's time to get to the chase.

First

Download Protobuf-net, unzip to the directory you want, according to the original meaning to put two Precompile\precompile.exe and protogen\protogen.exe Two files are added to the environment variable and are then easy to use. The first time I used the environment variable set but do not know why not work, later I wrote him a bat file, only change the file name and path to solve everything.

CD C:\Users\Administrator\Desktop\Moblie\protobuf-net\ProtoGen
Protogen-i:backstagemessagecontract.proto-o:backstagemessagecontract.cs-ns:com.czkj.jsys

This is the author write a very frustrated bat file, in order to deepen the memory, the author decided to write again, all for the great programmer mission.

After the author's attempt to set the environment variable successfully this time, it seems that I have made some mistakes before.

This to the people who do not understand the setting: Http://wenku.baidu.com/link?url=y_ hm5uzgcbs4uwpliq7upct4qrwi5qahto2tpmjrd6jgoq5nqx6egiswpndbr4zhfgfbr3w9rbahxygo2drdtedtq_geedpwq28be5bixvi

  

Post a successful picture.

Second, proto document writing rules

Message MessageContract {
Required String contractid = 1;
Optional bytes BODY = 2;
Required Int64 date = 3;
}

Message reconnectioncontract{
Optional String sessonid= 1;
}

Message reconnectionresultcontract{
Required Int32 result = 1;
}

Message logincontract{
Required Int32 versioncode = 1;
Required String versionname = 2;
Optional String userName = 3;
Optional String password = 4;
Optional String touristcode = 5;
Optional String openId = 6;
Optional String txwmid = 7;
Optional String ip= 8;
}

Message loginresultcontract{
Required Int32 result = 1;
Optional String sessonid= 2;
Optional String userName = 3;
}

Here to explain the actual use of my proto, MessageContract is actually as a message overall, he sent the message to his body, and then have a connection type to identify Contractid, as for the data is the author used to record something , you can take your own discretion.

Here's a chestnut (I hope you like it)

For example, like the author to do a login connection, you can see that we have logincontract and loginresultcontract two message body (the author is a rookie, do not know this is called correct). As the literal meaning is, one is used to send a login message that is used to receive the login message. Maybe some people will ask, you say these I know, but how to use it.

Here's a chestnut, combined with my test project.

After we complete the whole package (the specific can read the following and then back to see).

Once we have generated the file, we can use the message body we just had. Don't say much nonsense!

Here we instantiate this message object, and then give the message body value, and then convert to byte[], and then the same way to assign the value to the total message body, finally sent out, specifically how to transform, the following time in detail. By the way, there may be people here who ask why you're writing a logincontract, but the reason is that when you pack it, he'll rename your message class to this type, In other words, you write the body of the message will be in accordance with this type of structure (I do not know the author understand there is no mistake or write something wrong, welcome to point out, thank you extremely).

Third, generate CS file

Using the Protogen command line to generate code (PRECOMPILE and Protogen can only be run under Windows, it may be possible to run this exe via mono on a Mac, but I haven't tried it yet. )

Examples of use of the original text:

Protogen-i:test1.proto-i:test2.proto-i:test3.proto-o:output.cs-ns:com.fbmly.model

-I is an input file that can have multiple

-O output of CS file, can only have one: If-I has more than one will generate all the code into this CS file

The-ns namespace is best used if the default namespace that is not used every time the build is proto is the file name.

In general, just make all the data structures into a CS (or else use multiple-I, or write all the PROTOBUF definitions in a proto file), so that later operation is more convenient.

CD C:\Users\Administrator\Desktop\Moblie\protobuf-net\ProtoGen
Protogen-i:backstagemessagecontract.proto-o:backstagemessagecontract.cs-ns:com.czkj.jsys

This is the author wrote a bat file content, in your directory to generate CS file, the author here to the files are lost there, we can modify the file path according to your preferences. The following-ns are namespaces, which you define yourself.

Iv. Compiling DLL libraries (you can view the original text directly)

Use the MonoDevelop tool to compile the CS file generated in the previous step into a DLL library.

1. Create a new project (file->new->solution)

2. In the popup dialog box, select C # and select Library.

3. Fill in the name of the project below (this name is the name of the generated DLL, so to be good, the author is backstagemessagecontract)

4. Click on the forward (then one more time OK)

5. Delete the default generated MyClass.cs file, and if necessary, fill in the AssemblyInfo.cs with some copyright information.

6. Add the previous step to the project with the CS file generated by Protogen (right-click on the left project name to add)

7. Add Protobuf-net Reference Library

Click on the project's References->edit References

Then select the DLL library under the Coreonly\ios, double-click the library file can be added to the right side (it is worth mentioning that there are different platform options included)

 

(after the author's verification, to use on Android, you can choose mono)

Finally check the references inside if there is Protobuf-net.dll is correct.

8. When done, click Build->build All to generate the DLL in the current project (the DLL's path is in the Debug or Release folder of your project)

Do not forget the choice of debug and release, after the test should be re-release and then compile once.

V, Precompiled Serialization Library

The DLL file that generated the data in the previous step requires the DLL file in the previous step to generate a specially serialized DLL file.

(The DLL file directory that was generated in the previous step will automatically copy the Protobuf-net library file too, so just keep this in mind.) For example, if you copy to another directory to do this, it will be important.

D:
CD D:\Documents\Projects\BackstageMessagecontract\BackstageMessagecontract\bin\Release
C:\Users\Administrator\Desktop\Moblie\protobuf-net\Precompile\Precompile Backstagemessagecontract.dll-o: Protobufserialize.dll-t:com.czkj.protobufserializer

This is the author's bat in the corresponding path to generate the DLL file we want. When you're done, you'll find three DLLs all together and throw them into the project.

The author uses unity, and after testing it runs through the unity compiler in Xcode and on the real machine.

Finally, the Code test

1. How to serialize data

Here the message has been packaged two times, the last to send a message, as to how to send anything is the great God provides the ready-made interface, have the opportunity to write a post, so that they also learn to learn.

2. How to Deserialize

The author uses the message mechanism provided by the great God to make a message callback. When the server returns the message, it executes the ABC here, then deserializes the result, assigns it to our message class, and then we can see the login result.

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------

--------------------------------------------The first time I write a blog, feel really tired of writing, Programmers are a really simple creature---------------------------------------

--------------------------------------------------------------------------------------------------------------- --------------------------------------

Resolve PROTOBUF cannot be used directly on iOS, use protobuf-net to communicate on iOS

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.