学习使用google protocol buffers(持续添加)

说明

http://code.google.com/apis/protocolbuffers/docs/overview.html

接口教程

http://code.google.com/apis/protocolbuffers/docs/tutorials.html

安装

wget http://protobuf.googlecode.com/files/protobuf-2.4.1.tar.bz2
tar zf protobuf-2.4.1.tar.bz2
cd protobuf-2.4.1
./configure
make
make install

查看安装是否成功

protoc --version

注意:如果提示相应库找不到,执行 sudo ln -s /usr/local/lib/xxxx.so.xxx /usr/lib/

生成jar文件

sudo apt-get install maven2
cd protobuf目录/java
mvn package

java
1)创建proto

package com.xingqiba;
option java_outer_classname = "MessageProtoHandler";

message ChatMessage {
  required int32 chatChannel = 1;
  optional string fromId = 2;
  optional string toId =3;
  optional string name =4;
  optional int32 groupId =5;
  optional string message = 6;
}

2)运行

protoc  --java_out=../src/ ./message_java.proto

3)测试

package com.xingqiba;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import com.google.protobuf.InvalidProtocolBufferException;
import com.xingqiba.MessageProtoHandler.ChatMessage;

public class TestGoogleProto {

	private static final Log log = LogFactory.getLog(TestGoogleProto.class);

	/**
	 * @param args
	 * @throws InvalidProtocolBufferException
	 */
	public static void main(String[] args) throws InvalidProtocolBufferException {
		PropertyConfigurator.configure("config/log4j.properties");
		ChatMessage chatMessage = ChatMessage.newBuilder()
				.setChatChannel(1)
				.setFromId("110")
				.setToId("120")
				.setGroupId(1)
				.setName("ixqbar@gmail.com")
				.setMessage("Hello Proto")
				.build();

		byte[] message = chatMessage.toByteArray();

		log.info(message);

		ChatMessage clientMessage = ChatMessage.parseFrom(message);
		System.out.print(clientMessage);
	}

}

protoc-gen-as3

window下使用actionscript的protoc-gen-as3步骤
1)下载protoc-gen-as3-1.0.0-rc6-bin.tar.gz 并解压到d:\google\as_plugin目录
2)下载protoc-2.4.1-win32.zip 并解压到d:\google\
3)创建d:\google\as_out\目录
4)创建d:\google\message.proto

message Person {
    required string name = 1;
    required int32 id = 2;
}

5)开打cmd运行命令

protoc.exe --plugin=protoc-gen-as3="protoc-gen-as3.bat" --as3_out=d:\google\as_out message.proto

目录d:\google\as_out\下自动生成initializer.as.inc,Person.as文件
6)打开flash builder创建应用,引入d:\google\as_plugin\protobuf.swc文件
7)测试代码

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[

            public function init(): void {
                var a:Person = new Person();
                a.id = 1111;
                a.name="kankan";
                var b:ByteArray = new ByteArray();
                a.writeTo(b);

                var person2:Person = new Person();
                b.position = 0;//至关重要
                person2.mergeFrom(b);
                log.appendText("log2:" + person2.name + person2.id);
            }
        ]]>
    </fx:Script>
    <s:TextArea x="174" y="316" id="log"/>
</s:Application>

php
参看试用pb4php发送系统公告给聊天服务器

更多有关proto-gen-as3的使用可以参看wiki

此条目发表在 技术杂谈 分类目录,贴了 , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已被标记为 *

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>