Commit a9d8d2a8 authored by wangxuefeng's avatar wangxuefeng

Add Cmake support for thrift

parent a97e3d2a
...@@ -20,6 +20,7 @@ endif() ...@@ -20,6 +20,7 @@ endif()
option(BRPC_WITH_GLOG "With glog" OFF) option(BRPC_WITH_GLOG "With glog" OFF)
option(DEBUG "Print debug logs" OFF) option(DEBUG "Print debug logs" OFF)
option(WITH_DEBUG_SYMBOLS "With debug symbols" ON) option(WITH_DEBUG_SYMBOLS "With debug symbols" ON)
option(BRPC_WITH_THRIFT "With thrift framed protocol supported" OFF)
option(BUILD_UNIT_TESTS "Whether to build unit tests" OFF) option(BUILD_UNIT_TESTS "Whether to build unit tests" OFF)
set(WITH_GLOG_VAL "0") set(WITH_GLOG_VAL "0")
...@@ -31,6 +32,12 @@ if(WITH_DEBUG_SYMBOLS) ...@@ -31,6 +32,12 @@ if(WITH_DEBUG_SYMBOLS)
set(DEBUG_SYMBOL "-g") set(DEBUG_SYMBOL "-g")
endif() endif()
if(BRPC_WITH_THRIFT)
set(THRIFT_CPP_FLAG "-DENABLE_THRIFT_FRAMED_PROTOCOL")
set(THRIFT_LIB "thriftnb")
message("Enable thrift framed procotol")
endif()
include(GNUInstallDirs) include(GNUInstallDirs)
configure_file(${CMAKE_SOURCE_DIR}/config.h.in ${CMAKE_SOURCE_DIR}/src/butil/config.h @ONLY) configure_file(${CMAKE_SOURCE_DIR}/config.h.in ${CMAKE_SOURCE_DIR}/src/butil/config.h @ONLY)
...@@ -62,7 +69,7 @@ execute_process( ...@@ -62,7 +69,7 @@ execute_process(
set(CMAKE_CPP_FLAGS "-DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}") set(CMAKE_CPP_FLAGS "-DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DBRPC_REVISION=\\\"${BRPC_REVISION}\\\" -D__STRICT_ANSI__") set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DBRPC_REVISION=\\\"${BRPC_REVISION}\\\" -D__STRICT_ANSI__")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ${DEBUG_SYMBOL}") set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ${DEBUG_SYMBOL} ${THRIFT_CPP_FLAG}")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer") set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS "${CMAKE_CPP_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-unused-parameter -fno-omit-frame-pointer") set(CMAKE_C_FLAGS "${CMAKE_CPP_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-unused-parameter -fno-omit-frame-pointer")
...@@ -127,6 +134,7 @@ set(DYNAMIC_LIB ...@@ -127,6 +134,7 @@ set(DYNAMIC_LIB
${LEVELDB_LIB} ${LEVELDB_LIB}
${PROTOC_LIB} ${PROTOC_LIB}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
${THRIFT_LIB}
rt rt
ssl ssl
crypto crypto
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <gflags/gflags.h> #include <gflags/gflags.h>
#include <butil/logging.h> #include <butil/logging.h>
#include <butil/thrift_utils.h>
#include <brpc/server.h> #include <brpc/server.h>
#include <brpc/thrift_service.h> #include <brpc/thrift_service.h>
...@@ -30,7 +29,7 @@ DEFINE_int32(port, 8019, "TCP Port of this server"); ...@@ -30,7 +29,7 @@ DEFINE_int32(port, 8019, "TCP Port of this server");
DEFINE_int32(port2, 8018, "TCP Port of this server"); DEFINE_int32(port2, 8018, "TCP Port of this server");
DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no " DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no "
"read/write operations during the last `idle_timeout_s'"); "read/write operations during the last `idle_timeout_s'");
DEFINE_int32(max_concurrency, 1, "Limit of request processing in parallel"); DEFINE_int32(max_concurrency, 0, "Limit of request processing in parallel");
class EchoServiceHandler : virtual public example::EchoServiceIf { class EchoServiceHandler : virtual public example::EchoServiceIf {
public: public:
...@@ -64,15 +63,14 @@ public: ...@@ -64,15 +63,14 @@ public:
return; return;
} }
// Just an example, you don't need to new the processor each time. auto handler = new EchoServiceHandler();
boost::shared_ptr<EchoServiceHandler> service_hander(new EchoServiceHandler()); example::EchoRequest* req = request->cast<example::EchoRequest>();
boost::shared_ptr<example::EchoServiceProcessor> processor( example::EchoResponse* res = response->cast<example::EchoResponse>();
new example::EchoServiceProcessor(service_hander));
if (brpc_thrift_server_helper(request, response, processor)) { // process with req and res
LOG(INFO) << "success to process thrift request in brpc"; handler->Echo(*res, *req);
} else {
LOG(INFO) << "failed to process thrift request in brpc"; LOG(INFO) << "success to process thrift request in brpc with handler";
}
} }
......
...@@ -29,6 +29,11 @@ if(BRPC_WITH_GLOG) ...@@ -29,6 +29,11 @@ if(BRPC_WITH_GLOG)
target_link_libraries(brpc-shared ${GLOG_LIB}) target_link_libraries(brpc-shared ${GLOG_LIB})
endif() endif()
if(BRPC_WITH_THRIFT)
target_link_libraries(brpc-shared thrift)
target_link_libraries(brpc-static thrift)
endif()
SET_TARGET_PROPERTIES(brpc-static PROPERTIES OUTPUT_NAME brpc CLEAN_DIRECT_OUTPUT 1) SET_TARGET_PROPERTIES(brpc-static PROPERTIES OUTPUT_NAME brpc CLEAN_DIRECT_OUTPUT 1)
SET_TARGET_PROPERTIES(brpc-shared PROPERTIES OUTPUT_NAME brpc CLEAN_DIRECT_OUTPUT 1) SET_TARGET_PROPERTIES(brpc-shared PROPERTIES OUTPUT_NAME brpc CLEAN_DIRECT_OUTPUT 1)
......
...@@ -427,13 +427,11 @@ void ProcessThriftFramedResponse(InputMessageBase* msg_base) { ...@@ -427,13 +427,11 @@ void ProcessThriftFramedResponse(InputMessageBase* msg_base) {
uint32_t body_len = response->head.body_len; uint32_t body_len = response->head.body_len;
// Deserialize binary message to thrift message // Deserialize binary message to thrift message
uint8_t* thrift_buffer = std::unique_ptr<uint8_t[]>thrift_buffer(new uint8_t[body_len]);
static_cast<uint8_t*>(new uint8_t[body_len]);
const size_t k = response->body.copy_to(thrift_buffer, body_len); const size_t k = response->body.copy_to(thrift_buffer.get(), body_len);
if ( k != body_len) { if ( k != body_len) {
cntl->SetFailed("copy response body to thrift buffer failed!"); cntl->SetFailed("copy response body to thrift buffer failed!");
delete [] thrift_buffer;
return; return;
} }
...@@ -442,8 +440,7 @@ void ProcessThriftFramedResponse(InputMessageBase* msg_base) { ...@@ -442,8 +440,7 @@ void ProcessThriftFramedResponse(InputMessageBase* msg_base) {
auto in_portocol = auto in_portocol =
boost::make_shared<apache::thrift::protocol::TBinaryProtocol>(in_buffer); boost::make_shared<apache::thrift::protocol::TBinaryProtocol>(in_buffer);
in_buffer->resetBuffer(thrift_buffer, body_len, in_buffer->resetBuffer(thrift_buffer.get(), body_len);
::apache::thrift::transport::TMemoryBuffer::TAKE_OWNERSHIP);
// The following code was taken from thrift auto generate code // The following code was taken from thrift auto generate code
int32_t rseqid = 0; int32_t rseqid = 0;
......
...@@ -121,15 +121,14 @@ public: ...@@ -121,15 +121,14 @@ public:
// Cut the thrift buffer and parse thrift message // Cut the thrift buffer and parse thrift message
size_t body_len = head.body_len; size_t body_len = head.body_len;
auto thrift_buffer = static_cast<uint8_t*>(new uint8_t[body_len]); std::unique_ptr<uint8_t[]>thrift_buffer(new uint8_t[body_len]);
const size_t k = body.copy_to(thrift_buffer, body_len); const size_t k = body.copy_to(thrift_buffer.get(), body_len);
if ( k != body_len) { if ( k != body_len) {
delete [] thrift_buffer;
return false; return false;
} }
in_buffer->resetBuffer(thrift_buffer, body_len); in_buffer->resetBuffer(thrift_buffer.get(), body_len);
// The following code was taken and modified from thrift auto generated code // The following code was taken and modified from thrift auto generated code
...@@ -174,9 +173,6 @@ public: ...@@ -174,9 +173,6 @@ public:
in_portocol->readMessageEnd(); in_portocol->readMessageEnd();
in_portocol->getTransport()->readEnd(); in_portocol->getTransport()->readEnd();
// End thrfit auto generated code // End thrfit auto generated code
delete [] thrift_buffer;
} }
thrift_raw_instance_deleter = &thrift_framed_message_deleter<T>; thrift_raw_instance_deleter = &thrift_framed_message_deleter<T>;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment