Commit 59548597 authored by Ge Jun's avatar Ge Jun

Add a proxy example in example/thrift_extension_c++

parent f364cd37
......@@ -15,20 +15,22 @@ STATIC_LINKINGS += -lthrift -lgflags -lbrpc -levent
CLIENT_SOURCES = client.cpp
SERVER_SOURCES = server.cpp
SERVER2_SOURCES = server2.cpp
PROTOS = $(wildcard *.proto)
PROTO_OBJS = $(PROTOS:.proto=.pb.o)
PROTO_GENS = $(PROTOS:.proto=.pb.h) $(PROTOS:.proto=.pb.cc)
CLIENT_OBJS = $(addsuffix .o, $(basename $(CLIENT_SOURCES)))
SERVER_OBJS = $(addsuffix .o, $(basename $(SERVER_SOURCES)))
CLIENT_OBJS = $(addsuffix .o, $(basename $(CLIENT_SOURCES)))
SERVER_OBJS = $(addsuffix .o, $(basename $(SERVER_SOURCES)))
SERVER2_OBJS = $(addsuffix .o, $(basename $(SERVER2_SOURCES)))
.PHONY:all
all: echo_client echo_server native_server native_client libechothrift.a client.o server.o
all: echo_client echo_server echo_server2 native_server native_client libechothrift.a client.o server.o
.PHONY:clean
clean:
@echo "Cleaning"
@rm -rf echo_client echo_server $(PROTO_GENS) $(PROTO_OBJS) $(CLIENT_OBJS) $(SERVER_OBJS) native_server native_client EchoService.o echo_types.o libechothrift.a gen-cpp gen-py
@rm -rf echo_client echo_server echo_server2 $(PROTO_GENS) $(PROTO_OBJS) $(CLIENT_OBJS) $(SERVER_OBJS) $(SERVER2_OBJS) native_server native_client EchoService.o echo_types.o libechothrift.a gen-cpp gen-py
echo_client:$(PROTO_OBJS) $(CLIENT_OBJS) libechothrift.a
@echo "Linking $@"
......@@ -38,6 +40,10 @@ echo_server:$(PROTO_OBJS) $(SERVER_OBJS) libechothrift.a
@echo "Linking $@"
@$(CXX) $(LIBPATHS) $(SOPATHS) -Xlinker "-(" $^ libechothrift.a -Xlinker "-)" $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS) -o $@
echo_server2:$(PROTO_OBJS) $(SERVER2_OBJS) libechothrift.a
@echo "Linking $@"
@$(CXX) $(LIBPATHS) $(SOPATHS) -Xlinker "-(" $^ libechothrift.a -Xlinker "-)" $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS) -o $@
%.o:%.cpp libechothrift.a
@echo "Compiling $@"
@$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@
......
......@@ -62,7 +62,8 @@ int main(int argc, char* argv[]) {
example::EchoRequest req;
example::EchoResponse res;
req.data = "hello";
req.__set_data("hello");
req.__set_need_by_proxy(10);
stub.CallMethod("Echo", &cntl, &req, &res, NULL);
......@@ -71,7 +72,7 @@ int main(int argc, char* argv[]) {
sleep(1); // Remove this sleep in production code.
} else {
g_latency_recorder << cntl.latency_us();
LOG(INFO) << "Thrift Response: " << res.data;
LOG(INFO) << "Thrift Response: " << res;
}
LOG_EVERY_SECOND(INFO)
......
......@@ -2,8 +2,12 @@
namespace cpp example
struct EchoRequest {
1: required string data;
2: required i32 s;
1: optional string data;
2: optional i32 need_by_proxy;
}
struct ProxyRequest {
2: optional i32 need_by_proxy;
}
struct EchoResponse {
......
......@@ -51,15 +51,15 @@ int main(int argc, char **argv) {
transport->open();
example::EchoRequest req;
req.data = "hello";
req.__set_data("hello");
req.__set_need_by_proxy(10);
example::EchoResponse res;
while (1) {
try {
client.Echo(res, req);
LOG(INFO) << "Req: " << req.data
<< " Res: " << res.data;
LOG(INFO) << "Req=" << req << " Res=" << res;
} catch (std::exception& e) {
LOG(ERROR) << "Fail to rpc, " << e.what();
}
......
......@@ -51,7 +51,7 @@ public:
// to process the request asynchronously, pass done_guard.release().
brpc::ClosureGuard done_guard(done);
res->data = req->data + " (processed)";
res->data = req->data + " (Echo)";
}
};
......
......@@ -351,7 +351,6 @@ void ThriftClosure::DoRun() {
ParseResult ParseThriftMessage(butil::IOBuf* source,
Socket*, bool /*read_eof*/, const void* /*arg*/) {
char header_buf[sizeof(thrift_head_t) + 4];
const size_t n = source->copy_to(header_buf, sizeof(header_buf));
if (n < sizeof(header_buf)) {
......
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