1. 程式人生 > >thrift c++介面簡單 啟動一個簡單的server

thrift c++介面簡單 啟動一個簡單的server

我使用的是mac下安裝的,首先是安裝的是thrift,我直接用brew命令安裝的,不過可以直接去官網上下載thrift安裝,下載thrift之後可以檢視 thrift在哪個位置   which thrift,  主要是為了方便後面CMakeLists.txt中配置,先放出來:

include_directories(/usr/local/homebrew/include)
link_directories(/usr/local/homebrew/lib)

關於shared_ptr,老版本是用boost下面的,新版本改為標準庫下面的,這點要主要了,目前網上的程式碼基本上都是boost的寫法

使用thrift生成檔案,首先看student.thrift的內容:

struct Student{
    1: i32 sno,
    2: string sname,
    3: bool ssex,
    4: i16 sage,
}

service Serv{
     void put(1: Student s),
     i32 icall(1: Student s),
     string scall(1: Student s),
     Student stcall(1: Student s),
}

用命令   thrift -r –gen cpp student.thrift 生成,把生成的檔案拷貝到 專案下面的student目錄下面,首先sercer.cpp   client.cpp

server.cpp

#include "student/Serv.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>


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

class ServHandler : virtual public ServIf {
public:
    ServHandler() {
        // Your initialization goes here
    }

    void put(const Student& s) {
        // Your implementation goes here
        printf("put\n");
        printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
    }

    int32_t icall(const Student& s) {
        // Your implementation goes here
        printf("icall\n");
        printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
        return s.sage;
    }

    void scall(std::string& _return, const Student& s) {
        // Your implementation goes here
        printf("scall\n");
        printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
        _return = s.sname;
    }

    void stcall(Student& stu, const Student& s) {
        // Your implementation goes here
        printf("stcall\n");
        printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
        stu.sno     = s.sno + 1;
        stu.sname   = s.sname + "123";
        stu.ssex    = s.ssex;
        stu.sage    = s.sage + 10;
    }

};

int main(int argc, char **argv) {
    int port = 9090;
    stdcxx::shared_ptr<ServHandler> handler(new ServHandler());
    stdcxx::shared_ptr<TProcessor> processor(new ServProcessor(handler));
    stdcxx::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
    stdcxx::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    stdcxx::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

    TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    server.serve();
    return 0;
}

client.cpp:

#include "student/Serv.h"


#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>


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



int main(int argc, char **argv)
{
    stdcxx::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
    stdcxx::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

    transport->open();

    //呼叫server服務
    Student s;
    s.sno = 123;
    s.sname = "hao973";
    s.ssex = 1;
    s.sage = 30;

    ServClient client(protocol);
    printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
    //put
    client.put(s);
    //icall scall
    std::string strname = "";
    client.scall(strname, s);
    printf("icall=%d, scall=%s\n", client.icall(s), strname.c_str());
    //stcall

    Student stu;
    s.sno = 123;
    s.sname = "hao973";
    s.ssex = 1;
    s.sage = 30;
    client.stcall(stu, s);
    printf("student sno=%d sname=%s ssex=%d sage=%d\n", stu.sno, stu.sname.c_str(), stu.ssex, stu.sage);

    transport->close();

    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(credit_excise)

set(CMAKE_CXX_STANDARD 11)

include_directories(/usr/local/homebrew/include)
link_directories(/usr/local/homebrew/lib)

set(student_SOURCES
        student/Serv.cpp
        student/student_constants.cpp
        student/student_types.cpp
        server.cpp
        )

# 生成靜態庫目標
add_library(student STATIC ${student_SOURCES})
target_link_libraries(student thrift)

# 同下
add_executable(server server.cpp)
target_link_libraries(server student thrift)

# 生成 demo_client 可執行程式,要求連結 demo 靜態庫, thrift XX庫
add_executable(client client.cpp)
target_link_libraries(client student thrift)

使用cmake 和make命令編譯,

cd  target

cmake ..

make

生成server、client可執行檔案,

./server

./client既可以執行