Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions muduo/net/protorpc/RpcChannel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ void RpcChannel::onRpcMessage(const TcpConnectionPtr& conn,
google::protobuf::Message* response = service->GetResponsePrototype(method).New();
// response is deleted in doneCallback
int64_t id = message.id();
doneCallbackArg arg{response, id};
service->CallMethod(method, NULL, get_pointer(request), response,
NewCallback(this, &RpcChannel::doneCallback, response, id));
NewCallback(&RpcChannel::doneCallback, std::weak_ptr<RpcChannel>(shared_from_this()), arg));
error = NO_ERROR;
}
else
Expand Down Expand Up @@ -172,13 +173,17 @@ void RpcChannel::onRpcMessage(const TcpConnectionPtr& conn,
}
}

void RpcChannel::doneCallback(::google::protobuf::Message* response, int64_t id)
void RpcChannel::doneCallback(const std::weak_ptr<RpcChannel> wkChannel, const doneCallbackArg arg)
{
std::unique_ptr<google::protobuf::Message> d(response);
RpcMessage message;
message.set_type(RESPONSE);
message.set_id(id);
message.set_response(response->SerializeAsString()); // FIXME: error check
codec_.send(conn_, message);
std::unique_ptr<google::protobuf::Message> d(arg.response);
RpcChannelPtr channel(wkChannel.lock());
if (channel)
{
RpcMessage message;
message.set_type(RESPONSE);
message.set_id(arg.id);
message.set_response(arg.response->SerializeAsString()); // FIXME: error check
channel->codec_.send(channel->conn_, message);
}
}

11 changes: 9 additions & 2 deletions muduo/net/protorpc/RpcChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ namespace net
// RpcChannel* channel = new MyRpcChannel("remotehost.example.com:1234");
// MyService* service = new MyService::Stub(channel);
// service->MyMethod(request, &response, callback);
class RpcChannel : public ::google::protobuf::RpcChannel
class RpcChannel : public ::google::protobuf::RpcChannel,
public std::enable_shared_from_this<RpcChannel>
{
public:
RpcChannel();
Expand Down Expand Up @@ -123,11 +124,17 @@ class RpcChannel : public ::google::protobuf::RpcChannel
Timestamp receiveTime);

private:
struct doneCallbackArg
{
::google::protobuf::Message* response;
int64_t id;
};

void onRpcMessage(const TcpConnectionPtr& conn,
const RpcMessagePtr& messagePtr,
Timestamp receiveTime);

void doneCallback(::google::protobuf::Message* response, int64_t id);
static void doneCallback(const std::weak_ptr<RpcChannel> wkChannel, const doneCallbackArg arg);

struct OutstandingCall
{
Expand Down