Thrift第三課 編寫腳本

警告

    儘量使用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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章