[CPP]View Plaincopy
- More recently, the Protobuf interface has been provided for the needs of the work, and the processes and methods used are summarized as follows:
- Download protobuf-2.3.0:
- http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
- Installation:
- Unzip Protobuf-2.3.0.zip
- CD protobuf-2.3.0
- ./configure
- Make
- Make check
- Make install
- Results:
- Libraries has been installed in:
- /usr/local/lib
- Head files Hava been installed in:
- /usr/local/include/google/
- protobuf/
- Start writing. proto file:
- Basemessage.proto:
- Message Messagebase
- {
- required Int32 opcode = 1;
- //Other:sendmgrid, Sendid, Recvmgrid, Recvid, ...
- }
- Message Basemessage
- {
- Required Messagebase msgbase = 1;
- }
- Basemessage.proto is the basis for other message proto files, taking the c2s_getcontainerinfo of the container module as an example:
- Containermessage.proto:
- Import "Basemessage.proto";
- Message c2sgetcontainerinfomsg
- {
- Required Messagebase msgbase = 1;
- Optional Int32 containertype = 2;
- }
- . proto File Authoring rules:
- 1) All messages need to contain the msgbase, and the number is 1, namely:
- Required Messagebase msgbase = 1;
- 2) In addition to msgbase this written required, all other items are written in optional.
- Compiling the. Proto file
- Protoc-i=. --cpp_out=. Basemessage.proto
- Protoc-i=. --cpp_out=. Containermessage.proto
- Generate Basemessage.pb.h, BaseMessage.pb.cc
- Containermessage.pb.h, ContainerMessage.pb.cc
- Add them to the project file.
- Write C + + code:
- 1) Send message:
- C2sgetcontainerinfomsg msg;
- Msg.mutable_msgbase ()->set_opcode (c2s_getcontainerinfo);
- Msg.set_containertype (1);
- std::string out = Msg. Serializeasstring ();
- Send (SOCKFD, Out.c_str (), Out.size (), 0);
- 2) Receive messages
- Char buf[maxbuf + 1];
- int Len;
- Bzero (buf, maxbuf + 1);
- Len = recv (new_fd, buf, maxbuf, 0);
- if (len > 0)
- {
- printf ("%d received message succeeded: '%s ',%d bytes of data/n",
- NEW_FD, buf, Len);
- Basemessage basemsg;
- std::string data = BUF;
- basemsg.parsefromstring (data);
- int opcode = Basemsg.mutable_msgbase ()->opcode ();
- printf ("opcode=%d/n", opcode);
- switch (opcode)
- {
- Case C2s_getcontainerinfo:
- {
- C2sgetcontainerinfomsg msg;
- Msg. parsefromstring (data);
- printf ("containertype=%d/n", Msg.containertype ());
- Break ;
- }
- Default:
- {
- Break ;
- }
- }
- }
- Else
- {
- if (Len < 0)
- printf ("message received failed!") The error code is%d, and the error message is '%s '/n ',
- errno, Strerror (errno));
- Close (NEW_FD);
- return-1;
- }
- Compiling C + + code:
- Need to link lib:
- Protobuf
- Pthread
- Reference:
- 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml
- 2,http://code.google.com/p/protobuf/
Original address: http://blog.csdn.net/ganghust/article/details/6115283
Project home: http://code.google.com/p/protobuf/
Download: Http://code.google.com/p/protobuf/downloads/list protobuf-2.4.1.tar.gz
1,./configure (Note: The default may be installed in the/usr/local directory, you can add--PREFIX=/USR to specify the installation to/usr/lib, you can eliminate the path settings, the path settings see Linux command Pkg-config)
2. Make
3. Make check
4. Make install (requires Superuser root privileges)
Installation and use of Google protobuf