1. 程式人生 > >Thrift第三課 編寫腳本

Thrift第三課 編寫腳本

存在 sharp 註意 space 沒有 truct php val other

警告

盡量使用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方法必須是void,這個非常容易理解,因為返回值實際上就保存在

反饋信息當中,既然沒有監聽反饋信息,當然沒有返回值

鏈接提供了oneway方法在網絡不佳情況下,可能會丟包的說明

oneway定義的雙向通信,主要是避免反饋消息互相幹擾


參考

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


Thrift第三課 編寫腳本