日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

寒风之家 » Thrift压缩

發布時間:2023/12/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 寒风之家 » Thrift压缩 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寒風之家 ? Thrift壓縮

Thrift壓縮
Thrift壓縮
2012年9月25日 Bise??? 發表評論 閱讀評論

關于Thrift的壓縮有協議層面的壓縮和傳輸時的壓縮兩種。

協議層面的壓縮需要用到TCompactProtocol,而傳輸壓縮需要用到TZlibTransport(其實就是采用zlib壓縮)。

先給出接口定義文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
???
struct request_ad
{
1: byte mode,
2: byte device,
3: byte loctype = 1,
4: string location,
5: i32 adbar_id,
6: i32 time,
7: i64 pangu_id,
8: string page_id,
9: string machine_id,
10: string agent
}

struct result_ad
{
1: i32 status,
2: string debug,
3: string result
}

exception error_ad
{
1: i32 errno;
2: string error
}

service Ad
{
result_ad get_result(1: request_ad req) throws (1: error_ad err)
}
協議壓縮

編譯連接選項是-lthrift,協議為TCompactProtoco。

在服務端和客服端必須先包含頭文件“#include <config.h>”,否則會報錯“Unable to determine the behavior of a signed right shift”

服務端代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
???
#include <arpa/inet.h>
#include "block/Ad.h"
#include <config.h>
#include <protocol/TCompactProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

class AdHandler : virtual public AdIf {
?public:
? AdHandler() {
??? // Your initialization goes here
? }

? void get_result(result_ad& _return, const request_ad& req) {
??? // Your implementation goes here
??? //printf("get_result\n");
??? _return.debug.resize(1024, 'm');
??? _return.result.resize(8192, 'n');
? }

};

int main(int argc, char **argv)
{
? int port = 9090;
? shared_ptr<AdHandler> handler(new AdHandler());
? shared_ptr<TProcessor> processor(new AdProcessor(handler));
? shared_ptr<TServerTransport> serverTransport(
???????????????????????????? new TServerSocket(port));
? shared_ptr<TTransportFactory> transportFactory(
???????????????????????????? new TBufferedTransportFactory());
? shared_ptr<TProtocolFactory> protocolFactory(
???????????????????????????? new TCompactProtocolFactory());
? TSimpleServer server(processor, serverTransport,
?????????????????????????? transportFactory, protocolFactory);
? server.serve();
? return 0;
}

客服端代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
???
#include <arpa/inet.h>
#include "block/Ad.h"
#include <config.h>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TCompactProtocol.h>
#include <sys/time.h>
#include <unistd.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;

using boost::shared_ptr;

int main(int argc, char **argv) {
??? shared_ptr<TSocket> socket(
???????????? new TSocket("127.0.0.1", 9090));
??? shared_ptr<TTransport> transport(
?????????????? new TBufferedTransport(socket));
??? shared_ptr<TProtocol> protocol(
????????????? new TCompactProtocol(transport));
??? AdClient client(protocol);
??? transport->open();
??? request_ad req;
??? result_ad res;
??? req.page_id.resize(256, 'a');
??? req.machine_id.resize(256, 'b');
??? req.agent.resize(512, 'c');
??? client.get_result(res, req);
??? transport->close();
??? return 0;
}
傳輸壓縮

?
編譯連接選項是-lthrift -lthriftz
在服務端需要自己實現一個TZlibTransport 的工廠類
使用TZlibTransport時,為了提高效率,最好用TBufferedTransport來裝飾下(效率相差比較大)。
服務端代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
???
#include <arpa/inet.h>
#include "block/Ad.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>
#include <transport/TZlibTransport.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;
??
class AdHandler : virtual public AdIf {
?public:
? AdHandler() {
??? // Your initialization goes here
? }

? void get_result(result_ad& _return, const request_ad& req) {
??? // Your implementation goes here
??? printf("get_result\n");
??? _return.debug.resize(1024, 'm');
??? _return.result.resize(8192, 'n');
? }

};

class TZlibTransportFactory : public TTransportFactory {
?public:
? TZlibTransportFactory() {}

? virtual ~TZlibTransportFactory() {}

? /**
?? * Wraps the transport into a framed one.
?? */
? virtual boost::shared_ptr<TTransport>
????????? getTransport(shared_ptr<TTransport> trans) {
??? return shared_ptr<TTransport>(
????? new TBufferedTransport(shared_ptr<TTransport>(
?????????????????????? new TZlibTransport(trans))));
? }

};

int main(int argc, char **argv)
{
? int port = 9090;
? shared_ptr<AdHandler> handler(new AdHandler());
? shared_ptr<TProcessor> processor(new AdProcessor(handler));
? shared_ptr<TServerTransport> serverTransport(
???????????????????????????????????? new TServerSocket(port));
? shared_ptr<TTransportFactory> transportFactory(
?????????????????????????????????? new TZlibTransportFactory());
? shared_ptr<TProtocolFactory> protocolFactory(
????????????????????????????????? new TBinaryProtocolFactory());
? TSimpleServer server(processor, serverTransport,
???????????????????????????? transportFactory, protocolFactory);
? server.serve();
? return 0;
}

客服端代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
???
#include <arpa/inet.h>
#include "block/Ad.h"
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <transport/TZlibTransport.h>
#include <protocol/TBinaryProtocol.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;

using boost::shared_ptr;

int main(int argc, char **argv) {
??? shared_ptr<TSocket> socket(new TSocket("127.0.0.1", 9090));
??? shared_ptr<TZlibTransport> zlibtransport(
?????????????????????????????? new TZlibTransport(socket));
??? shared_ptr<TTransport> transport(
?????????????????????? new TBufferedTransport(zlibtransport));
??? shared_ptr<TProtocol> protocol(
?????????????????????? new TBinaryProtocol(transport));
??? AdClient client(protocol);
??? transport->open();
??? request_ad req;
??? result_ad res;
??
??? req.page_id.resize(256, 'a');
??? req.machine_id.resize(256, 'b');
??? req.agent.resize(512, 'c');
??? client.get_result(res, req);
??? printf("%s\n", res.debug.c_str());
??? client.get_result(res, req);
??? printf("%s\n", res.debug.c_str());
??? transport->close();
??? return 0;
}

總結

以上是生活随笔為你收集整理的寒风之家 » Thrift压缩的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。