信息系统框架集成第三方产品案例

来源:互联网 发布:navicat 修改表中数据 编辑:程序博客网 时间:2024/06/11 10:55

传智扫地僧课程学习笔记。


之前,分析了产品背景,这里我没写出来,不长,自己去看就行了,


先定义一套产品接口,用于让第三方厂商按照这个接口来实现,

分别是接口的初始化,发送,接收,销毁,

CSocketProtocol.h

class CSocketProtocol{public:virtual int cltSocketInit( ) = 0;virtual int cltSocketSend(  unsigned char *buf, int buflen ) = 0;virtual int cltSocketRev(  unsigned char *buf, int *buflen ) = 0;virtual int cltSocketDestory( ) = 0;};

在mainclass.cpp文件中,测试能否使用,

int SckSendAndRec( CSocketProtocol *sp, unsigned char *in, int inlen, unsigned char *out, int *outlen){int ret = 0;ret = sp->cltSocketInit();if( ret!= 0){goto End;}ret = sp->cltSocketSend( in, inlen);if( ret!= 0){goto End;}ret = sp->cltSocketRev( out, outlen);if( ret!= 0){goto End;}End:ret = sp->cltSocketDestory();return 0;}
这里来个题外话,由于我没有看C,直接来看C++的,所以这里的handle句柄若想多了解,去看C实现版本,


接下来,厂商1来实现这套接口,

CSckFactoryImp1.h

class CSckFactoryImp1 : public CSocketProtocol{public:virtual int cltSocketInit( );virtual int cltSocketSend(  unsigned char *buf, int buflen );virtual int cltSocketRev(  unsigned char *buf, int *buflen );virtual int cltSocketDestory( );private:unsigned char *p;int len;};
这里也和上面所说呼应起来了,

C中的实现是用结构体,C++突破了函数的概念,

用对象来实现,将函数和数据封装在一起,

CSckFactoryImp1.cpp

#include "CSckFactoryImp1.h"int CSckFactoryImp1::cltSocketInit( ){p = NULL;len = 0;return 0;}int CSckFactoryImp1::cltSocketSend(  unsigned char *buf, int buflen ){p = ( unsigned char *)malloc( sizeof( unsigned char)*buflen);if( p == NULL ){return -1;}memcpy( p, buf, buflen);len = buflen;return 0;}int CSckFactoryImp1::cltSocketRev(  unsigned char *buf, int *buflen ){if( buf == NULL || buflen == NULL ){return -1;}*buflen = this->len;memcpy( buf, this->p, this->len);return 0;}int CSckFactoryImp1::cltSocketDestory( ){if( p != NULL ){free( p );p = NULL;len = 0;}return 0;}
类中函数操作的变量是char *p,int len,注意下操作,

然后我真正想说的是,Rev函数中,buflen为什么要通过指针来操作,

因为我们这里是接收数据,修改数据没问题,本来就是指针,

但是修改长度,通过函数的话,你修改的可能是函数的参数,所以得用指针的方式,ok,

提示重复包含,记得加#pragma once



基本没什么变动,复制刚才的是实现,修改几个名字,就简单的实现了第二个厂商的产品,



实现加密,解密,

这是让第三方实现的接口,

CEncDesProtocol.h

#pragma onceclass CEncDesProtocol{public:CEncDesProtocol(){;}virtual ~CEncDesProtocol(){;}virtual int EncData( unsigned char *plain, int plainlen, unsigned char *cryptdata, int *cryptlen) = 0;virtual int DecData( unsigned char *cryptdata, int cryptlen, unsigned char *plain, int *plainlen) = 0;};



然后是第三方的实现,HwEncDec.h文件就没什么好贴的了,就是继承,下面是实现,

HwEncDec.cpp

#include "iostream"using namespace std;#include "HwEncDec.h"#include "des.h"int DesEnc(unsigned char *pInData,int            nInDataLen,unsigned char *pOutData,int           *pOutDataLen);//用户使用函数des解密int DesDec(   unsigned char *pInData,   int            nInDataLen,   unsigned char *pOutData,   int           *pOutDataLen);int HwEncDec::EncData( unsigned char *plain, int plainlen, unsigned char *cryptdata, int *cryptlen){int ret;ret = DesEnc( plain, plainlen, cryptdata, cryptlen);if( ret != 0 ){cout<<"func desenc err"<<ret<<endl;return ret;}return ret;}int HwEncDec::DecData( unsigned char *cryptdata, int cryptlen, unsigned char *plain, int *plainlen){int ret = 0;ret = DesDec( cryptdata, cryptlen, plain, plainlen);if( ret != 0 ){cout<<"func desenc err"<<ret<<endl;return ret;}return ret;}
中间调用了已经实现的文件,des.h,des.cpp,



接着是构建测试环境, 来测试上面的加密解密,

int SckSendAndRec_EncDec(CSocketProtocol *sp, CEncDesProtocol *ed, unsigned char *in, int inlen, unsigned char *out, int *outlen){int ret = 0;unsigned char data[4096];int datalen = 0;ret = sp->cltSocketInit();if (ret != 0){goto End;}ret = ed->EncData(in,inlen, data, &datalen);if (ret != 0){goto End;}ret = sp->cltSocketSend(data, datalen); //发送数据之前对数据加密 ..if (ret != 0){goto End;}ret = sp->cltSocketRev(data, &datalen); //收到的数据是密文,需要进行解密if (ret != 0){goto End;}ret = ed->DecData(data, datalen, out, outlen );if (ret != 0){goto End;}End:ret = sp->cltSocketDestory();return 0;}
这个测试环境,是在之前那个环境的基础之上,添加了我们刚才完成的加密,解密类的实现,

参数的传入多了

CEncDesProtocol *ed,这里面包含了包含了加解密,

unsigned char data[4096];
int datalen = 0; 这2个变量,用于起到明文到密文,密文到明文之间转换的时候,一个存储的容器,

突然想到一个问题,尼玛,这中间的长度,怎么确定的,想起来了,在最初传输的时候,就设置了一个长度值,后面就用的它,

CSocketProtocol *sp = NULL;
CEncDesProtocol *ed = NULL;
sp = new CSckFactoryImp2; /
ed = new HwEncDec;
ret = SckSendAndRec_EncDec(sp, ed, in, inlen, out, &outlen);




最后一步改进,

改为用类的方式来实现,

只选取了关键代码

class  MainOp : public CSocketProtocol, public CEncDesProtocol{public:protected:private:};//可以用继承的方式,也可以用组合的方式,即类成员就是我们要用的类对象指针,class MainOp{public:MainOp(){this->sp = NULL;this->ed = NULL;}MainOp(CSocketProtocol *sp, CEncDesProtocol *ed){this->sp = sp;this->ed = ed;}void setSp(CSocketProtocol *sp){this->sp = sp;}void setEd(CEncDesProtocol *ed){this->ed = ed;}public:int SckSendAndRec_EncDec3(CSocketProtocol *sp, CEncDesProtocol *ed, unsigned char *in, int inlen, unsigned char *out, int *outlen){这里的实现和下面的实现一样,不同的是这里的sp,ed是参数传进来的,而且由于函数重载的缘故,这里是对的,}int SckSendAndRec_EncDec3(unsigned char *in, int inlen, unsigned char *out, int *outlen){int ret = 0;unsigned char data[4096];int datalen = 0;ret = this->sp->cltSocketInit();if (ret != 0){goto End;}ret = this->ed->EncData(in,inlen, data, &datalen);if (ret != 0){goto End;}ret = this->sp->cltSocketSend(data, datalen); //发送数据之前对数据加密 ..if (ret != 0){goto End;}ret = sp->cltSocketRev(data, &datalen); //收到的数据是密文,需要进行解密if (ret != 0){goto End;}ret = ed->DecData(data, datalen, out, outlen );if (ret != 0){goto End;}End:ret = sp->cltSocketDestory();return 0;}private:CSocketProtocol *sp;CEncDesProtocol *ed;};//写一个框架int main(){int ret = 0;unsigned char in[4096];int inlen;unsigned char out[4096];int outlen = 0;strcpy((char *)in, "aadddddddddddaaaaaaaaaaa");inlen = 9;MainOp *myMainOp = new MainOp;CSocketProtocol *sp = NULL;CEncDesProtocol *ed = NULL;//sp = new CSckFactoryImp1sp = new CSckFactoryImp2; //ed = new HwEncDec;myMainOp->setSp(sp);myMainOp->setEd(ed);ret = myMainOp->SckSendAndRec_EncDec3(in, inlen, out, &outlen);if (ret!= 0){printf("myMainOp SckSendAndRec_EncDec3() err\n ", ret);}delete sp;delete ed;delete myMainOp;cout<<"hello..."<<endl;system("pause");return ret;}



几个面向对象的思想,

继承,组合,就是我们刚才用的,


注入,把类的实现,如协议的实现,加解密的实现,注入我们的mainop中,


控制反转,即我们调用main函数,main函数调用类实现,


MVC,

model 模型,即类,

V,视图,。。。忘记了,,,

C,控制,。。。。


0 0
原创粉丝点击