Skip to content

Latest commit

 

History

History
129 lines (98 loc) · 4.05 KB

07.封装RpcController模块.md

File metadata and controls

129 lines (98 loc) · 4.05 KB

RpcController的作用

在 protobuf 中,RpcController 定义在 service.h 头文件中,RpcController 是一个抽象类,其中的成员函数都需要我们自己实现。我们可以通过 RpcController 的方法得到 RPC 调用是否发生错误。并可以重置 RPC 连接。

比如客户端调用 rpc 方法,如果错误会打印错误的 response 信息。但是有可能这个 rpc 发生的错误导致根本不会有信息返回,比如网络传输出现了问题。那么这个时候如何得到提示信息呢?这个时候就可以使用 RpcController 来判断这次 rpc 方法调用是否成功。

class PROTOBUF_EXPORT RpcController 
{
public:
    inline RpcController() {}
    virtual ~RpcController();

    virtual void Reset() = 0;
    virtual bool Failed() const = 0;
    virtual std::string ErrorText() const = 0;
    virtual void StartCancel() = 0;
    virtual void SetFailed(const std::string& reason) = 0;
    virtual bool IsCanceled() const = 0;
    virtual void NotifyOnCancel(Closure* callback) = 0;

private:
	GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcController);
};

RpcController API

Reset() 将 RpcController 重设为初始状态,以便在新的调用中可以重新使用。这只能从客户端调用。当一个 RPC 正在进行时,它不能被调用。

virtual void Reset() = 0

Failed()在一个调用完成后,如果调用失败,则返回 true。失败的可能原因取决于 RPC 的实现。 failed()只能在客户端调用,并且不能在调用结束前调用。

virtual bool Failed() const = 0

如果 Failed() 为 true,则返回用户可读的错误描述。

virtual std::string	ErrorText() const = 0

startCancel()告知 RPC 系统,调用者希望取消 RPC 调用。RPC 系统可以立即取消它,也可以等待一段时间后再取消它,或者甚至根本不取消调用。如果调用被取消,done回调仍将被调用,RpcController 将表明当时的调用失败。

virtual void StartCancel() = 0

isCanceled() 如果为真,表示客户端取消了 RPC,所以服务器可能会放弃对它的回复。这个方法必须只在服务器端调用。服务器仍然应该调用最后的 "完成 "回调。

boolean isCanceled()

NotifyOnCancel()要求在 RPC 被取消时调用给定的回调。传递给回调的参数将永远是空的。回调将总是被精确地调用一次。如果 RPC 完成而没有被取消,回调将在完成后被调用。如果当 NotifyOnCancel() 被调用时,RPC 已经被取消了,回调将被立即调用。

NotifyOnCancel() 必须在每个请求中被调用不超过一次。它必须只在服务器端调用。

virtual void NotifyOnCancel(Closure * callback) = 0

MprpcController实现

#ifndef __MPRPC_CONTROLLER_H__
#define __MPRPC_CONTROLLER_H__

#include <google/protobuf/service.h>
#include <string>

class MprpcController : public google::protobuf::RpcController
{
public:
    MprpcController();
    void Reset();
    bool Failed() const;
    std::string ErrorText() const;
    void SetFailed(const std::string& reason);

    // TODO:目前未实现具体的功能
    void StartCancel();
    bool IsCanceled() const;
    void NotifyOnCancel(google::protobuf::Closure* callback);
private:
    bool m_failed; // RPC方法执行过程中的状态
    std::string m_errText; // RPC方法执行过程中的错误信息
};

#endif // __MPRPC_CONTROLLER_H__
#include "mprpc_controller.h"

MprpcController::MprpcController()
{
    m_failed = false;
    m_errText = "";
}

void MprpcController::Reset()
{
    m_failed = false;
    m_errText = "";
}

bool MprpcController::Failed() const
{
    return m_failed;
}

std::string MprpcController::ErrorText() const
{
    return m_errText;
}

void MprpcController::SetFailed(const std::string& reason)
{
    m_failed = true;
    m_errText = reason;
}

// 目前未实现具体的功能
void MprpcController::StartCancel(){}
bool MprpcController::IsCanceled() const {return false;}
void MprpcController::NotifyOnCancel(google::protobuf::Closure* callback) {}