欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

ThriftUsageC++ - Thrift Wiki

系統 2982 0

ThriftUsageC++ - Thrift Wiki

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

service Something {
  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");
  }

};

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:

?

      g++ -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -Wall -I/usr/local/include/thrift *.cpp -L/usr/local/lib -lthrift -o something
    

?

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 *.o -o Something_server -lthrift
    

?

Writing the client code

thrift does not auto generate a client interface, so you have to create the file.

?

      #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();
  transport->close();

  return 0;
}
    

?

Compiling

?

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

?

Linking

?

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

?

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 -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H $(INC) -c $Slt; -o $@

something_server: Something_server.o $(GEN_OBJ)
        $(CXX) $^ -o $@ -L/usr/local/lib -lthrift 

something_client: Something_client.o $(GEN_OBJ)
        $(CXX) $^ -o $@ -L/usr/local/lib -lthrift 

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();
    

PHP client code (you have to use TFramedTransport here):

          $transport = new TFramedTransport(new TSocket("localhost", 8888));
    $transport->open();
    $protocol = new TBinaryProtocol($transport);
    $client= new SomethingClient($protocol, $protocol);
    // do something here...
    transport->close();
    

ThriftUsageC++ - Thrift Wiki


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美在线观看视频一区 | 成熟人妻AV无码专区A片 | 亚洲黄色第一页 | 亚洲午夜电影 | 日本国产成人精品视频 | 97精品一区二区 | 婷婷激情综合五月天 | 欧美福利视频一区二区三区 | 国产精品久久久久久无码人妻 | 精品国产福利久久久 | 九九九色| 色综合97天天综合网 | 91短视频版在线观看免费大全 | 国产日产精品一区二区三区四区 | 午夜视频在线观看一区 | 亚洲一区电影 | 免费精品美女久久久久久久久久 | 国产精品 第1页 | 污的视频在线观看 | 国产亚洲精品久久久久久线投注 | 毛片a级| 九色av| 久久人人爽人人爽人人 | 国产午夜精品一区二区三区嫩草 | 超污视频在线看 | 日日摸夜夜添夜夜添破第一 | 在线不卡一区 | 免费看的黄网站 | 精品一卡2卡三卡4卡免费观看 | 精品欧美在线精品 | 搜一级毛片 | 波多野一区| 国产成人综合久久精品红 | www.久久草| 亚洲精品国产电影 | 一区二区三区免费视频 www | 国产精品福利视频免费观看 | 午夜资源在线 | 日本毛片爽看免费视频 | pornoⅹxxxxhd麻豆 | 爽毛片 |