如何解决通信架构迁移时的消息格式不一致问题

假设在旧的架构中有这样的普通消息体结构:

class A {
public:
    int a = 1;
    int b;
};

其中,a是传入参数,b是传出参数。

而在新的架构中,消息体要满足以下结构:

class Message {
public:    
struct Request {} request;
struct Response {} response; };

如果完全改写消息,则会造成极大范围的影响,并且要全局一起修改,短时间内难以验证正确性。所以,可以采用定义内嵌引用的方式来解决,如下示例:

class A {
public:
    int& a;
    int& b;

    struct Request {
        int a = 1;
    } request;

    struct Response {
        int b;
    } response;

    A() : a(request.a), b(response.b) {}
}

按上述方法,即可使原数据类型实现兼容。

posted @ 2023-04-26 11:19  倾越  阅读(12)  评论(0编辑  收藏  举报