精英盒子 -> 零毫秒 -> 想到一个好主意来解决代码中多协议并存的问题- [打印本页]

jybox 2011-11-13 17:04

想到一个好主意来解决代码中多协议并存的问题-

在重构之前采用的是这样的方法:
const.h中的常量定义:
  1. //正在使用的协议
    const int P_USE = 1;

    //-------------------------------------------------
    //协议相关 版本1

    //头部长度
    const int P1_HEAD_LEN = 16;
    //用于waitForLen,代表等待一个头
    const int P1_HEAD = -1;

    const int M1_Error = 0;
    const int M1_AskTime = 10;
    const int M1_Time = 11;
    const int M1_Ping = 20;
    const int M1_Exit = 30;
    const int M1_CMsg = 100;
    const int M1_SMsg = 101;
    const int M1_Login = 200;
    const int M1_LoginOk = 211;
    const int M1_LoginError = 213;
    const int M1_AskUList = 300;
    const int M1_UList = 301;
    const int M1_ChangeUList = 311;

    //判断版本是否支持
    inline int checkVer(int n)
    {
        if(n==1)
            return 1;
        else
            return 0;
    }



重构之后:
单独用一个p1.h来写于第一版协议相关的内容:
  1. namespace P1
    {
    //--该文件是1版通讯协议的常量定义文件
    const int P_VER = 1;

    //头部长度
    const int P_HEADLEN        = 16;

    //消息编号
    const int M_Error        = 0;
    const int M_AskTime        = 10;
    const int M_Time        = 11;
    const int M_Ping        = 20;
    const int M_Exit        = 30;
    const int M_CMsg        = 100;
    const int M_SMsg        = 101;
    const int M_Login        = 200;
    const int M_LoginOk        = 211;
    const int M_LoginError        = 213;
    const int M_AskUList        = 300;
    const int M_UList        = 301;
    const int M_ChangeUList        = 311;

    //关于用户验证api的返回信息
    const int UAPI_RESULT            = 0;
    const int UAPI_UNAME            = 1;
    const int UAPI_LISTNAME            = 2;
    const int UAPI_CLIENTVER    = 3;
    const int UAPI_CLIENTNAME   = 4;

    //验证是否兼容该协议版本
    inline int checkVer(int n)
    {
        if(n==1)
            return 1;
        else
            return 0;
    }

    }        //namespace P1

然后在const.h中包含p1.h,并包含引用命名空间
  1. #include "p1.h"
    using namespace P1;



哈哈,我真聪明


abreto 2011-11-14 22:56
这是啥?

jybox 2011-11-15 00:00
abreto:[表情] 这是啥? (2011-11-14 22:56) 

你有没有看啊

scxyscxy 2011-11-15 08:53
我承认我看不懂

jybox 2011-11-15 11:01
第3楼sky于2011-11-15 08:53发表的:
我承认我看不懂
不会吧.....




Powered by phpwind v8.7 Code ©2003-2011 phpwind
Time 0.048789 second(s),query:5 Gzip enabled