亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

用thrift實現客戶端和服務端的C++代碼 - 金美光

系統 3184 0

用thrift實現客戶端和服務端的C++代碼 - 金美光的小屋 - 博客頻道 - CSDN.NET

用thrift實現客戶端和服務端的C++代碼

分類: thrift 1058人閱讀 評論 (0) 收藏 舉報

Getting started

The first thing you need to know is that the C++ code generated by Thrift compiles only on Unix based systems, although some success has been reported using Cygwin on Win32 in ? ThriftInstallationWin32 .

Requirements

Make sure that your system meets the requirements as noted in ? ThriftRequirements

  • Thrift library files
  • Thrift header files.

Installing the Thrift library

Installing the Thrift library is trivial to link with the generated code.

1. ??? Download a snapshot of Thrift and extract if you haven't done so already - ? Direct Link

2. ??? Navigate to ? lib/cpp ? using the terminal of your choice

3. ??? Run ? ?make ? to compile

4. ??? Run ? ?make?install ? to install the library. Your user needs root permissions.

Generating the server code

In this example we use an imaginary file called ? your_thrift_file.thrift :

#!/usr/local/bin/thrift --gen cpp

?

namespace cpp Test

/*

struct param ??? //thrift 中的 struct 形式其實是參數,客戶端要傳遞的參數類型必須在 thrift 文件中用 struct 先進行定義。注意只是客戶端要傳遞的參數,服務器端的參數無需在 thrift 文件中定義。

{

? 1:double left,

? 2:double right,

}

/*

service Something { // service 中聲明要實現的函數

? i32 ping () ?? // 服務器端要實現的函數的聲明

}

Now run:

thrift --gen cpp your_thrift_file.thrift

Exploring the generated code - The Server

The generated code should be under the ? gen-cpp ? directory. You will see a number of generated C++ and header files along with an automatically generated server skeleton code (in bold).

  • Something.cpp
  • Something.h
  • Something_server.skeleton.cpp
  • your_thrift_file_constants.cpp
  • your_thrift_file_constants.h
  • your_thrift_file_types.cpp
  • your_thrift_file_types.h

Implementing the Server

Copy the generated server skeleton to a file named Something_server.cpp and keep the original:

cp Something_server.skeleton.cpp Something_server.cpp

When this server is run in console it prints "ping" on the console window each time the function is called from a client.

Here's the autogenerated skeleton file to illustrate how to write a server: ? Something_server.cpp

#include "Something.h"

#include <protocol/TBinaryProtocol.h>

#include <server/TSimpleServer.h>

#include <transport/TServerSocket.h>

#include <transport/TTransportUtils.h>

?

using namespace apache::thrift;

using namespace apache::thrift::protocol;

using namespace apache::thrift::transport;

using namespace apache::thrift::server;

?

using boost::shared_ptr;

?

using namespace Test;

?

class SomethingHandler : virtual public SomethingIf {

? public:

? SomethingHandler() {

?? // Your initialization goes here

? }

?

? int32_t ping() {

?? // Your implementation goes here

printf("ping/n");

應該添加一條返回語句,源程序省略了,在此補上。

return 0;

? }

?

};

?

int main(int argc, char **argv) {

? int port = 9090; // 端口是隨機分配的 可修改,但要保持一致

? shared_ptr<SomethingHandler> handler(new SomethingHandler());

? shared_ptr<TProcessor> processor(new SomethingProcessor(handler));

? shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));

? shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());

? shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

?

? TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);

? server.serve();

? return 0;

}

Compiling/Building the server

To quickly build a binary using a single command use:

mv Something_server.skeleton.cpp Something_client.cpp.backup

g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o something ?

// 這里首先得把 Something_server.skeleton.cpp 的文件名做修改否則編譯時會出現問題,我們不妨先把 Something_server.skeleton.cpp 文件名改成 Something_client.cpp.backup ,最后在把文件名改成 cpp 格式,并把 client cpp 代碼寫在這里頭

Compiling

You need to point your compiler to the thrift include path (CXX flag: ? ?-I/usr/local/include/thrift )

g++ -Wall -I/usr/local/include/thrift -c Something.cpp -o something.o

g++ -Wall -I/usr/local/include/thrift -c Something_server.cpp -o server.o

g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_constants.cpp -o constants.o

g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_types.cpp -o types.o

Linking

You need to point your linker to the thrift library. (Linker flag: ? ?-lthrift? ? or ? ?-l/usr/local/lib/libthrift.so?

g++ -L/usr/local/lib -lthrift *.o -o Something_server

Writing the client code

thrift ? generates a client interface, but you have to hook it up a bit on your own.

以下的代碼是標準的客戶端代碼模板,把這段代碼 copy client cpp 文件中,黃色標記的部分是主要需要修改的地方。

#include "Something.h" ? // As an example

?

#include <transport/TSocket.h>

#include <transport/TBufferTransports.h>

#include <protocol/TBinaryProtocol.h>

?

using namespace apache::thrift;

using namespace apache::thrift::protocol;

using namespace apache::thrift::transport;

?

using namespace Test;

?

int main(int argc, char **argv) {

? boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));// 注意端口號的匹配

? boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));

? boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

?

? SomethingClient client(protocol);

? transport->open();

client.ping(); /* 客戶端只要寫了這條語句,服務器就將執行。

???????????????? 其他的語句都是套話??梢灾苯? copy

????????????? ping 是執行的函數名,在 thrift 中聲明了。

如果函數的原型,既 ping 是一個帶參數的函數,則使用 ping 函數之前先進行參數的賦值等操作,如

? param op; 這個 param 結構體也需要在 thrift 文件中先定義。

? op.left = 2.0;

? op.right = 3.5;

client.ping(op);*/

? transport->close();

?

? return 0;

}

Compiling

再次把 Something_client.cpp.backup 改成 Something_client.cpp

mv Something_client.cpp.backup Something_client.cpp

g++ -Wall -I/usr/local/include/thrift -c Something_client.cpp -o client.o

Linking

g++ -L/usr/local/lib -lthrift client.o something.o constants.o types.o -o Something_client

也可以把所有的語句做成一個 makefile ,如下

Compiling/Building everything with Makefile

GEN_SRC := Something.cpp your_thrift_file_constants.cpp your_thrift_file_types.cpp

GEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC))

?

THRIFT_DIR := /usr/local/include/thrift

BOOST_DIR := /usr/local/include

?

INC := -I$(THRIFT_DIR) -I$(BOOST_DIR)

?

.PHONY: all clean

?

all: something_server something_client

?

%.o: %.cpp

??????? $(CXX) -Wall $(INC) -c $Slt; -o $@

?

something_server: Something_server.o $(GEN_OBJ)

??????? $(CXX) -L/usr/local/lib -lthrift $^ -o $@

?

something_client: Something_client.o $(GEN_OBJ)

??????? $(CXX) -L/usr/local/lib -lthrift $^ -o $@

?

clean:

??????? $(RM) *.o something_server something_client

Appendix: About TNonblockingServer

If you are writing an application that will serve a lot of connection (like php front end calling thrift service), you'd better use TNonblockingServer. TNonblockingServer can accept a lot of connections while throttling the processor threads using a pool.

* TNonblockingServer with a thread pool is the c++ alternative of the JAVA THsHaServer; ? * TNonblockingServer withOUT a thread pool is the c++ alternative of the JAVA TNonblockingServer;

Server code with thread pool:

??? shared_ptr<TProcessor> processor(new SomethingProcessor(handler));

??? shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

?

??? // using thread pool with maximum 15 threads to handle incoming requests

??? shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(15);

??? shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());

??? threadManager->threadFactory(threadFactory);

??? threadManager->start();

??? TNonblockingServer server(processor, protocolFactory, 8888, threadManager);

??? server.serve();

?

??? // ...

C++ client code (you have to use TFramedTransport here):

??? boost::shared_ptr<TSocket> socket(new TSocket("localhost", 8888));

??? boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));

??? boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

?

??? SomethingClient client(protocol);

??? transport->open();

??? // do something here...

??? transport->close();

用thrift實現客戶端和服務端的C++代碼 - 金美光的小屋 - 博客頻道 - CSDN.NET


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 九九精品久久久久久久久 | 伊人五月 | 免费黄色小视频在线观看 | www.男人的天堂.com | 欧美一级爱爱视频 | 四虎影视网站 | 国产成人精品久久二区二区 | 久久97久久97精品免视看 | 中文字幕在线看视频一区二区三区 | 国产成人18黄网站免费 | 中文字幕久久亚洲一区 | 成人午夜在线观看 | 色综合社区 | 中文字幕伦视频 | 国产日韩精品视频一区二区三区 | 国产黄色91| 欧美aaaa黄色一级毛片 | 国产深夜福利19禁在线播放 | 国产女人精品性视频 | 久久久这里只有免费精品2018 | 国产一区二区三区在线观看视频 | 国产精品亚洲综合一区在线观看 | 久草在线在线精品观看 | 97在线免费观看视频 | 国产精品久久久久影院色 | 在线观看国产一区二三区 | 性欧美一级毛片欧美片 | 色综合天天综合给合国产 | 亚洲欧美日韩精品高清 | 国产3级在线观看 | 九九伦理影院手机观看 | 视频二区精品中文字幕 | 牛人盗摄一区二区三区视频 | 久草免费在线视频 | 日日干天天| 91久久青草精品38国产 | 特黄aaaaaaaaa真人毛片 | 四虎亚洲国产成人久久精品 | 松永纱奈在线观看 | 91精品国产91久久久久久 | 久久精品乱子伦免费 |