警告

尽量使用tutorial下面的模板,注意脚本的格式,否则生成错误

使用thrift-0.9.0生成C++/csharp代码

使用的指令如下:

thrift-0.9.0.exe --gen cpp thriftcom.thrift

thrift-0.9.0.exe --gen csharp thriftcom.thrift


1 注释

有如下的三种方式

1)//

2)/**/

支持C语言的注释方法

3)#


2 命名空间

namespace cpp tutorial


namespace d tutorial


namespace java tutorial


namespace php tutorial


namespace perl tutorial


3 脚本嵌套

include "shared.thrift"


4 基本类型

* bool Boolean, one byte

* byte Signed byte

* i16 Signed 16-bit integer

* i32 Signed 32-bit integer

* i64 Signed 64-bit integer

* double 64-bit floating point value

* string String

* binary Blob (byte array)

* map<t1,t2> Map from one type to another

* list<t1> Ordered list of one type

* set<t1> Set of unique elements of one type


4 定义结构体


struct SharedStruct

{

1: i32 key;

2: string value;


}




5 例子


service SharedService

{

SharedStruct getStruct(1: i32 key);

}


6 双向通信使用oneway关键字修饰

oneway修饰一个方法的时候,意味着客户端仅仅是发送请求,而没有监听反馈信息,所以调用oneway修饰的函数会直接返回。oneway方法必须是void,这个非常容易理解,因为返回值实际上就保存在反馈信息当中,既然没有监听反馈信息,当然没有返回值链接提供了oneway方法在网络不佳情况下,可能会丢包的说明,暂时没有发现该问题。oneway定义的双向通信,主要是避免反馈消息互相干扰


参考

http://blog.csdn.net/lovemdx/article/details/9829457