資訊傳遞工具protobuf通過pb的反射來賦值

來源:互聯網
上載者:User

最近因為項目邏輯方面都做的差不多了,於是趕緊去做做測試工作,免得項目上線之後出問題,於是打算用配置的形式做一個類似與白盒測試載入器的東西出來。

因為項目使用pb來做協議通訊,所以配置的xml也是類似於pb,將pb的欄位和類型配置進去,然後加上值,一個協議結構就可以了,現在只能通過修改值來做測試,後面會改動的更智能化一些,例如某個行為的次數,某個行為更隨機等等。

去讀了一下陳碩的關於pb處理協議的反射,學到了不少東西,同時對pb的一些東西理解更深刻了,google還是大牛很多。

1.如何處理pb的反射,通過協議字串動態產生一個協議

pb提供了一個強大的DescriptorPool::generated_pool()

代碼如下:

 代碼如下 複製代碼
inline google::protobuf::Message* CreateMessage(const std::string& msg)
{
    google::protobuf::Message* message = NULL;
    const google::protobuf::Descriptor* descriptor = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName("ProtoMsg." + msg);
    if (descriptor)
    {
        const google::protobuf::Message* prototype = google::protobuf::MessageFactory::generated_factory()->GetPrototype(descriptor);
        if (prototype)
        {
            message = prototype->New();
        }
    }

    return message;
}

inline void ReleaseMessage(google::protobuf::Message* pMsg)
{
    if (NULL != pMsg)
    {
        pMsg->Clear();
        delete pMsg;
    }
}



這兩個函數可以動態產生pb的message,其中ProtoMsg是你pb package的名字

2.通過反射將配置中的值設定進pb欄位

pb的Message基類提供了一個Reflection,這個類非常強大

代碼如下

 代碼如下 複製代碼
// mstrCurMsg 當前正在執行的協議
google::protobuf::Message* pMsg = Test::CreateMessage(mstrCurMsg);
const google::protobuf::Descriptor* pDescriptor = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName("ProtoMsg." + mstrCurMsg);
assert(NULL != pDescriptor);

// 這一個可以擷取到反射類,然後可以將配置中值賦值進去
const google::protobuf::Reflection* pReflection = pMsg->GetReflection();
assert(NULL != pReflection);

for (int i = 0; i < pDescriptor->field_count(); ++i)
{
    const google::protobuf::FieldDescriptor* pFieldDescriptor = pDescriptor->field(i);
    assert(NULL != pFieldDescriptor);

    const std::string& strFieldName = pFieldDescriptor->name();
   
    const TestConfigModule::MsgEntry* pMsgEntry = pMsgStruct->GetMsgEntry(strFieldName);
    assert(NULL != pMsgEntry);

    // 讀取欄位類型,順帶可以做類型檢查
    assert(pMsgEntry->mnType == pFieldDescriptor->type());

    // 設定值
    switch (pMsgEntry->mnType)
    {
    case Test::TYPE_STRING:
        pReflection->SetString(pMsg, pFieldDescriptor, pMsgEntry->mstrValue);
        break;
    // ...
    default:
        break;
    }
}

std::string strData;
if (!pMsg->SerializeToString(&strData))
{
    m_pLogModule->LogNormal("Test stop, cannot SerializeToString ", mstrCurMsg, __FUNCTION__, __LINE__);
    return;
}

Test::ReleaseMessage(pMsg);



通過這樣的步驟,就可以自動建立message和對field賦值了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.