Skip to content

Commit

Permalink
server replies via non-native-table api #7
Browse files Browse the repository at this point in the history
  • Loading branch information
GothAck committed Mar 13, 2020
1 parent b0d7d7d commit 2c96148
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
9 changes: 3 additions & 6 deletions compiler/flatrpcc_inja/flatrpc.hpp.inja
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,22 @@ private:
protected:
std::string handleRequest(std::string &&reqData, const TRouting &routing) {
auto reqInt = unpackInt(std::move(reqData));
auto repInt = makeReply(reqInt);
const auto replyType = getReplyType(reqInt->type);

try {
## for call in service.calls
if (reqInt->name == "{{ call.name }}") {
auto req = unpack<{{ call.request.cpp }}T>({reqInt->data.data(), reqInt->data.data() + reqInt->data.size()});
auto rep = handle{{ call.name }}(move(req), routing).get();
repInt->data = pack(rep);
return packInt(reqInt->requestId, replyType, reqInt->name, pack(rep));
} else
## endfor
{
throw exception("Unknown request name");
}
} catch (std::exception &e) {
repInt->exception = std::make_shared<flatrpc::rpc::ExceptionT>();
repInt->exception->what = e.what();
return packInt(reqInt->requestId, replyType, reqInt->name, e);
}

return packInt(std::move(repInt));
}
public:
## for call in service.calls
Expand Down
2 changes: 1 addition & 1 deletion rpc/include/flatrpc/rpcbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ class RpcBase : public RpcSockets, public virtual RpcRunnable {

TIntNativePtr unpackInt(std::string &&data);

std::string packInt(TIntNativePtr nativePtr);
std::string packInt(uint64_t requestId, flatrpc::rpc::RPCType type, const std::string& name, std::vector<signed char> data);
std::string packInt(uint64_t requestId, flatrpc::rpc::RPCType type, const std::string& name, std::exception &exception);

flatrpc::rpc::RPCType getReplyType(flatrpc::rpc::RPCType type);
TIntNativePtr makeReply(TIntNativePtr req);

zmqpp::context &_context;
Expand Down
27 changes: 10 additions & 17 deletions rpc/rpcbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,22 @@ RpcBase::TIntNativePtr RpcBase::unpackInt(std::string &&data) {
return ptr;
}

std::string RpcBase::packInt(RpcBase::TIntNativePtr nativePtr) {
flatbuffers::FlatBufferBuilder fbb;
flatbuffers::Offset<TIntRpc> rep;

rep = TIntRpc::Pack(fbb, nativePtr.get());
fbb.Finish(rep);

std::string str(fbb.GetBufferPointer(), fbb.GetBufferPointer() + fbb.GetSize());
return str;
}

RpcBase::TIntNativePtr RpcBase::makeReply(TIntNativePtr req) {
auto rep = make_shared<TIntNative>();
rep->requestId = req->requestId;
switch(req->type) {
flatrpc::rpc::RPCType RpcBase::getReplyType(flatrpc::rpc::RPCType type) {
switch(type) {
case flatrpc::rpc::RPCType::CLIENT_REQ:
case flatrpc::rpc::RPCType::SERVER_REQ:
rep->type = static_cast<flatrpc::rpc::RPCType>(
static_cast<uint8_t>(req->type) << 1);
return static_cast<flatrpc::rpc::RPCType>(
static_cast<uint8_t>(type) << 1);
break;
default:
throw exception("Cannot make reply from a reply");
}
}

RpcBase::TIntNativePtr RpcBase::makeReply(TIntNativePtr req) {
auto rep = make_shared<TIntNative>();
rep->requestId = req->requestId;
rep->type = getReplyType(req->type);
rep->name = req->name;
return rep;
}
Expand Down

0 comments on commit 2c96148

Please sign in to comment.