本文描述以下内容:
.如何利用协议脚本校验参数
.如何利用协议检查器访问参数
1.协议脚本基本介绍
支持利用XML文件描述协议参数的约束,称为协议规则文件,关于每个协议的内容称为协议脚本
利用协议脚本可以简化编码,且在变化时不需要修改程序..(更多特性以后介绍).
每个插件可以定义一个协议规则文件,文件名与插件同名,扩展名为:pr.
letein的协议规则文件letein.pr,内容如下:
<?xml version="1.0" encoding="gb2312" standalone="yes" ?>
<protocol>
<msgs>
<msg id="601" type="5" desc="企业注册">
<parameters>
<parameter>
<name>Name,Address,Manager,Contact,Account,Pswd,Mobile,VerifyCode</name>
<comment>企业名称,地址,负责人,联系人,账号,密码,联系人手机,验证码</comment>
<nullable>false</nullable>
</parameter>
<parameter>
<name>Tele,Fax,Email,ManagerTel,ManagerMobile</name>
<comment>电话,传真,Email,负责人电话,负责人手机</comment>
<nullable>true</nullable>
</parameter>
<parameter>
<name>OrgType,AreaId,Postcode</name>
<comment>企业类型,区域编码,邮编</comment>
<type>1</type>
<nullable>true</nullable>
</parameter>
</parameters>
</msg>
</msgs>
</protocol>
2.主要代码
协议:601-Request,该协议不需要安全验证.
处理企业注册请求.
MSG_FUNC_MAP CLetein::func_[] = {
{601,MT_REQUEST,(MSGFUNC)&CLetein::OnRegister,true,"企业注册",""},
};
////////////////////////////////////////////////////////////////////////////////
int CLetein::Initialize() {
parent::Initialize();
sec_->SetProtAuthProp(0,601,false);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int CLetein::OnRegister(CWrappedMsg<> *in,vector<CWrappedMsg<> *> &out,DISPATCH_RESULT &or) {
CMsg *msg = in->msg;
///< 协议检查器对letein.pr检查的结果,验证不通过时不会调用协议处理函数.
IProtCheckResult* chk_result = or.chk_result;
char *verify_code = 0;
chk_result->GetParam("VerifyCode",&verify_code);
///< @todo 检查短信验证码
CEnterprise org;
///< 从协议检查器获取企业对象成员信息
chk_result->GetParam("Name",org.name_);
chk_result->GetParam("OrgType",*(unsigned int*)&org.type_);
chk_result->GetParam("AreaId",org.area_id_);
chk_result->GetParam("Address",org.addr_);
chk_result->GetParam("Postcode",org.postcode_);
chk_result->GetParam("Tele",org.tele_);
chk_result->GetParam("Fax",org.fax_);
chk_result->GetParam("Email",org.email_);
chk_result->GetParam("ManagerTel",org.mgr_tele_);
chk_result->GetParam("ManagerMobile",org.mgr_mobile_);
chk_result->GetParam("Contact",org.contract_);
char *account = 0;
chk_result->GetParam("Account",&account);
char *pswd = 0;
unsigned int pswd_len = 0; ///< @todo 密码目前没有加密
chk_result->GetParam("Pswd",&pswd);
chk_result->GetParam("Mobile",org.contract_mobile_);
GETDBC(pdbor,this->local_dbc_.c_str());
FAIL_RETURN(pdbor->BeginTrans(),,-1);
///< 分配企业ID
SLIC_EID eid = CEnterprise::ApplyID(org.type_);
if (eid==-1) {
return -1;
}
///< 分配主站
SLIC_SITE_ID site_id = org.AssignSite();
if (site_id==-1) {
return -1;
}
if (org.Save())
return -1;
///< @todo 创建用户
FAIL_RETURN(pdbor->CommitTrans(),,-1);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
3.框架升级
IProtCheckResult增加2个方法:
class IProtCheckResult {
public:
virtual int GetParam(const char *name,string &val) = 0;
virtual int GetParam(const char *name,char **val,unsigned int &len) = 0;
};
目的分别是:
.支持string类型变量
.支持二进制数据访问
xebo版本:4.5